note 35449 deleted from function.getimagesize by cmb
| From: | cmb@php.net | Date: | Sun, 18 Dec 2016 09:43:25 +0000 |
| Subject: | note 35449 deleted from function.getimagesize by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208637@lists.php.net to get a copy of this message | ||
Note Submitter: ten tod xmg ta rotanimrev (reverse it)
----
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.