Problem generating thumbnail from imagejpeg

Hi guys!

I’m trying to generate thumbnail images in WordPress. The easiest way would have been to use the EXIF suite of functions, but the guys behind WordPress, in their infinite wisdom, decided to strip out the image previews from any photographs uploaded via their Media Library.

So, I’m now reliant on GD Library instead. However, I can’t seem to get to work either, and I don’t know why.

I’m using a function written by David Walsh, which should work, but doesn’t:

function make_thumb($src, $desired_width) {


    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);
    
    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));
    
    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
    
    /* copy source image at a resized size */
    imagecopyresized($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);


    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image);


}

You may notice that I’ve stripped out the destination variable, as I want this to print directly to screen.

Instead of generating an image, per the imagejpeg() function, it’s just burping out raw code.

Now, I’ve tried encoding this with the base64_encode() function, I even tried decoding with it’s sibling, and I’ve also tried the [URL=“http://php.net/manual/en/function.header.php”]header() function, but nothing works.

I can’t use a Plugin, as I have very specific requirements to meet.

Any suggestions would be greatly appreciated.

What do you mean by saying

? You need to tell the browser that the output is a JPEG image and you’d do it by sending a Content-type header:


header("Content-type: image/jpeg");

You need to send it before calling the imagejpeg() function.

Hi, and thanks for the reply.

And yes, I’ve done all of the above.

I’ve done this kind of thing before (many times), but for whatever reason, it’s just not working within WordPress.

Any more takers on this one?

How are you calling this script?

It should be something like this:

<img src="thumbnail.php?image=myphotos/photo.jpg" />

Also, if you request the file making the thumbnail directly, does it display anything?

Last note: if you’re using this to show a gallery of thumbnails, you might want to save them. Generating a bunch of thumbnails on the fly like this will eat processor time :slight_smile:

Hi!

I was trying to run the function inside the same page, but it looks as though I’ll have to external (per the example), which is the way I created thumbnails for a gallery 4 or 5 years ago.

However, the more I think about it, the more I think it’s going to make more sense to just upload images by hand, given that this technique just isn’t as efficient as the EXIF technique.