Corrupt thumbnails with imagejpeg()

Posted on 2006-05-01
Last Modified: 2008-02-01
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?
Question by:feign3
    LVL 12

    Expert Comment

    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...)

    Author Comment

    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.
    LVL 12

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now