Corrupt thumbnails with imagejpeg()

I have created a script to create and upload a thumbnail of a selected image however, once in a while the thumbnail comes out corrupt as seen in this link:

Is anyone familiar with this problem?
Who is Participating?
AmigoJackConnect With a Mentor Commented:
the following two lines:

    $dst_img = imagecreate($new_w,$new_h);

...i am using differently. the rest is the same, however - here are my two lines:

            $dst_img = imagecreatetruecolor($new_w, $new_h);
            imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));

...i suspect it might be the use of imagecreatetruecolor rather than imagecopyresampled versus imagecopyresized. so you might only want to change the first line.

the documentation ( also recommends to use imagecreatetruecolor() instead.
no, dont ever encountered such a problem. you might want to give us your code and your system on which you perform the code (php version, gd-library version...)
feign3Author Commented:
Hi AmigoJack,

Here's the data you requested:

PHP Version 4.4.2
GD Support  enabled  
GD Version  bundled (2.0.28 compatible)  
FreeType Support  enabled  
FreeType Linkage  with freetype  
GIF Read Support  enabled  
GIF Create Support  enabled  
JPG Support  enabled  
PNG Support  enabled  
WBMP Support  enabled  
XBM Support  enabled  

System: FreeBSD 5.4-STABLE

Here's the code (variable assignments not included):

function thumbnail($image_path,$thumb_path,$image_name,$thumb_width)
                $src_img = imagecreatefromjpeg("$image_path/$image_name");
      if (!isset($HTTP_GET_VARS['maxwidth'])) $max_width=150; else $max_width=($HTTP_GET_VARS['maxwidth']);
      if (!isset($HTTP_GET_VARS['maxheight'])) $max_height=600; else $max_height=($HTTP_GET_VARS['maxheight']);
      $width =$size[0];
      $x_ratio = $max_width / $width;
      $y_ratio = $max_height / $height;
      if ( ($width <= $max_width) && ($height <= $max_height)) {
      else if (($x_ratio * $height) < $max_height) {
      $tn_height = ceil($x_ratio * $height);
      $tn_width =$max_width;
      else {
      $tn_width = ceil($y_ratio * $width);
      $new_w = $tn_width;
      $new_h = $tn_height;
    $dst_img = imagecreate($new_w,$new_h);
    imagejpeg($dst_img, "$thumb_path/TN_$image_name",100);
    return true;

This doesn't happen to every thumbnail... maybe one in every 5.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.