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

From: Date: Fri, 29 Sep 2006 04:10:52 +0000
Subject: note 69994 added to function.image-type-to-extension
Groups: php.notes 
Request: Send a blank email to php-notes+get-117816@lists.php.net to get a copy of this message
To: mail at spybreak dot de I noted your solution was for mime_type_to_extension which is flawed because the MIME types to extensions are not unique. See my example to consider what I have observed. This function performs image type or mime type to extension. With limitation it will not attempt to handle duplicated MIME types. NOT DEFINITIVE! <?php if(!function_exists('image_type_to_extension')){ $extension; function image_type_or_mime_type_to_extension($image_type, $include_dot) { define ("INVALID_IMAGETYPE", ''); $extension = INVALID_IMAGETYPE; /// Default return value for invalid input $image_type_identifiers = array ( ### These values correspond to the IMAGETYPE constants array (IMAGETYPE_GIF => 'gif', "mime_type" => 'image/gif'), ### 1 = GIF array (IMAGETYPE_JPEG => 'jpg', "mime_type" => 'image/jpeg'), ### 2 = JPG array (IMAGETYPE_PNG => 'png', "mime_type" => 'image/png'), ### 3 = PNG array (IMAGETYPE_SWF => 'swf', "mime_type" => 'application/x-shockwave-flash'), ### 4 = SWF // A. Duplicated MIME type array (IMAGETYPE_PSD => 'psd', "mime_type" => 'image/psd'), ### 5 = PSD array (IMAGETYPE_BMP => 'bmp', "mime_type" => 'image/bmp'), ### 6 = BMP array (IMAGETYPE_TIFF_II => 'tiff', "mime_type" => 'image/tiff'), ### 7 = TIFF (intel byte order) array (IMAGETYPE_TIFF_MM => 'tiff', "mime_type" => 'image/tiff'), ### 8 = TIFF (motorola byte order) array (IMAGETYPE_JPC => 'jpc', "mime_type" => 'application/octet-stream'), ### 9 = JPC // B. Duplicated MIME type array (IMAGETYPE_JP2 => 'jp2', "mime_type" => 'image/jp2'), ### 10 = JP2 array (IMAGETYPE_JPX => 'jpf', "mime_type" => 'application/octet-stream'), ### 11 = JPX // B. Duplicated MIME type array (IMAGETYPE_JB2 => 'jb2', "mime_type" => 'application/octet-stream'), ### 12 = JB2 // B. Duplicated MIME type array (IMAGETYPE_SWC => 'swc', "mime_type" => 'application/x-shockwave-flash'), ### 13 = SWC // A. Duplicated MIME type array (IMAGETYPE_IFF => 'aiff', "mime_type" => 'image/iff'), ### 14 = IFF array (IMAGETYPE_WBMP => 'wbmp', "mime_type" => 'image/vnd.wap.wbmp'), ### 15 = WBMP array (IMAGETYPE_XBM => 'xbm', "mime_type" => 'image/xbm') ### 16 = XBM ); if((is_int($image_type)) AND (IMAGETYPE_GIF <= $image_type) AND (IMAGETYPE_XBM >= $image_type)){ $extension = $image_type_identifiers[$image_type-1]; // -1 because $image_type_identifiers array starts at [0] $extension = $extension[$image_type]; } elseif(is_string($image_type) AND (($image_type != 'application/x-shockwave-flash') OR ($image_type != 'application/octet-stream'))){ $extension = match_mime_type_to_extension($image_type, $image_type_identifiers); } else { $extension = INVALID_IMAGETYPE; } if(is_bool($include_dot)){ if((false != $include_dot) AND (INVALID_IMAGETYPE != $extension)){ $extension = '.' . $extension; } } else { $extension = INVALID_IMAGETYPE; } return $extension; } } function match_mime_type_to_extension($image_type, $image_type_identifiers){ // Return from loop on a match foreach($image_type_identifiers as $_key_outer_loop => $_val_outer_loop){ foreach($_val_outer_loop as $_key => $_val){ if(is_int ($_key)){ // Keep record of extension for mime check $extension = $_val; } if($_key == 'mime_type'){ if($_val === $image_type){ // Found match no need to continue looping return $extension; ### Return } } } } // Compared all values without match return $extension = INVALID_IMAGETYPE; } $extension = image_type_or_mime_type_to_extension($image_type, $include_dot); return $extension; } ?> ---- Server IP: 83.138.144.80 Probable Submitter: 86.142.241.158 ---- Manual Page -- http://www.php.net/manual/en/function.image-type-to-extension.php Edit -- https://master.php.net/note/edit/69994 Del: integrated -- https://master.php.net/note/delete/69994/integrated Del: useless -- https://master.php.net/note/delete/69994/useless Del: bad code -- https://master.php.net/note/delete/69994/bad+code Del: spam -- https://master.php.net/note/delete/69994/spam Del: non-english -- https://master.php.net/note/delete/69994/non-english Del: in docs -- https://master.php.net/note/delete/69994/in+docs Del: other reasons-- https://master.php.net/note/delete/69994 Reject -- https://master.php.net/note/reject/69994 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117816) next »