note 109895 added to function.image-type-to-extension

From: Date: Fri, 31 Aug 2012 19:50:22 +0000
Subject: note 109895 added to function.image-type-to-extension
Groups: php.notes 
Request: Send a blank email to php-notes+get-190391@lists.php.net to get a copy of this message
Here is a way to get the extension of an image without GD installed by using getimagesize() http://php.net/getimagesize. Note that using the predefined constants like IMAGETYPE_JPEG only get loaded if GD is enabled; see http://php.net/image.constants. <?php $file = 'http://static.php.net/www.php.net/images/php.gif'; echo get_image_extension($file); // Returns '.gif' /** * Given an image filename, get the file extension. * * @param $filename * This parameter specifies the file you wish to retrieve information about. * It can reference a local file or (configuration permitting) a remote file * using one of the supported streams. * @param $include_dot * Whether to prepend a dot to the extension or not. Default to TRUE. * @param $shorter_extensions * Whether to use a shorter extension or not. Default to TRUE. * @return * A string with the extension corresponding to the given image filename, or * FALSE on failure. */ function get_image_extension($filename, $include_dot = true, $shorter_extensions = true) { $image_info = @getimagesize($filename); if (!$image_info || empty($image_info[2])) { return false; } if (!function_exists('image_type_to_extension')) { /** * Given an image filename, get the file extension. * * @param $imagetype * One of the IMAGETYPE_XXX constants. * @param $include_dot * Whether to prepend a dot to the extension or not. Default to TRUE. * @param $shorter_extensions * Whether to use a shorter extension or not. Default to TRUE. * @return * A string with the extension corresponding to the given image type, or * FALSE on failure. */ function image_type_to_extensiona ($imagetype, $include_dot = true) { // Note we do not use the IMAGETYPE_XXX constants as these will not be // defined if GD is not enabled. $extensions = array( 1 => 'gif', 2 => 'jpeg', 3 => 'png', 4 => 'swf', 5 => 'psd', 6 => 'bmp', 7 => 'tiff', 8 => 'tiff', 9 => 'jpc', 10 => 'jp2', 11 => 'jpf', 12 => 'jb2', 13 => 'swc', 14 => 'aiff', 15 => 'wbmp', 16 => 'xbm', ); // We are expecting an integer between 1 and 16. $imagetype = (int)$imagetype; if (!$imagetype || !isset($extensions[$imagetype])) { return false; } return ($include_dot ? '.' : '') . $extensions[$imagetype]; } } $extension = image_type_to_extension($image_info[2], $include_dot); if (!$extension) { return false; } if ($shorter_extensions) { $replacements = array( 'jpeg' => 'jpg', 'tiff' => 'tif', ); $extension = strtr($extension, $replacements); } return $extension; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 74.203.51.2 ---- Manual Page -- http://www.php.net/manual/en/function.image-type-to-extension.php Edit -- https://master.php.net/note/edit/109895 Del: integrated -- https://master.php.net/note/delete/109895/integrated Del: useless -- https://master.php.net/note/delete/109895/useless Del: bad code -- https://master.php.net/note/delete/109895/bad+code Del: spam -- https://master.php.net/note/delete/109895/spam Del: non-english -- https://master.php.net/note/delete/109895/non-english Del: in docs -- https://master.php.net/note/delete/109895/in+docs Del: other reasons-- https://master.php.net/note/delete/109895 Reject -- https://master.php.net/note/reject/109895 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190391) next »