note 40971 added to function.pdf-open-image-file

From: Date: Thu, 25 Mar 2004 02:20:13 +0000
Subject: note 40971 added to function.pdf-open-image-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-67133@lists.php.net to get a copy of this message
I don't see any functions that will tell you what type of image file you will use. So why do we need one? 1.) Image files carry extensions that are not the file format from time to time. 2.) In a parsing engine, image types are not always going to be the same. It was the latter that lend me to this niffty function: function _GetImageFormat($file) { $fp = fopen($file, "r"); $data = fread($fp, filesize($file)); fclose($fp); if(substr($data, 0, 3) == "GIF") return "gif"; if(substr($data, 0, 2) == "BM") return "bitmap"; if(substr($data, 6, 4) == "JFIF") return "jpeg"; if(substr($data, 1, 3) == "PNG") return "png"; if(substr($data, 0, 2) == "II" || substr($data, 0, 2) == "MM") return "tiff"; return "UNKNOWN IMAGE TYPE!!!"; } Then you do one of these: $file = "./images/myimage.badextension"; $type = _GetImageFormat($file); if($type == "bitmap" || $type == "UNKNOWN IMAGE TYPE!!!") { die("Bad Image Format. Format: $type"); } $img = pdf_open_image_file($pdf, $type, $file); ---- Manual Page -- http://www.php.net/manual/en/function.pdf-open-image-file.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40971 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40971&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40971&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67133) next »