Re: this has got me baffled: imagesx() and imagesy()reporting the wrong size?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2009-11-15 at 16:25 -0500, Ben wrote:

> Ashley Sheridan wrote:
> > On Sun, 2009-11-15 at 15:07 -0500, Ben wrote:
> >> On my site I have a web form for users to upload graphics, however there 
> >> are constraints on the size allowed.  Recently, a user has been having 
> >> problems, because the code is reporting the wrong size - a size too 
> >> small to be allowed!  They sent me a copy of the image so I could 
> >> confirm the error, and I have: even though the image is a 32x32 image 
> >> (2-frame animated GIF, if it matters), imagesx() and imagesy() report 
> >> that the image is 30x24!  I have never had this problem before (or, at 
> >> least, no one has reported it to me) so I'm tempted to think that it's a 
> >> bug in PHP, or (more likely) I am missing something crucial...
> >>
> >> The relevant code:
> >>
> >> if($itype == 'image/gif')
> >>    $img = imagecreatefromgif($_FILES['file']['tmp_name']);
> >> else if($itype == 'image/png' || $itype == 'image/x-png')
> >>    $img = imagecreatefrompng($_FILES['file']['tmp_name']);
> >>
> >> // note: we do not get here if($itype != 'image/gif' && $itype !=
> >> // 'image/png' && $itype != 'image/x-png'), so the problem should not
> >> // lie with an image type we were not expecting
> >>
> >> $w = imagesx($img);
> >> $h = imagesy($img);
> >>
> >> if(!(($w == 48 && $h == 48) || ($w >= 4 && $w <= 48 && $h == 32)))
> >> {
> >>    $error = 'The graphic\'s dimensions are not correct (' .
> >>             $w . 'x' . $h . ').';
> >> }
> >> else
> >> {
> >>    ...
> >> }
> >>
> >> yes, the logic wants either a 48x48 graphic, or a 4x32 to 48x32 graphic.
> >>
> >> I'd be happy to link the animated GIF in question... just in case there 
> >> was a problem with the GIF itself, I tried opening it up in an (old) 
> >> version of Fireworks, and re-exporting it, but the problem remains.
> >>
> >> besides the possibility of a bug in PHP, I'm wondering if I need to 
> >> handle animated GIFs differently, or something like that?  but again, 
> >> this code has been running for... 3ish years without a problem like this 
> >> ever being reported... quite confusing.
> >>
> > 
> > If you could link the gif I think that would help.
> > 
> > Also, just to point out, the comment in the script is wrong, if the
> > image is neither png or gif, the rest of the script will still be
> > parsed. The if logic is like this:
> > 
> > if($itype == 'image/gif')
> >     $img = imagecreatefromgif($_FILES['file']['tmp_name']);
> > else
> >     if($itype == 'image/png' || $itype == 'image/x-png')
> >         $img = imagecreatefrompng($_FILES['file']['tmp_name']);
> > 
> > 
> > 
> > everything after that is executed
> > 
> > perhaps you wanted something like this:
> > 
> > if($itype == 'image' || $itype == 'image/png' || $itype ==
> > 'image/x-png')
> > {
> >     if($itype == 'image/gif')
> >         $img = imagecreatefromgif($_FILES['file']['tmp_name']);
> >     if($itype == 'image/png' || $itype == 'image/x-png')
> >         $img = imagecreatefrompng($_FILES['file']['tmp_name']);
> > 
> >     // execute the rest of the image checking script
> > }
> > 
> > 
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> > 
> > 
> > 
> 
> what I meant by the comment, is that that previous code DOES exist... to 
> assure you that the problem isn't in having $img unset, or something 
> along those lines.  (I added in the comment after the fact.)
> 
> I have uploaded the image causing the problem here: 
> www.telkoth.net/temp/radioactive-bread-eek.gif
> 
> I should also add: I have ended up working around the problem by using 
> getimagesize() instead; that function is reporting the correct width and 
> height.  this furthers my belief that there's a bug somewhere with 
> imagesx(), imagesy() or imagecreatefromgif()...
> 


It does indeed seem to be some sort of bug. I've just tested it with
your image on my machine here and for imagesx() and imagesy() it gives
30 and 24 respectively. getimagesize() does return the correct
dimensions though.

The same image saved as a flat single layer gif from the Gimp behaves
exactly the same way.

This might not be a proper bug as such, as the image itself might be
32x32, but the first layer (the first frame in the animated version) is
30x24 when you remove the dead, unused, transparent background. Perhaps
GD is meant to report on the *actual* size of the image, rather than the
dimensions of the frame?

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux