Thumbnails Creation Error

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

Thumbnails Creation Error

Post 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?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

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

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

Post by feyd »

why not specifically refer to the last element in the array?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I'm unaware of how to do that. Explain?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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

Post 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.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

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

Post 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?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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...
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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:
Post Reply