name = 'GD'; $this->determineTypes(); parent::__construct(); } /** * Load an image file. * Loads an image file and returns a reference to it. * * @param string $file File to load. * @param string $mime The MIME type of the file. * * @return string Reference to the file in this handler. * * @see ezcImageAnalyzer * * @throws ezcImageHandlerException * @see ezcImageHandlerException::FILE_NOT_EXISTS * @see ezcImageHandlerException::UNKNOWN_TYPE * @see ezcImageHandlerException::FILE_NOT_PROCESSABLE */ public function load( $file, $mime = null ) { $ref = $this->loadCommon( $file, isset( $mime ) ? $mime : null ); $loadFunction = $this->getLoadFunction( $this->getReferenceData( $ref, 'mime' ) ); if ( !function_exists( $loadFunction ) || ( $handle = @$loadFunction( $file ) ) === '' ) { throw new ezcImageHandlerException( "File could not be opened: <{$file}>", ezcImageHandlerException::FILE_NOT_PROCESSABLE ); } $this->setReferenceData( $ref, $handle, 'resource' ); return $ref; } /** * Save an image file. * Saves a given open file. Can optionally save to a new file name. * * @see ezcImageHandler::load() * * @param string $image File reference created through load(). * @param string $newFile Filename to save the image to. * @param string $mime New MIME type, if differs from initial one. */ public function save( $image, $newFile = null, $mime = null ) { $this->saveCommon( $image, isset( $newFile ) ? $newFile : null, isset( $mime ) ? $mime : null ); $saveFunction = $this->getSaveFunction( $this->getReferenceData( $image, 'mime' ) ); if ( !function_exists( $saveFunction ) || $saveFunction( $this->getReferenceData( $image, 'resource' ), $this->getReferenceData( $image, 'file' ) ) === false ) { throw new ezcImageHandlerException( "Unable to save file <{$file}> of type <{$mime}>.", ezcImageHandlerException::FILE_NOT_PROCESSABLE ); } } /** * Close the file referenced by $image. * Frees the image reference. You should call close() before. * * @see ezcImageHandler::load() * @see ezcImageHandler::save() * @param string $image The image reference. */ public function close( $image ) { $res = $this->getReferenceData( $image, 'resource' ); imagedestroy( $res ); $this->closeCommon( $image ); } /** * Determine, the image types the available GD extension is able to process. * */ protected function determineTypes() { $possibleTypes = array( IMG_GIF => 'image/gif', IMG_JPG => 'image/jpeg', IMG_PNG => 'image/png', IMG_WBMP => 'image/wbmp', IMG_XPM => 'image/xpm', ); $imageTypes = imagetypes(); foreach ( $possibleTypes as $bit => $mime ) { if ( $imageTypes & $bit ) { $this->inputTypes[] = $mime; $this->outputTypes[] = $mime; } } } /** * Generate imagecreatefrom* function out of a MIME type. * * @param string $mime MIME type in format "image/". * @return string imagecreatefrom* function name. */ protected function getLoadFunction( $mime ) { if ( !$this->allowsInput( $mime ) ) { throw new ezcImageHandlerException( "MIME type <{$mime}> not supported.", ezcImageHandlerException::UNKNOWN_TYPE ); } return 'imagecreatefrom' . substr( strstr( $mime, '/' ), 1 ); } /** * Generate image* function out of a MIME type. * * @param string $mime MIME type in format "image/". * @return string image* function name for saving. */ protected function getSaveFunction( $mime ) { if ( !$this->allowsOutput( $mime ) ) { throw new ezcImageHandlerException( "MIME type <{$mime}> not supported.", ezcImageHandlerException::UNKNOWN_TYPE ); } return 'image' . substr( strstr( $mime, '/' ), 1 ); } } ?>