image_type_to_extension

(no version information, might be only in CVS)

image_type_to_extension --  Get file extension for image type

Description

string image_type_to_extension ( int imagetype [, bool include_dot] )

Внимание

К настоящему времени эта функция еще не была документирована; для ознакомления доступен только список аргументов.



image_type_to_extension
discerer at _REMOVETHIS_yahoo dot co dot uk
08-Jan-2006 07:23
at aleksandrs note about mime checking

i really hope that piece of code isn't used as some kind of security check for uploading (excluding those which have no value in the switch-case ie), as the mime type in uploading could be very easily changed

and btw if you want to get the extension why not just use substr to get all the text after the last dot in the filename to be really sure that the file is really what the mime says?

this could be done easily with
<?php
$filename
= "roflhax.pwnage.jpeg";
if ((
$pos = strrpos($filename, ".")) === FALSE)
  echo
"Error - file doesn't have a dot... weird.";
else {
 
$extension = substr($filename, $pos + 1);
  echo
$extension; // will echo "jpeg"
}
?>
(or you could use pathinfo() or a regex :))
aleksandrs dot bogdanovs at gmail dot com
23-Dec-2005 05:44
When I was writing a script for my photo website, it was necessary to write such function, which can get the extension of uploaded file (image), so the function is:

<?php
function get_extension($imagetype)
   {
       if(empty(
$imagetype)) return false;
       switch(
$imagetype)
       {
           case
'image/bmp': return '.bmp';
           case
'image/cis-cod': return '.cod';
           case
'image/gif': return '.gif';
           case
'image/ief': return '.ief';
           case
'image/jpeg': return '.jpg';
           case
'image/pipeg': return '.jfif';
           case
'image/tiff': return '.tif';
           case
'image/x-cmu-raster': return '.ras';
           case
'image/x-cmx': return '.cmx';
           case
'image/x-icon': return '.ico';
           case
'image/x-portable-anymap': return '.pnm';
           case
'image/x-portable-bitmap': return '.pbm';
           case
'image/x-portable-graymap': return '.pgm';
           case
'image/x-portable-pixmap': return '.ppm';
           case
'image/x-rgb': return '.rgb';
           case
'image/x-xbitmap': return '.xbm';
           case
'image/x-xpixmap': return '.xpm';
           case
'image/x-xwindowdump': return '.xwd';
           case
'image/png': return '.png';
           case
'image/x-jps': return '.jps';
           case
'image/x-freehand': return '.fh';
           default: return
false;
       }
   }
?>

It's useful for those, who upload files on server.
oridan at hotmail dot com
11-Nov-2005 10:06
i used your code in the following to open different types of files depending on their 'type' (or rather, extension).  the eval simply creates
<?
// make sure you initialize $filename as the path to the image you want to load/create

  
list(,,$type) = getimagesize($filename);
  
$type = image_type_to_extension($type);
   eval(
"global \$filename; \$source = imagecreatefrom$type(\$filename);");
   global
$source;
?>

$source is then the var you can play with.  saved me a big switch statement calling different "imagecreatefromjpg"/"imagecreatefromgif" functions just to do the one simple task.... open an image of varying types.
robe at amd dot co dot at
28-Sep-2005 12:31
neil: your version fails to make sense for me
spybreak.de guy:

a) you left out BMP
b) you fucked up the order of the extension in the mid of the listing. if this was changed in the getimagesize function during php versions, I'm sorry. Otherwise: WHY?!
c) the include_dot stuff is the worst abuse of function arguments I've ever seen. Shame on you.

Here's a unfucked, working version:

<?
if(!function_exists('image_type_to_extension'))
{
   function
image_type_to_extension($imagetype)
   {
       if(empty(
$imagetype)) return false;
       switch(
$imagetype)
       {
           case
IMAGETYPE_GIF    : return 'gif';
           case
IMAGETYPE_JPEG    : return 'jpg';
           case
IMAGETYPE_PNG    : return 'png';
           case
IMAGETYPE_SWF    : return 'swf';
           case
IMAGETYPE_PSD    : return 'psd';
           case
IMAGETYPE_BMP    : return 'bmp';
           case
IMAGETYPE_TIFF_II : return 'tiff';
           case
IMAGETYPE_TIFF_MM : return 'tiff';
           case
IMAGETYPE_JPC    : return 'jpc';
           case
IMAGETYPE_JP2    : return 'jp2';
           case
IMAGETYPE_JPX    : return 'jpf';
           case
IMAGETYPE_JB2    : return 'jb2';
           case
IMAGETYPE_SWC    : return 'swc';
           case
IMAGETYPE_IFF    : return 'aiff';
           case
IMAGETYPE_WBMP    : return 'wbmp';
           case
IMAGETYPE_XBM    : return 'xbm';
           default                : return
false;
       }
   }
}
?>
neil at g4s dot org
21-Sep-2005 10:24
refining the homebrew method a little further, i wound up with this version. note that i had to change the case statements from previous version to include call to image_type_to_mime_type()
<?php
public static function imageTypeToExtension($imageType, $includeDot = false) {
      
$dot = $includeDot ? '.' : '';
      
$ext = false;
       if(!empty(
$imageType)) {
         switch(
$imageType) {
             case
image_type_to_mime_type(IMAGETYPE_GIF)    : $ext = $dot.'gif'; break;
             case
image_type_to_mime_type(IMAGETYPE_JPEG)    : $ext = $dot.'jpg'; break;
             case
image_type_to_mime_type(IMAGETYPE_PNG)    : $ext = $dot.'png'; break;
             case
image_type_to_mime_type(IMAGETYPE_SWF)    : $ext = $dot.'swf'; break;
             case
image_type_to_mime_type(IMAGETYPE_PSD)    : $ext = $dot.'psd'; break;
             case
image_type_to_mime_type(IMAGETYPE_WBMP)    : $ext = $dot.'wbmp'; break;
             case
image_type_to_mime_type(IMAGETYPE_XBM)    : $ext = $dot.'xbm'; break;
             case
image_type_to_mime_type(IMAGETYPE_TIFF_II) : $ext = $dot.'tiff'; break;
             case
image_type_to_mime_type(IMAGETYPE_TIFF_MM) : $ext = $dot.'tiff'; break;
             case
image_type_to_mime_type(IMAGETYPE_IFF)    : $ext = $dot.'aiff'; break;
             case
image_type_to_mime_type(IMAGETYPE_JB2)    : $ext = $dot.'jb2'; break;
             case
image_type_to_mime_type(IMAGETYPE_JPC)    : $ext = $dot.'jpc'; break;
             case
image_type_to_mime_type(IMAGETYPE_JP2)    : $ext = $dot.'jp2'; break;
             case
image_type_to_mime_type(IMAGETYPE_JPX)  : $ext = $dot.'jpf'; break;
             case
image_type_to_mime_type(IMAGETYPE_SWC)    : $ext = $dot.'swc'; break;
         }
   }
   return
$ext;
  }
?>
Golf at dds dot nl
24-Apr-2005 07:23
To get the "dot" part of this funtion working, one needs to change the following line from:

$dot = $include_dot ? $dot.'' : '';
to:
$dot = $include_dot ? $dot.'.' : '';

I also recommend to change the following line from:

function image_type_to_extension($imagetype,$include_dot=true)
to:
function image_type_to_extension($imagetype,$include_dot=false)

(This so that one gets a result without the dot by default, witch is nice when one needs to compair it later on)...
19-Apr-2005 05:11
Just an improvement/bugfix for the function below:
<?php
$dot
= $include_dot ? $dot.'' : '';
//surely what was meant is....:
$dot = $include_dot ? '.' : '';
?>

Because otherwise $dot always will be empty.....
mail at spybreak dot de
14-Aug-2004 06:12
In case your PHP doesn't have this function, you can use this:
<?

if(!function_exists('image_type_to_extension'))
{
   function
image_type_to_extension($imagetype,$include_dot=true)
   {
       if(empty(
$imagetype)) return false;
      
$dot = $include_dot ? $dot.'' : '';
       switch(
$imagetype)
       {
           case
IMAGETYPE_GIF    : return $dot.'gif';
           case
IMAGETYPE_JPEG    : return $dot.'jpg';
           case
IMAGETYPE_PNG    : return $dot.'png';
           case
IMAGETYPE_SWF    : return $dot.'swf';
           case
IMAGETYPE_PSD    : return $dot.'psd';
           case
IMAGETYPE_WBMP    : return $dot.'wbmp';
           case
IMAGETYPE_XBM    : return $dot.'xbm';
           case
IMAGETYPE_TIFF_II : return $dot.'tiff';
           case
IMAGETYPE_TIFF_MM : return $dot.'tiff';
           case
IMAGETYPE_IFF    : return $dot.'aiff';
           case
IMAGETYPE_JB2    : return $dot.'jb2';
           case
IMAGETYPE_JPC    : return $dot.'jpc';
           case
IMAGETYPE_JP2    : return $dot.'jp2';
           case
IMAGETYPE_JPX    : return $dot.'jpf';
           case
IMAGETYPE_SWC    : return $dot.'swc';
           default                : return
false;
       }
   }
}
?>

<getimagesizeimage_type_to_mime_type>
 Last updated: Mon, 14 Nov 2005