# ImageType.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2006-2008 by Konstantin V. Arkhipov                     *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00017     final class ImageType extends Enumeration
    00018     {
    00019         const IMAGETYPE_PJPEG   = 100;
    00020
    00021         const GIF       = IMAGETYPE_GIF;
    00022         const JPEG      = IMAGETYPE_JPEG;
    00023         const PNG       = IMAGETYPE_PNG;
    00024         const SWF       = IMAGETYPE_SWF;
    00025         const PSD       = IMAGETYPE_PSD;
    00026         const BMP       = IMAGETYPE_BMP;
    00027         const TIFF_II   = IMAGETYPE_TIFF_II;
    00028         const TIFF_MM   = IMAGETYPE_TIFF_MM;
    00029         const JPC       = IMAGETYPE_JPC;
    00030         const JP2       = IMAGETYPE_JP2;
    00031         const JPX       = IMAGETYPE_JPX;
    00032         const JB2       = IMAGETYPE_JB2;
    00033         const SWC       = IMAGETYPE_SWC;
    00034         const IFF       = IMAGETYPE_IFF;
    00035         const WBMP      = IMAGETYPE_WBMP;
    00036         const JPEG2000  = IMAGETYPE_JPEG2000;
    00037         const XBM       = IMAGETYPE_XBM;
    00038         const PJPEG     = self::IMAGETYPE_PJPEG;
    00039
    00040         protected $names = array(
    00041             IMAGETYPE_GIF           => 'gif',
    00042             IMAGETYPE_JPEG          => 'jpeg',
    00043             IMAGETYPE_PNG           => 'png',
    00044             IMAGETYPE_SWF           => 'swf',
    00045             IMAGETYPE_PSD           => 'psd',
    00046             IMAGETYPE_BMP           => 'bmp',
    00047             IMAGETYPE_TIFF_II       => 'tif',
    00048             IMAGETYPE_TIFF_MM       => 'tif',
    00049             IMAGETYPE_JPC           => 'jpc',
    00050             IMAGETYPE_JP2           => 'jp2',
    00051             IMAGETYPE_JPX           => 'jpx',
    00052             IMAGETYPE_JB2           => 'jb2',
    00053             IMAGETYPE_SWC           => 'swc',
    00054             IMAGETYPE_IFF           => 'iff',
    00055             IMAGETYPE_WBMP          => 'bmp',
    00056             IMAGETYPE_JPEG2000      => 'jpc',
    00057             IMAGETYPE_XBM           => 'xbm',
    00058             self::IMAGETYPE_PJPEG   => 'jpeg'
    00059         );
    00060
    00061         protected $extensions = array(
    00062             'gif'   => IMAGETYPE_GIF,
    00063             'jpg'   => IMAGETYPE_JPEG,
    00064             'jpeg'  => IMAGETYPE_JPEG,
    00065             'pjpeg' => self::IMAGETYPE_PJPEG,
    00066             'png'   => IMAGETYPE_PNG,
    00067             'swf'   => IMAGETYPE_SWF,
    00068             'psd'   => IMAGETYPE_PSD,
    00069             'bmp'   => IMAGETYPE_BMP,
    00070             'tif'   => IMAGETYPE_TIFF_II,
    00071             'tiff'  => IMAGETYPE_TIFF_II,
    00072             'jpc'   => IMAGETYPE_JPC,
    00073             'jp2'   => IMAGETYPE_JP2,
    00074             'jpx'   => IMAGETYPE_JPX,
    00075             'jb2'   => IMAGETYPE_JB2,
    00076             'swc'   => IMAGETYPE_SWC,
    00077             'iff'   => IMAGETYPE_IFF,
    00078             'wbmp'  => IMAGETYPE_WBMP,
    00079             'jpc'   => IMAGETYPE_JPEG2000,
    00080             'xbm'   => IMAGETYPE_XBM
    00081         );
    00082
    00083         protected $mimeTypes = array(
    00084             IMAGETYPE_GIF           => 'image/gif',
    00085             IMAGETYPE_JPEG          => 'image/jpeg',
    00086             IMAGETYPE_PNG           => 'image/png',
    00087             IMAGETYPE_SWF           => 'application/x-shockwave-flash',
    00088             IMAGETYPE_PSD           => 'image/x-photoshop',
    00089             IMAGETYPE_BMP           => 'image/bmp',
    00090             IMAGETYPE_TIFF_II       => 'image/tiff',
    00091             IMAGETYPE_TIFF_MM       => 'image/tiff',
    00092             IMAGETYPE_JPC           => 'image/jpc',
    00093             IMAGETYPE_JP2           => 'image/jp2',
    00094             IMAGETYPE_JPX           => 'image/jpx',
    00095             IMAGETYPE_JB2           => 'image/jb2',
    00096             IMAGETYPE_SWC           => 'application/x-shockwave-flash',
    00097             IMAGETYPE_IFF           => 'image/iff',
    00098             IMAGETYPE_WBMP          => 'image/vnd.wap.wbmp',
    00099             IMAGETYPE_JPEG2000      => 'image/jpeg',
    00100             IMAGETYPE_XBM           => 'image/xbm',
    00101             self::IMAGETYPE_PJPEG   => 'image/pjpeg'
    00102         );
    00103
    00104         public static function getAnyId()
    00105         {
    00106             return self::GIF;
    00107         }
    00108
    00109         public function getMimeType()
    00110         {
    00111             return $this->mimeTypes[$this->id];
    00112         }
    00113
    00114         public function getExtension()
    00115         {
    00116             $flippedExensions = array_flip($this->extensions);
    00117
    00118             return $flippedExensions[$this->id];
    00119         }
    00120
    00121         public function getExtensionList()
    00122         {
    00123             return $this->extensions;
    00124         }
    00125
    00126         public static function createByFileName($fileName)
    00127         {
    00128             $ext =
    00129                 strtolower(
    00130                     pathinfo($fileName, PATHINFO_EXTENSION)
    00131                 );
    00132
    00133             $anyImageType = new self(self::getAnyId());
    00134             $extensionList = $anyImageType->getExtensionList();
    00135
    00136             if (isset($extensionList[$ext]))
    00137                 return new self($extensionList[$ext]);
    00138
    00139             throw new WrongArgumentException(
    00140                 "don't know type for '{$ext}' extension"
    00141             );
    00142         }
    00143     }
    00144 ?>