Page 1 of 1

Invalid JPEG file

Posted: Mon Mar 07, 2005 1:34 am
by s.dot
I've mentioned before that sometimes I get blank black thumbnails when someone uploads a picture. I searched through my error log and this is the error I get.

[06-Mar-2005 17:50:41] PHP Warning: imagecreatefromjpeg(): 'uploads/sassy_stacy/1110149441.jpg' is not a valid JPEG file in /home/scottttt/public_html/createthumb.inc.php on line 6

However, when the picture is viewed fullsize, it shows up fine. What could the problem be?

Here is my thumbnail code:

Code: Select all

<?
function createthumb($name,$filename,$new_w,$new_h)&#123;
    global $gd2;
    $system=explode(".",$name);
    if (preg_match("/jpg|jpeg|JPG|JPEG|Jpg|JpG|jPG|JPg|JPEg|JPeg|Jpeg|jPEG|jpEG|jPeG|JpEg|JPEg|JpeG|jpEg|jpeG/",$system&#1111;1]))&#123;
        $src_img=imagecreatefromjpeg($name);
    &#125;
    if (preg_match("/png|PNG|pNg|Png|pNG|PnG/",$system&#1111;1]))&#123;
        $src_img=imagecreatefrompng($name);
    &#125; 
    $old_x=imageSX($src_img);
    $old_y=imageSY($src_img);
    if ($old_x > $old_y) &#123;
        $thumb_w=$new_w;
        $thumb_h=$old_y*($new_h/$old_x);
    &#125;
    if ($old_x < $old_y) &#123;
        $thumb_w=$old_x*($new_w/$old_y);
        $thumb_h=$new_h;
    &#125;
    if ($old_x == $old_y) &#123;
        $thumb_w=$new_w;
        $thumb_h=$new_h;
    &#125; 
        $dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

    if (preg_match("/png|PNG|pNg|Png|pNG|PnG/",$system&#1111;1]))&#123;
        imagepng($dst_img,$filename);
    &#125; else &#123;
        imagejpeg($dst_img,$filename);
    &#125;
    imagedestroy($dst_img);
    imagedestroy($src_img);
&#125; 
?>

Posted: Mon Mar 07, 2005 1:47 am
by feyd
there are MANY variants to JPEG files.. as JPEG is an interchange file format, not a specific graphic format. So it could be JBIG, JPEG lossless, JFIF, EXIF.. there are many versions available. Not all are supported by GD.

Posted: Mon Mar 07, 2005 1:49 am
by s.dot
how can I bypass this INVALID JPEG problem?

Posted: Mon Mar 07, 2005 1:55 am
by feyd
for the most compatibility, you need to use something that can load all the variants of JPEG, such as ImageMagick

If you want to just ignore them altogether, use getimagesize() to detect if gd can (likely) read the file. Add code to ensure that you handle these errors/warnings gracefully.

Posted: Mon Mar 07, 2005 11:47 am
by pickle
Somewhat off-topic, but I'd suggest using getimagesize() to get the file type, and use it once as opposed to using preg_match multiple times.