note 73212 deleted from function.imagesx by nlopess
| From: | nlopess@php.net | Date: | Thu, 15 Feb 2007 15:24:37 +0000 |
| Subject: | note 73212 deleted from function.imagesx by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-123457@lists.php.net to get a copy of this message | ||
Note Submitter: richard at burninghorizons dot spam dot net
----
Hope some of you may find this useful; a few PHP functions that allow you to get the sizes of your
favourite image file types (JPEG, PNG and GIF)!
<?php
function GetSizeJPEG($filename)
{
if (!is_file($filename)) return false;
$hnd = fopen($filename, "rb");
// 255 216 <- Header of JPEG file
if (fgeti($hnd) != 65496) return false;
while (fgetb($hnd) == 255)
{
$marker = fgetb($hnd);
$length = fgeti($hnd);
if ($marker == 192)
{
fgetb($hnd);
$height = fgeti($hnd);
$width = fgeti($hnd);
fclose($hnd);
return array("width" => $width, "height" => $height);
}
fread($hnd, $length - 2);
}
fclose($hnd);
return false;
}
function GetSizePNG($filename)
{
if (!is_file($filename)) return false;
$hnd = fopen($filename, "rb");
// 137 80 78 71 13 10 26 10 <- Header of PNG file
if (fgetl($hnd) != -1991225785 ||
fgetl($hnd) != 218765834) return false;
$chunksize = fgetl($hnd);
$chunk = fread($hnd, 4);
while ($chunk != "IHDR" && !feof($hnd))
{
fread($hnd, $chunksize + 4);
$chunksize = fgetl($hnd);
$chunk = fread($hnd, 4);
}
if ($chunk == "IHDR")
{
$width = fgetl($hnd);
$height = fgetl($hnd);
fclose($hnd);
return array("width" => $width, "height" => $height);
}
else
{
fclose($hnd);
return false;
}
}
function GetSizeGIF($filename)
{
if (!is_file($filename)) return false;
$hnd = fopen($filename, "rb");
// GIF87a / GIF89a <- Header of PNG file
if (fread($hnd, 3) != "GIF") return false;
$ver = fread($hnd, 3);
if ($ver != "87a" && $ver != "89a") return false;
$width = fgetb($hnd) | fgetb($hnd) << 8;
$height = fgetb($hnd) | fgetb($hnd) << 8;
fclose($hnd);
return array("width" => $width, "height" => $height);
}
function fgetb($hnd)
{
return ord(fgetc($hnd));
}
function fgeti($hnd)
{
return fgetb($hnd) << 8 | fgetb($hnd);
}
function fgetl($hnd)
{
return fgetb($hnd) << 24 | fgetb($hnd) << 16 | fgetb($hnd) << 8 |
fgetb($hnd);
}
?>