Page 1 of 1

Thumbnails Creation Error

Posted: Mon Feb 14, 2005 7:21 pm
by s.dot
So I have this code that generates thumbnails for images uploaded

Once in a blue moon it creates a black thumbnail.
Here's the 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|/",$system&#1111;1]))&#123;
        $src_img=imagecreatefromjpeg($name);
    &#125;
    if (preg_match("/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; 
   // if ($gd2=="")&#123;
   //     $dst_img=ImageCreate($thumb_w,$thumb_h);
   //     imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
   // &#125;else&#123;
        $dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
    // &#125; 
    if (preg_match("/png/",$system&#1111;1]))&#123;
        imagepng($dst_img,$filename);
    &#125; else &#123;
        imagejpeg($dst_img,$filename);
    &#125;
    imagedestroy($dst_img);
    imagedestroy($src_img);
&#125; 
?>
Does anyone see an error in this that might be causing this problem?

Posted: Mon Feb 14, 2005 8:01 pm
by Buddha443556
Here's what I would look into ...

Code: Select all

$system=explode(".",$name);
What happens if there's more than one "."? What good is $system[1] then?

Code: Select all

if (preg_match("/jpg|jpeg|JPG|JPEG|/",$system&#1111;1]))&#123;
The extra "|" at the end will match even nothing or even png and the real problem PNG's too.

Code: Select all

if (preg_match("/png/",$system&#1111;1]))&#123;
This one doesn't catch PNG just png.

Code: Select all

if (preg_match("/png/",$system&#1111;1]))&#123;
        imagepng($dst_img,$filename);
    &#125; else &#123;
        imagejpeg($dst_img,$filename);
    &#125;
Well if you are sending PNG thru imagecreatefromjpeg($name) this test isn't going to help because they goto imagejpeg().

Hope this helps ... :)

Posted: Mon Feb 14, 2005 11:44 pm
by s.dot
Buddha443556 wrote:Here's what I would look into ...

Code: Select all

$system=explode(".",$name);
What happens if there's more than one "."? What good is $system[1] then?
How would I effectively check to make sure that the . $system[1] is referring to is the last . in the filename, giving me the file extension?

Posted: Mon Feb 14, 2005 11:46 pm
by feyd
why not specifically refer to the last element in the array?

Posted: Tue Feb 15, 2005 1:37 am
by s.dot
I'm unaware of how to do that. Explain?

Posted: Tue Feb 15, 2005 1:38 am
by feyd
ever heard of sizeof() or count() ... add a little math, and you have the last element of the array.

there are array_* functions that can break off the last element too.. which I'll leave to you to find which ones do it..

Posted: Tue Feb 15, 2005 2:08 am
by s.dot
Well, actually I didn't think before I asked the question about the . s in $system[1], I have renamed the file to be thumbnailed time().extension , so there's sure to be only 1 . in the file name..... So forgive me.

However my next question would be this.

Code: Select all

<? 
function createthumb($name,$filename,$new_w,$new_h)&#123; 
    global $gd2; 
    $system=explode(".",$name); 
    if (preg_match("/jpg|jpeg|JPG|JPEG|/",$system&#1111;1]))&#123; 
        $src_img=imagecreatefromjpeg($name); 
    &#125; 
    if (preg_match("/png/",$system&#1111;1]))&#123; 
        $src_img=imagecreatefrompng($name); 
    &#125; 
?>
Where is the $name variable coming from (I didn't write this thumbnail script)? I know that filename is the name of the file that I have renamed.

Posted: Tue Feb 15, 2005 2:14 am
by n00b Saibot
scrotaye wrote:Where is the $name variable coming from
$name is the filename of the original pic file that you want to convert to thumbnail. you will pass this argument when calling this function

Posted: Tue Feb 15, 2005 2:18 am
by s.dot
ah I see, thanks.

Well, I have personally tested out the theory that multiple .'s is what's giving me a black picture some of the time. I renamed a file to 1.1.1.1.2.jpg and uploaded it and it thumbnailed fine.

Any other possibly theory as to why some thumbnails are generated as blank black images?

Posted: Tue Feb 15, 2005 2:28 am
by n00b Saibot
scrotaye wrote:Well, I have personally tested out the theory that multiple .'s is what's giving me a black picture some of the time. I renamed a file to 1.1.1.1.2.jpg and uploaded it and it thumbnailed fine.
it was because it still passed the jpeg|JPEG.. match criteria if the criteria had been strict, your test would have failed...

Posted: Tue Feb 15, 2005 7:09 am
by Buddha443556
Well, I have personally tested out the theory that multiple .'s is what's giving me a black picture some of the time. I renamed a file to 1.1.1.1.2.jpg and uploaded it and it thumbnailed fine.
The "." thing was just an obvious problem. Try a PNG with the file extension capitialized: image.PNG

It's the regular expressions that are giving you the problem of black pictures. Every thing is processed as jpg's including png's and PNG's. However, png's are then processed again correctly but not PNG or Png or pNg or ... well you get the idea.

Yes capitialization counts. :wink: