Apache Zeta Components Manual :: File Source for imagemagick.php
Source for file imagemagick.php
Documentation is available at imagemagick.php
* This file contains the ezcImageImagemagickHandler 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 ImageMagick.
* @package ImageConversion
* @version //autogentag//
* General scale filter. Scales the image to fit into a given box size,
* determined by a given width and height value, measured in pixel. This
* method maintains the aspect ratio of the given image. Depending on the
* given direction value, this method performs the following scales:
* - ezcImageGeometryFilters::SCALE_BOTH:
* The image will be scaled to fit exactly into the given box
* dimensions, no matter if it was smaller or larger as the box
* - ezcImageGeometryFilters::SCALE_DOWN:
* The image will be scaled to fit exactly into the given box
* only if it was larger than the given box dimensions before. If it
* is smaller, the image will not be scaled at all.
* - ezcImageGeometryFilters::SCALE_UP:
* The image will be scaled to fit exactly into the given box
* only if it was smaller than the given box dimensions before. If it
* is larger, the image will not be scaled at all. ATTENTION:
* In this case, the image does not necessarily fit into the given box
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $width Scale to width
* @param int $height Scale to height
* @param int $direction Scale to which direction.
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference.
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
public function scale( $width, $height, $direction =
ezcImageGeometryFilters::SCALE_BOTH )
if ( !is_int( $width ) ||
$width <
1 )
if ( !is_int( $height ) ||
$height <
1 )
$this->getActiveReference(),
$width.
$dirMod.
'x'.
$height.
$dirMod
* Scale after width filter.
* Scales the image to a give width, measured in pixel. Scales the height
* automatically while keeping the ratio. The direction dictates, if an
* image may only be scaled {@link self::SCALE_UP}, {@link self::SCALE_DOWN}
* or if the scale may work in {@link self::SCALE_BOTH} directions.
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $width Scale to width
* @param int $direction Scale to which direction
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
if ( !is_int( $width ) ||
$width <
1 )
$this->getActiveReference(),
* Scale after height filter.
* Scales the image to a give height, measured in pixel. Scales the width
* automatically while keeping the ratio. The direction dictates, if an
* image may only be scaled {@link self::SCALE_UP}, {@link self::SCALE_DOWN}
* or if the scale may work in {@link self::SCALE_BOTH} directions.
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $height Scale to height
* @param int $direction Scale to which direction
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
if ( !is_int( $height ) ||
$height <
1 )
$this->getActiveReference(),
* Scale percent measures filter.
* Scale an image to a given percentage value size.
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $width Scale to width
* @param int $height Scale to height
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
if ( !is_int( $height ) ||
$height <
1 )
if ( !is_int( $width ) ||
$width <
1 )
$this->getActiveReference(),
* Scale the image to a fixed given pixel size, no matter to which
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $width Scale to width
* @param int $height Scale to height
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference.
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
if ( !is_int( $height ) ||
$height <
1 )
if ( !is_int( $width ) ||
$width <
1 )
$this->getActiveReference(),
* Crop an image to a given size. This takes cartesian coordinates of a
* rect area to crop from the image. The cropped area will replace the old
* image resource (not the input image immediately, if you use the
* {@link ezcImageConverter}). Coordinates are given as integer values and
* are measured from the top left corner.
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $x X offset of the cropping area.
* @param int $y Y offset of the cropping area.
* @param int $width Width of cropping area.
* @param int $height Height of cropping area.
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference.
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
public function crop( $x, $y, $width, $height )
$x =
( $x >=
0 ) ?
$x :
$data[0] +
$x;
$y =
( $y >=
0 ) ?
$y :
$data[1] +
$y;
$xStart =
( $xStart =
min( $x, $x +
$width ) ) >=
0 ?
'+'.
$xStart :
$xStart;
$yStart =
( $yStart =
min( $y, $y +
$height ) ) >=
0 ?
'+'.
$yStart :
$yStart;
$this->getActiveReference(),
abs( $width ).
'x'.
abs( $height ).
$xStart.
$yStart.
'!'
* Transform the color space of the picture. The following color space are
* - {@link self::COLORSPACE_GREY} - 255 grey colors
* - {@link self::COLORSPACE_SEPIA} - Sepia colors
* - {@link self::COLORSPACE_MONOCHROME} - 2 colors black and white
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $space Colorspace, one of self::COLORSPACE_* constants.
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference
* @throws ezcBaseValueException
* If the parameter submitted as the colorspace was not within the
* self::COLORSPACE_* constants.
case self::COLORSPACE_GREY:
$this->getActiveReference(),
$this->getActiveReference(),
case self::COLORSPACE_MONOCHROME:
$this->getActiveReference(),
case self::COLORSPACE_SEPIA:
$this->getActiveReference(),
throw
new ezcBaseValueException( 'space', $space, 'self::COLORSPACE_GREY, self::COLORSPACE_SEPIA, self::COLORSPACE_MONOCHROME' );
* Apply a noise transformation to the image. Valid values are the following
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param strings $value Noise value as described above.
* @throws ezcBaseValueException
* If the noise value is out of range.
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference.
public function noise( $value )
if ( !in_array( $value, $possibleValues ) )
throw
new ezcBaseValueException( 'value', $value, 'Uniform, Gaussian, Multiplicative, Impulse, Laplacian, Poisson' );
$this->getActiveReference(),
* Applies a swirl with the given intense to the image.
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $value Intense of swirl.
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference.
* @throws ezcBaseValueException
* If the swirl value is out of range.
public function swirl( $value )
if ( !is_int( $value ) ||
$value <
0 )
$this->getActiveReference(),
* Adds a border to the image. The width is measured in pixel. The color is
* defined in an array of hex values:
* ATTENTION: Using this filter method directly results in the filter being
* applied to the image which is internally marked as "active" (most
* commonly this is the last recently loaded one). It is highly recommended
* to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
* method, which enables you to specify the image a filter is applied to.
* @param int $width Width of the border.
* @param array(int) $color Color.
* @throws ezcImageInvalidReferenceException
* No loaded file could be found or an error destroyed a loaded reference.
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
public function border( $width, array $color )
$this->getActiveReference(),
$this->getActiveReference(),
* Places a watermark on the image. The file to use as the watermark image
* is given as $image. The $posX, $posY and $size values are given in
* percent, related to the destination image. A $size value of 10 will make
* the watermark appear in 10% of the destination image size.
* $posX = $posY = 10 will make the watermark appear in the top left corner
* of the destination image, 10% of its size away from its borders. If
* $size is ommitted, the watermark image will not be resized.
* @param string $image The image file to use as the watermark
* @param int $posX X position in the destination image in percent.
* @param int $posY Y position in the destination image in percent.
* @param int|bool$size Percentage size of the watermark, false for none.
* @throws ezcImageInvalidReferenceException
* If no valid resource for the active reference could be found.
* @throws ezcImageFilterFailedException
* If the operation performed by the the filter failed.
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
if ( !is_int( $posX ) ||
$posX <
0 ||
$posX >
100 )
if ( !is_int( $posY ) ||
$posY <
0 ||
$posY >
100 )
if ( !is_bool( $size ) &&
( !is_int( $size ) ||
$size <
0 ||
$size >
100 ) )
$data =
getimagesize( $this->getReferenceData( $this->getActiveReference(), "resource" ) );
$originalWidth =
$data[0];
$originalHeight =
$data[1];
$watermarkHeight =
false;
$watermarkWidth = (int)
round( $originalWidth *
$size /
100 );
$watermarkHeight = (int)
round( $originalHeight *
$size /
100 );
$watermarkPosX = (int)
round( $originalWidth *
$posX /
100 );
$watermarkPosY = (int)
round( $originalHeight *
$posY /
100 );
$this->watermarkAbsolute( $image, $watermarkPosX, $watermarkPosY, $watermarkWidth, $watermarkHeight );
* Places a watermark on the image. The file to use as the watermark image
* is given as $image. The $posX, $posY and $size values are given in
* pixel. The watermark appear at $posX, $posY in the destination image
* with a size of $size pixel. If $size is ommitted, the watermark image
* @param string $image The image file to use as the watermark
* @param int $posX X position in the destination image in pixel.
* @param int $posY Y position in the destination image in pixel.
* @param int|bool$width Pixel size of the watermark, false to keep size.
* @param int|bool$height Pixel size of the watermark, false to keep size.
* @throws ezcImageInvalidReferenceException
* If no valid resource for the active reference could be found.
* @throws ezcImageFilterFailedException
* If the operation performed by the the filter failed.
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
public function watermarkAbsolute( $image, $posX, $posY, $width =
false, $height =
false )
$posX =
( $posX >=
0 ) ?
$posX :
$data[0] +
$posX;
$posY =
( $posY >=
0 ) ?
$posY :
$data[1] +
$posY;
$this->getActiveReference(),
$this->getActiveReference(),
( $width !==
false ?
$width :
"" ) .
( $height !==
false ?
"x$height" :
"" ) .
"+$posX+$posY"
* Creates a thumbnail, and crops parts of the given image to fit the range best.
* This filter creates a thumbnail of the given image. The image is scaled
* down, keeping the original ratio and keeping the image larger as the
* given range, if necessary. Overhead for the target range is cropped from
* If you are looking for a filter that just resizes your image to
* thumbnail size, you should consider the {@link }
* ezcImageImagemagickHandler::scale()} filter.
* @param int $width Width of the thumbnail.
* @param int $height Height of the thumbnail.
if ( !is_int( $width ) ||
$width <
1 )
if ( !is_int( $height ) ||
$height <
1 )
$data =
getimagesize( $this->getReferenceData( $this->getActiveReference(), "resource" ) );
$scaleRatio =
max( $width /
$data[0], $height /
$data[1] );
$scaleWidth =
round( $data[0] *
$scaleRatio );
$scaleHeight =
round( $data[1] *
$scaleRatio );
$cropOffsetX =
( $scaleWidth >
$width ) ?
"+" .
round( ( $scaleWidth -
$width ) /
2 ) :
"+0";
$cropOffsetY =
( $scaleHeight >
$height ) ?
"+" .
round( ( $scaleHeight -
$height ) /
2 ) :
"+0";
$this->getActiveReference(),
$scaleWidth .
"x" .
$scaleHeight
$this->getActiveReference(),
$width .
"x" .
$height .
$cropOffsetX .
$cropOffsetY .
"!"
* Creates a thumbnail, and fills up the image to fit the given range.
* This filter creates a thumbnail of the given image. The image is scaled
* down, keeping the original ratio and scaling the image smaller as the
* given range, if necessary. Overhead for the target range is filled with the given
* color on both sides equally.
* The color is defined by the following array format (integer values 0-255):
* If you are looking for a filter that just resizes your image to
* thumbnail size, you should consider the {@link }
* ezcImageImagemagickHandler::scale()} filter.
* @param int $width Width of the thumbnail.
* @param int $height Height of the thumbnail.
* @param array $color Fill color.
if ( !is_int( $width ) ||
$width <
1 )
if ( !is_int( $height ) ||
$height <
1 )
$data =
getimagesize( $this->getReferenceData( $this->getActiveReference(), "resource" ) );
$scaleRatio =
min( $width /
$data[0], $height /
$data[1] );
$scaleWidth =
round( $data[0] *
$scaleRatio );
$scaleHeight =
round( $data[1] *
$scaleRatio );
$cropOffsetX =
( $scaleWidth <
$width ) ?
"-" .
round( ( $width -
$scaleWidth ) /
2 ) :
"+0";
$cropOffsetY =
( $scaleHeight <
$height ) ?
"-" .
round( ( $height -
$scaleHeight ) /
2 ) :
"+0";
foreach ( $color as $id =>
$colorVal )
if ( !is_int( $colorVal ) ||
$colorVal <
0 ||
$colorVal >
255 )
$colorString .=
sprintf( '%02x', $colorVal );
$this->getActiveReference(),
$this->getActiveReference(),
$width .
"x" .
$height .
$cropOffsetX .
$cropOffsetY .
"!"
$this->getActiveReference(),
$this->getActiveReference(),
* Returns the ImageMagick direction modifier for a direction constant.
* ImageMagick supports the following modifiers to determine if an
* image should be scaled up only, down only or in both directions:
* This method returns the correct modifier for the internal direction
* @param int $direction One of ezcImageGeometryFilters::SCALE_*
* @return string The correct modifier.
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
throw
new ezcBaseValueException( 'direction', $direction, 'self::SCALE_BOTH, self::SCALE_UP, self::SCALE_DOWN' );