• You are not logged in. | Login

Post a reply

November 16, 2006 3:27 am

steven9x
Member
Ranks

problem creating image thumbnails

Hi, I have found a tutorial to create thumbnails on the fly so that I don't have to resize my images. Here's the code:

# Constants
define(IMAGE_BASE, './images');
define(MAX_WIDTH, 200);
define(MAX_HEIGHT, 200);

function show_thumbnail($image, $dirname)
{

# Get image location
$image_path = IMAGE_BASE . "/" . $dirname . "/" . $image;

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image)));
if ($ext == 'jpg' || $ext == 'jpeg') {
    $img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
    $img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
    $img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

    # Get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

    # If the image is larger than the max shrink it
    if ($scale < 1) {
        $new_width = floor($scale*$width);
        $new_height = floor($scale*$height);

        # Create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);

        # Copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                         $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }
}# Create error image if necessary
if (!$img) {
    $img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
    imagecolorallocate($img,0,0,0);
    $c = imagecolorallocate($img,70,70,70);
    imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
    imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);

}

Now when I put in my page:

<img src="<?php show_thumbnail($img, $dir)?>" alt='Resized Image'>

I get this weird error:

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\speelschare\index.php:23) in c:\program files\easyphp1-8\www\speelschare\sources\fotos\test2.php on line 59
ÿØÿàJFIFÿþ&gt;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#(7),01444'9=82&lt;.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀÈÈ"ÿÄ ÿĵ}!1AQa"q2?‘¡#B±ÁRÑð$3br‚

with lots of other gibberish.

Can someone tell me what's wrong?


 

 

November 16, 2006 4:04 am

phppat
Member
Ranks

Re: problem creating image thumbnails

The trouble here is the show_thumbnail function expects the image to be the only thing on the page. 

You can get around this by calling the function from a seperate file, and passing the name of the image you're looking for, for example:

Create a new file - image.php:

<?php

$img = $_GET['img']
$path = $_GET['path']

## include the show_thumbnail function declaration here
function show_thumbnail ($image, $dirname ) {
  ...code...
  ...code...
}

##  And then call the function
if ($img && $path) 
{
  show_thumbnail($img, $path);
}

?>

Then, change the image source to point to your new script, passing the image data along:

<img src="image.php?img=myimg.jpg&path=yourpath" alt='Resized Image'>

That should do the trick.


PHP monster

 

 
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services