note 73212 added to function.imagesx

From: Date: Tue, 13 Feb 2007 15:32:43 +0000
Subject: note 73212 added to function.imagesx
Groups: php.notes 
Request: Send a blank email to php-notes+get-123323@lists.php.net to get a copy of this message
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); } ?> ---- Server IP: 194.145.210.3 Probable Submitter: 193.34.231.229 (proxied: 10.172.129.65) ---- Manual Page -- http://www.php.net/manual/en/function.imagesx.php Edit -- https://master.php.net/note/edit/73212 Del: integrated -- https://master.php.net/note/delete/73212/integrated Del: useless -- https://master.php.net/note/delete/73212/useless Del: bad code -- https://master.php.net/note/delete/73212/bad+code Del: spam -- https://master.php.net/note/delete/73212/spam Del: non-english -- https://master.php.net/note/delete/73212/non-english Del: in docs -- https://master.php.net/note/delete/73212/in+docs Del: other reasons-- https://master.php.net/note/delete/73212 Reject -- https://master.php.net/note/reject/73212 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123323) next »