Invalid JPEG file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Invalid JPEG file

Post 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; 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

how can I bypass this INVALID JPEG problem?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply