note 35449 added to function.getimagesize
| From: | ten at rack1 dot php dot net | Date: | Mon, 01 Sep 2003 15:30:06 +0000 |
| Subject: | note 35449 added to function.getimagesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55423@lists.php.net to get a copy of this message | ||
An additional note to "tightcode_nosp@m_hotmail":
If that doesn't work try this instead:
<?
$img = imagecreatefromjpeg ($filename);
$x = imagesx ($img);
$y = imagesy ($img);
imagedestroy ($img);
?>
Though keep in mind that this consumes lots of CPU. So if you're doing something like creating
a page of thumbnails this is considerably slower.
So what you can do is use getimagesize() and check if
- the width and height are empty strings ("")
- and those two values aren't too high
Both indicate that getimagesize() didn't work properly. The latter may happen if getimagesize()
thought that it recognized the format and therefore the size properly. I mean if you're looking
at pictures that you know are max. 1024x768 and getimagesize() returns a width of e.g. 20234 then
it's obvious that something went wrong. In that case use the code mentioned above. Of course if
getimagesize() returned small values that are wrong you still get the wrong size. So check your
pictures and priorities first.
So all of this could look like as follows:
<?
$picinfo = @getimagesize ($filename);
if ($picinfo !== false) {
$x = $picinfo [0];
$y = $picinfo [1];
}
// change this according the picture resolutions you're expecting
if ($x > 2000 || $y > 2000) $x = $y = "";
if ($x == "") {
$img = imagecreatefromjpeg ($filename);
$x = imagesx ($img);
$y = imagesy ($img);
imagedestroy ($img);
}
?>
Note: fix syntax stuff if there's an error as I compiled this example from a few places.
If you don't care about the huge load on your CPU or you have to rely on the proper size use
the snippet noted at the beginning only.
----
Manual Page -- http://www.php.net/manual/en/function.getimagesize.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35449
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35449&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35449&report=yes