Version 4.0.13.1
This commit is contained in:
1 parent
f9e857d255
commit
c6a86f0d9b
7517 files changed
+597400
-668041
No files matched your search
@@ -0,0 +1,313 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Image Class - GD
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 19 Feb 2013
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Image;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Image Class - GD
|
||||
*/
|
||||
class _GD extends \IPS\Image
|
||||
{
|
||||
/**
|
||||
* @brief Image Resource
|
||||
*/
|
||||
public $image;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $contents Contents
|
||||
* @return void
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct( $contents )
|
||||
{
|
||||
/* Create the resource */
|
||||
$this->image = @imagecreatefromstring( $contents );
|
||||
if ( $this->image === FALSE )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
}
|
||||
|
||||
/* Set width/height */
|
||||
$this->width = imagesx( $this->image );
|
||||
$this->height = imagesy( $this->image );
|
||||
|
||||
/* Try to maintain any transparency */
|
||||
imagealphablending( $this->image, true );
|
||||
imagesavealpha( $this->image, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if( is_resource( $this->image ) )
|
||||
{
|
||||
imagedestroy( $this->image );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Contents
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
ob_start();
|
||||
switch( $this->type )
|
||||
{
|
||||
case 'gif':
|
||||
if ( $this->isAnimatedGif )
|
||||
{
|
||||
return $this->contents;
|
||||
}
|
||||
imagegif( $this->image );
|
||||
break;
|
||||
|
||||
case 'jpeg':
|
||||
$quality = \IPS\Settings::i()->image_jpg_quality ?: NULL;
|
||||
|
||||
imagejpeg( $this->image, NULL, $quality );
|
||||
break;
|
||||
|
||||
case 'png':
|
||||
$quality = \IPS\Settings::i()->image_png_quality_gd ?: NULL;
|
||||
|
||||
imagepng( $this->image, NULL, $quality );
|
||||
break;
|
||||
}
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize
|
||||
*
|
||||
* @param int $width Width (in pixels)
|
||||
* @param int $height Height (in pixels)
|
||||
* @return void
|
||||
*/
|
||||
public function resize( $width, $height )
|
||||
{
|
||||
return $this->_manipulate( $width, $height, FALSE );
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop to a given width and height (will attempt to downsize first)
|
||||
*
|
||||
* @param int $width Width (in pixels)
|
||||
* @param int $height Height (in pixels)
|
||||
* @return void
|
||||
*/
|
||||
public function crop( $width, $height )
|
||||
{
|
||||
return $this->_manipulate( $width, $height, TRUE );
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize and/or crop image
|
||||
*
|
||||
* @param int $width Width (in pixels)
|
||||
* @param int $height Height (in pixels)
|
||||
* @param bool $crop Crop image to provided dimensions
|
||||
* @return void
|
||||
*/
|
||||
public function _manipulate( $width, $height, $crop=FALSE )
|
||||
{
|
||||
if ( $this->isAnimatedGif )
|
||||
{
|
||||
return $this->image;
|
||||
}
|
||||
|
||||
/* Create a new canvas */
|
||||
$newImage = imagecreatetruecolor( $width, $height );
|
||||
switch( $this->type )
|
||||
{
|
||||
case 'gif':
|
||||
imagealphablending( $newImage, FALSE );
|
||||
$transindex = imagecolortransparent( $this->image );
|
||||
if( $transindex >= 0 )
|
||||
{
|
||||
$transcol = @imagecolorsforindex( $this->image, $transindex );
|
||||
$transindex = imagecolorallocatealpha( $newImage, $transcol['red'], $transcol['green'], $transcol['blue'], 127 );
|
||||
imagefill( $newImage, 0, 0, $transindex );
|
||||
}
|
||||
break;
|
||||
|
||||
case 'jpeg':
|
||||
imagealphablending( $newImage, TRUE );
|
||||
break;
|
||||
|
||||
case 'png':
|
||||
imagealphablending( $newImage, FALSE );
|
||||
imagesavealpha( $newImage, TRUE );
|
||||
break;
|
||||
}
|
||||
|
||||
/* Crop the image? */
|
||||
if( $crop === TRUE )
|
||||
{
|
||||
/* First, downsize the image */
|
||||
$ratio = ( $this->width / $this->height );
|
||||
|
||||
if ( $width / $height > $ratio )
|
||||
{
|
||||
$nheight = $width / $ratio;
|
||||
$nwidth = $width;
|
||||
}
|
||||
else
|
||||
{
|
||||
$nwidth = $height * $ratio;
|
||||
$nheight = $height;
|
||||
}
|
||||
|
||||
$this->resizeToMax( $nwidth, $nheight );
|
||||
|
||||
/* Then we use imagecopy which will crop */
|
||||
imagecopy( $newImage, $this->image, 0, 0, 0, 0, $this->width, $this->height );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Copy the image resampled */
|
||||
imagecopyresampled( $newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height );
|
||||
}
|
||||
|
||||
/* Replace */
|
||||
imagedestroy( $this->image );
|
||||
$this->image = $newImage;
|
||||
|
||||
/* Set width/height */
|
||||
$this->width = imagesx( $this->image );
|
||||
$this->height = imagesy( $this->image );
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop at specific points
|
||||
*
|
||||
* @param int $point1X x-point for top-left corner
|
||||
* @param int $point1Y y-point for top-left corner
|
||||
* @param int $point2X x-point for bottom-right corner
|
||||
* @param int $point2Y y-point for bottom-right corner
|
||||
* @return void
|
||||
*/
|
||||
public function cropToPoints( $point1X, $point1Y, $point2X, $point2Y )
|
||||
{
|
||||
/* Create a new canvas */
|
||||
$newImage = imagecreatetruecolor( ( $point2X - $point1X > 0 ) ? $point2X - $point1X : 0, ( $point2Y - $point1Y > 0 ) ? $point2Y - $point1Y : 0 );
|
||||
switch( $this->type )
|
||||
{
|
||||
case 'gif':
|
||||
imagealphablending( $newImage, FALSE );
|
||||
$transindex = imagecolortransparent( $this->image );
|
||||
if( $transindex >= 0 )
|
||||
{
|
||||
$transcol = @imagecolorsforindex( $this->image, $transindex );
|
||||
$transindex = imagecolorallocatealpha( $newImage, $transcol['red'], $transcol['green'], $transcol['blue'], 127 );
|
||||
imagefill( $newImage, 0, 0, $transindex );
|
||||
}
|
||||
break;
|
||||
|
||||
case 'jpeg':
|
||||
imagealphablending( $newImage, TRUE );
|
||||
break;
|
||||
|
||||
case 'png':
|
||||
imagealphablending( $newImage, FALSE );
|
||||
imagesavealpha( $newImage, TRUE );
|
||||
break;
|
||||
}
|
||||
|
||||
/* Then we use imagecopy which will crop */
|
||||
imagecopy( $newImage, $this->image, 0, 0, $point1X, $point1Y, $point2X - $point1X, $point2Y - $point1Y );
|
||||
|
||||
/* Replace */
|
||||
imagedestroy( $this->image );
|
||||
$this->image = $newImage;
|
||||
|
||||
/* Set width/height */
|
||||
$this->width = imagesx( $this->image );
|
||||
$this->height = imagesy( $this->image );
|
||||
}
|
||||
|
||||
/**
|
||||
* Impose image
|
||||
*
|
||||
* @param \IPS\Image $image Image to impose
|
||||
* @param int $x Location to impose to, x axis
|
||||
* @param int y Location to impose to, y axis
|
||||
* @return void
|
||||
*/
|
||||
public function impose( $image, $x=0, $y=0 )
|
||||
{
|
||||
imagecopy( $this->image, $image->image, $x, $y, 0, 0, $image->width, $image->height );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate image
|
||||
*
|
||||
* @param int $angle Angle of rotation
|
||||
* @return void
|
||||
*/
|
||||
public function rotate( $angle )
|
||||
{
|
||||
$this->image = imagerotate( $this->image, $angle, 0 );
|
||||
|
||||
/* Set width/height */
|
||||
$this->width = imagesx( $this->image );
|
||||
$this->height = imagesy( $this->image );
|
||||
|
||||
/* Try to maintain any transparency */
|
||||
imagealphablending( $this->image, true );
|
||||
imagesavealpha( $this->image, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Image Orientation
|
||||
*
|
||||
* @return int|NULL
|
||||
*/
|
||||
public function getImageOrientation()
|
||||
{
|
||||
if ( static::exifSupported() )
|
||||
{
|
||||
$exif = $this->parseExif();
|
||||
|
||||
if ( isset( $exif['IFD0.Orientation'] ) )
|
||||
{
|
||||
return $exif['IFD0.Orientation'];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Image Orientation
|
||||
*
|
||||
* @param int $orientation The orientation
|
||||
* @return void
|
||||
*/
|
||||
public function setImageOrientation( $orientation )
|
||||
{
|
||||
/* Note, GD does not require orientation to be set after rotation */
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,369 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Image Class
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 19 Feb 2013
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Image Class
|
||||
*/
|
||||
abstract class _Image
|
||||
{
|
||||
/**
|
||||
* @brief Image Extensions
|
||||
*/
|
||||
public static $imageExtensions = array( 'gif', 'jpeg', 'jpe', 'jpg', 'png' );
|
||||
|
||||
/**
|
||||
* Determine if EXIF extraction is supported
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function exifSupported()
|
||||
{
|
||||
return function_exists( 'exif_read_data' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Object
|
||||
*
|
||||
* @param string $contents Contents
|
||||
* @return \IPS\Image
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function create( $contents )
|
||||
{
|
||||
/* Create object */
|
||||
if( \IPS\Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
|
||||
{
|
||||
$obj = new Image\Imagemagick( $contents );
|
||||
}
|
||||
else
|
||||
{
|
||||
$obj = new Image\Gd( $contents );
|
||||
}
|
||||
|
||||
/* Work out the type */
|
||||
preg_match( '/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(?:\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/', $contents, $matches );
|
||||
switch ( count( $matches ) )
|
||||
{
|
||||
case 2:
|
||||
$obj->type = 'jpeg';
|
||||
break;
|
||||
case 3:
|
||||
$obj->type = 'gif';
|
||||
if ( (boolean) preg_match( '#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $contents ) )
|
||||
{
|
||||
$obj->isAnimatedGif = TRUE;
|
||||
$obj->contents = $contents;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
$obj->type = 'png';
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException;
|
||||
}
|
||||
|
||||
/* Set EXIF data immediately */
|
||||
if( static::exifSupported() )
|
||||
{
|
||||
$obj->setExifData( $contents );
|
||||
|
||||
/* If the image is misoriented, attempt to automatically reorient */
|
||||
if( \IPS\Image::exifSupported() )
|
||||
{
|
||||
/* Differences in orientation between GD and ImageMagick can cause auto-reorient to not work properly */
|
||||
/* @see http://community.invisionpower.com/4bugtrack/rc1-imagemagick-and-vertical-photos-r2952 */
|
||||
$orientation = $obj->getImageOrientation();
|
||||
|
||||
if ( !( $obj instanceof Image\Imagemagick ) )
|
||||
{
|
||||
switch ( $orientation )
|
||||
{
|
||||
case 3:
|
||||
$obj->rotate( 180 );
|
||||
break;
|
||||
|
||||
case 6:
|
||||
$obj->rotate( -90 );
|
||||
break;
|
||||
|
||||
case 8:
|
||||
$obj->rotate( 90 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch( $orientation )
|
||||
{
|
||||
case 3:
|
||||
$obj->rotate( 180 );
|
||||
break;
|
||||
|
||||
case 6:
|
||||
$obj->rotate( 90 );
|
||||
break;
|
||||
|
||||
case 8:
|
||||
$obj->rotate( -90 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* ImageMagick requires orientation be reset when rotated */
|
||||
$obj->setImageOrientation( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Type ('png', 'jpeg' or 'gif')
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @brief Width
|
||||
*/
|
||||
public $width;
|
||||
|
||||
/**
|
||||
* @brief Height
|
||||
*/
|
||||
public $height;
|
||||
|
||||
/**
|
||||
* @brief Is this an animated gif?
|
||||
*/
|
||||
protected $isAnimatedGif = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Contents of the image file when animated gif
|
||||
*/
|
||||
public $contents = NULL;
|
||||
|
||||
/**
|
||||
* @brief EXIF data - has to be pulled and stored before GD manipulates image
|
||||
*/
|
||||
protected $exif = array();
|
||||
|
||||
/**
|
||||
* Resize to maximum
|
||||
*
|
||||
* @param int|NULL $maxWidth Max Width (in pixels) or NULL
|
||||
* @param int|NULL $maxHeight Max Height (in pixels) or NULL
|
||||
* @param bool $retainRatio If TRUE, the image will keep it's current width/height ratio rather than being squashed
|
||||
* @return bool
|
||||
*/
|
||||
public function resizeToMax( $maxWidth=NULL, $maxHeight=NULL, $retainRatio=TRUE )
|
||||
{
|
||||
/* If the image is smaller than max we can skip */
|
||||
if( ( $maxWidth == NULL or $this->width < $maxWidth ) and ( $maxHeight == NULL or $this->height < $maxHeight ) )
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Work out the maximum width/height */
|
||||
$width = ( $maxWidth !== NULL and $this->width > $maxWidth ) ? $maxWidth : $this->width;
|
||||
$height = ( $maxHeight !== NULL and $this->height > $maxHeight ) ? $maxHeight : $this->height;
|
||||
|
||||
if ( $width != $this->width or $height != $this->height )
|
||||
{
|
||||
/* Adjust the width/height as necessary if we want to keep the ratio */
|
||||
if ( $retainRatio === TRUE )
|
||||
{
|
||||
if ( ( $this->height - $height ) <= ( $this->width - $width ) )
|
||||
{
|
||||
$ratio = $this->height / $this->width;
|
||||
$height = $width * $ratio;
|
||||
}
|
||||
else
|
||||
{
|
||||
$ratio = $this->width / $this->height;
|
||||
$width = $height * $ratio;
|
||||
}
|
||||
}
|
||||
|
||||
/* And resize */
|
||||
return $this->resize( $width, $height );
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Watermark
|
||||
*
|
||||
* @param \IPS\Image $watermark The watermark
|
||||
* @return void
|
||||
*/
|
||||
public function watermark( \IPS\Image $watermark )
|
||||
{
|
||||
/* If it's too big, resize the watermark */
|
||||
$watermark->resizeToMax( $this->width, $this->height );
|
||||
|
||||
/* Impose */
|
||||
$this->impose( $watermark, $this->width - $watermark->width, $this->height - $watermark->height );
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse file object to extract EXIF data
|
||||
*
|
||||
* @return array
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public function parseExif()
|
||||
{
|
||||
if( !static::exifSupported() )
|
||||
{
|
||||
throw new \LogicException( 'NO_EXIF' );
|
||||
}
|
||||
|
||||
if( !in_array( $this->type, array( 'jpeg', 'jpg', 'jpe' ) ) )
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
$result = array();
|
||||
|
||||
/* Read the data and store in an array */
|
||||
if( $values = $this->getExifData() )
|
||||
{
|
||||
foreach( $values as $section => $data )
|
||||
{
|
||||
foreach( $data as $name => $value )
|
||||
{
|
||||
$result[ $section . '.' . $name ] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the EXIF data */
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get EXIF data, if possible
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getExifData()
|
||||
{
|
||||
return $this->exif;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get EXIF data, if possible
|
||||
*
|
||||
* @param string $image Image contents
|
||||
* @return void
|
||||
*/
|
||||
public function setExifData( $contents )
|
||||
{
|
||||
if( !in_array( $this->type, array( 'jpeg', 'jpg', 'jpe' ) ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* Exif requires a file on disk, so write it temporarily */
|
||||
$temporary = tempnam( \IPS\TEMP_DIRECTORY, 'exif' );
|
||||
\file_put_contents( $temporary, $contents );
|
||||
|
||||
$result = @exif_read_data( $temporary, NULL, TRUE );
|
||||
|
||||
/* Remove the temporary file */
|
||||
if( @is_file( $temporary ) )
|
||||
{
|
||||
@unlink( $temporary );
|
||||
}
|
||||
|
||||
$this->exif = $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Contents
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function __toString();
|
||||
|
||||
/**
|
||||
* Resize
|
||||
*
|
||||
* @param int $width Width (in pixels)
|
||||
* @param int $height Height (in pixels)
|
||||
* @return void
|
||||
*/
|
||||
abstract public function resize( $width, $height );
|
||||
|
||||
/**
|
||||
* Crop to a given width and height (will attempt to downsize first)
|
||||
*
|
||||
* @param int $width Width (in pixels)
|
||||
* @param int $height Height (in pixels)
|
||||
* @return void
|
||||
*/
|
||||
abstract public function crop( $width, $height );
|
||||
|
||||
/**
|
||||
* Crop at specific points
|
||||
*
|
||||
* @param int $point1X x-point for top-left corner
|
||||
* @param int $point1Y y-point for top-left corner
|
||||
* @param int $point2X x-point for bottom-right corner
|
||||
* @param int $point2Y y-point for bottom-right corner
|
||||
* @return void
|
||||
*/
|
||||
abstract public function cropToPoints( $point1X, $point1Y, $point2X, $point2Y );
|
||||
|
||||
/**
|
||||
* Impose image
|
||||
*
|
||||
* @param \IPS\Image $image Image to impose
|
||||
* @param int $x Location to impose to, x axis
|
||||
* @param int $y Location to impose to, y axis
|
||||
* @return void
|
||||
*/
|
||||
abstract public function impose( $image, $x=0, $y=0 );
|
||||
|
||||
/**
|
||||
* Rotate image
|
||||
*
|
||||
* @param int $angle Angle of rotation
|
||||
* @return void
|
||||
*/
|
||||
abstract public function rotate( $angle );
|
||||
|
||||
/**
|
||||
* Get Image Orientation
|
||||
*
|
||||
* @return int|NULL
|
||||
*/
|
||||
abstract public function getImageOrientation();
|
||||
|
||||
/**
|
||||
* Set Image Orientation
|
||||
*
|
||||
* @param int $orientation The orientation
|
||||
* @return void
|
||||
*/
|
||||
abstract public function setImageOrientation( $orientation );
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Image Class - ImageMagick
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 06 Mar 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Image;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Image Class - ImageMagick
|
||||
*/
|
||||
class _Imagemagick extends \IPS\Image
|
||||
{
|
||||
/**
|
||||
* @brief Temporary filename
|
||||
*/
|
||||
protected $tempFile;
|
||||
|
||||
/**
|
||||
* @brief Imagick object
|
||||
*/
|
||||
protected $imagick;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $contents Contents
|
||||
* @return void
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct( $contents )
|
||||
{
|
||||
$this->tempFile = tempnam( \IPS\TEMP_DIRECTORY, 'imagick' );
|
||||
\file_put_contents( $this->tempFile, $contents );
|
||||
|
||||
try
|
||||
{
|
||||
$this->imagick = new \Imagick( $this->tempFile );
|
||||
}
|
||||
catch ( \ImagickException $e )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
}
|
||||
|
||||
/* Set width/height */
|
||||
$this->setDimensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
unlink( $this->tempFile );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Contents
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return (string) $this->imagick->getImagesBlob();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize
|
||||
*
|
||||
* @param int $width Width (in pixels)
|
||||
* @param int $height Height (in pixels)
|
||||
* @return void
|
||||
*/
|
||||
public function resize( $width, $height )
|
||||
{
|
||||
$format = $this->imagick->getImageFormat();
|
||||
|
||||
if( mb_strtolower( $format ) == 'gif' )
|
||||
{
|
||||
$this->imagick = $this->imagick->coalesceImages();
|
||||
|
||||
foreach( $this->imagick as $frame )
|
||||
{
|
||||
$frame->thumbnailImage( $width, $height );
|
||||
}
|
||||
|
||||
$this->imagick = $this->imagick->deconstructImages();
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->imagick->thumbnailImage( $width, $height );
|
||||
}
|
||||
|
||||
/* Set width/height */
|
||||
$this->setDimensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop to a given width and height (will attempt to downsize first)
|
||||
*
|
||||
* @param int $width Width (in pixels)
|
||||
* @param int $height Height (in pixels)
|
||||
* @return void
|
||||
*/
|
||||
public function crop( $width, $height )
|
||||
{
|
||||
$this->imagick->cropThumbnailImage( $width, $height );
|
||||
|
||||
/* Set width/height */
|
||||
$this->setDimensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop at specific points
|
||||
*
|
||||
* @param int $point1X x-point for top-left corner
|
||||
* @param int $point1Y y-point for top-left corner
|
||||
* @param int $point2X x-point for bottom-right corner
|
||||
* @param int $point2Y y-point for bottom-right corner
|
||||
* @return void
|
||||
*/
|
||||
public function cropToPoints( $point1X, $point1Y, $point2X, $point2Y )
|
||||
{
|
||||
$this->imagick->cropImage( $point2X - $point1X, $point2Y - $point1Y, $point1X, $point1Y );
|
||||
|
||||
/* Set width/height */
|
||||
$this->setDimensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Impose image
|
||||
*
|
||||
* @param \IPS\Image $image Image to impose
|
||||
* @param int $x Location to impose to, x axis
|
||||
* @param int $y Location to impose to, y axis
|
||||
* @return void
|
||||
*/
|
||||
public function impose( $image, $x=0, $y=0 )
|
||||
{
|
||||
$this->imagick->compositeImage( $image->imagick, \Imagick::COMPOSITE_DEFAULT, $x, $y );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate image
|
||||
*
|
||||
* @param int $angle Angle of rotation
|
||||
* @return void
|
||||
*/
|
||||
public function rotate( $angle )
|
||||
{
|
||||
$this->imagick->rotateImage( new \ImagickPixel('#00000000'), $angle );
|
||||
|
||||
/* Set width/height */
|
||||
$this->setDimensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the image width and height
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setDimensions()
|
||||
{
|
||||
/* Set width/height */
|
||||
$this->width = $this->imagick->getImageWidth();
|
||||
$this->height = $this->imagick->getImageHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Image Orientation
|
||||
*
|
||||
* @return int|NULL
|
||||
*/
|
||||
/**
|
||||
* Get Image Orientation
|
||||
*
|
||||
* @return int|NULL
|
||||
*/
|
||||
public function getImageOrientation()
|
||||
{
|
||||
try
|
||||
{
|
||||
/* This method does not exist in ImageMagick < 6.6.4 */
|
||||
return ( method_exists( $this->imagick, 'getImageOrientation' ) ) ? $this->imagick->getImageOrientation() : NULL;
|
||||
}
|
||||
catch( \ImagickException $e )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set image orientation
|
||||
*
|
||||
* @param int $orientation The orientation
|
||||
* @return void
|
||||
*/
|
||||
public function setImageOrientation( $orientation )
|
||||
{
|
||||
if( method_exists( $this->imagick, 'getImageOrientation' ) )
|
||||
{
|
||||
$this->imagick->setImageOrientation($orientation);
|
||||
}
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
Reference in new issue
Block a user