Link to home
Start Free TrialLog in
Avatar of feign3
feign3

asked on

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: http://www.starfieldmedia.com/thumb_problem.htm

Is anyone familiar with this problem?
Avatar of AmigoJack
AmigoJack

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...)
Avatar of feign3

ASKER

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']);
      
      $size=GetImageSize("../PICS/".$image_name);
      $width =$size[0];
      $height=$size[1];
      
      $x_ratio = $max_width / $width;
      $y_ratio = $max_height / $height;
      
      if ( ($width <= $max_width) && ($height <= $max_height)) {
      $tn_width=$width;
      $tn_height=$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);
      $tn_height=$max_height;
      }
      
      $new_w = $tn_width;
    //$diff=$origw/$new_w;
    //$new_h=$new_w;
      $new_h = $tn_height;
    $dst_img = imagecreate($new_w,$new_h);
    imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
    imagejpeg($dst_img, "$thumb_path/TN_$image_name",100);
    return true;
}
thumbnail("../PICS","../PICS",$imgf,150);




This doesn't happen to every thumbnail... maybe one in every 5.
ASKER CERTIFIED SOLUTION
Avatar of AmigoJack
AmigoJack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial