Apache Zeta Components Manual :: File Source for gd_base.php
Source for file gd_base.php
Documentation is available at gd_base.php
* This file contains the ezcImageGdBaseHandler class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @package ImageConversion
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* ezcImageHandler implementation for the GD2 extension of PHP.
* This class only implements the base funtionality of handling GD images. If
* you want to manipulate images using ext/GD in your application, you should
* use the {@link ezcImageGdHandler}.
* You can use this base class to implement your own filter set on basis of
* ext/GD, but you can also use {@link ezcImageGdHandler} for this and profit
* from its already implemented filters.
* @package ImageConversion
* @version //autogentag//
* Create a new image handler.
* Creates an image handler. This should never be done directly,
* but only through the manager for configuration reasons. One can
* get a direct reference through manager afterwards.
* @param ezcImageHandlerSettings $settings
* Settings for the handler.
* @throws ezcImageHandlerNotAvailableException
* If the precondition for the handler is not fulfilled.
public function __construct( ezcImageHandlerSettings $settings )
parent::__construct( $settings );
* 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.
* @throws ezcBaseFileNotFoundException
* If the given file does not exist.
* @throws ezcImageMimeTypeUnsupportedException
* If the type of the given file is not recognized
* @throws ezcImageFileNotProcessableException
* If the given file is not processable using this handler.
* @throws ezcImageFileNameInvalidException
* If an invalid character (", ', $) is found in the file name.
public function load( $file, $mime =
null )
$this->checkFileName( $file );
$ref =
$this->loadCommon( $file, isset
( $mime ) ?
$mime :
null );
$loadFunction =
$this->getLoadFunction( $this->getReferenceData( $ref, 'mime' ) );
$this->setReferenceData( $ref, $handle, 'resource' );
* 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.
* @param ezcImageSaveOptions $options Save options.
* @throws ezcImageFileNotProcessableException
* If the given file could not be saved with the given MIME type.
* @throws ezcBaseFilePermissionException
* If the desired file exists and is not writeable.
* @throws ezcImageMimeTypeUnsupportedException
* If the desired MIME type is not recognized
* @throws ezcImageFileNameInvalidException
* If an invalid character (", ', $) is found in the file name.
public function save( $image, $newFile =
null, $mime =
null, ezcImageSaveOptions $options =
null )
$this->checkFileName( $newFile );
// Check is transparency must be converted
if ( $this->needsTransparencyConversion( $this->getReferenceData( $image, 'mime' ), $mime ) &&
$options->transparencyReplacementColor !==
null )
$this->saveCommon( $image, isset
( $newFile ) ?
$newFile :
null, isset
( $mime ) ?
$mime :
null );
$saveFunction =
$this->getSaveFunction( $this->getReferenceData( $image, 'mime' ) );
$this->getReferenceData( $image, 'resource' ),
$this->getReferenceData( $image, 'file' ),
if ( $options->quality !==
null )
$saveParams[] =
$options->quality;
if ( $options->compression !==
null )
$saveParams[] =
$options->compression;
* Replaces a transparent background with the given color.
* This method is used to replace the transparent background of an image
* with an opaque color when converting from a transparency supporting MIME
* type (e.g. image/png) to a MIME type that does not support transparency.
$oldResource =
$this->getReferenceData( $image, 'resource' );
$width =
imagesx( $oldResource );
$height =
imagesy( $oldResource );
if ( imageistruecolor( $oldResource ) )
$newResource =
imagecreatetruecolor( $width, $height );
// $res = imagecopyresampled(
$newResource, // destination resource
$oldResource, // source resource
0, // destination x coord
0, // destination y coord
$width, // destination width
$height, // destination height
$this->setReferenceData( $image, $newResource, 'resource' );
* 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' );
$this->closeCommon( $image );
* Determine, the image types the available GD extension is able to process.
private function determineTypes()
IMG_WBMP =>
'image/wbmp',
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/<type>".
* @return string imagecreatefrom* function name.
* @throws ezcImageMimeTypeUnsupportedException
* If the load function for a given MIME type does not exist.
private function getLoadFunction( $mime )
if ( !$this->allowsInput( $mime ) )
return 'imagecreatefrom' .
substr( strstr( $mime, '/' ), 1 );
* Generate image* function out of a MIME type.
* @param string $mime MIME type in format "image/<type>".
* @return string image* function name for saving.
* @throws ezcImageImagemagickHandler
* If the save function for a given MIME type does not exist.
private function getSaveFunction( $mime )
if ( !$this->allowsOutput( $mime ) )
* Creates default settings for the handler and returns it.
* The reference name will be set to 'GD'.
* @return ezcImageHandlerSettings