[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

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?
0
feign3
Asked:
feign3
  • 2
1 Solution
 
AmigoJackCommented:
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...)
0
 
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']);
      
      $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.
0
 
AmigoJackCommented:
the following two lines:

    $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));

...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 (http://www.php.net/manual/en/function.imagecreate.php) also recommends to use imagecreatetruecolor() instead.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now