Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+77 -72
View File
@@ -11,16 +11,26 @@
namespace IPS\Image;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use InvalidArgumentException;
use IPS\Image;
use IPS\IPS;
use IPS\Settings;
use function defined;
use function function_exists;
use function intval;
use function is_resource;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Image Class - GD
*/
class _Gd extends \IPS\Image
class Gd extends Image
{
/**
* @brief Image Resource
@@ -30,12 +40,12 @@ class _Gd extends \IPS\Image
/**
* Constructor
*
* @param string|NULL $contents Contents
* @param bool $noImage We are creating a new instance of the object internally and are not passing an image string
* @param string|null $contents Contents
* @param bool $noImage We are creating a new instance of the object internally and are not passing an image string
* @return void
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct( $contents, $noImage=FALSE )
public function __construct( ?string $contents, bool $noImage=FALSE )
{
/* If we are just creating an instance of the object without passing image contents as a string, return now */
if( $noImage === TRUE )
@@ -47,12 +57,12 @@ class _Gd extends \IPS\Image
$this->image = @imagecreatefromstring( $contents );
if ( $this->image === FALSE )
{
if ( $error = \IPS\IPS::$lastError )
if ( $error = IPS::$lastError )
{
throw new \InvalidArgumentException( $error->getMessage(), $error->getCode() );
throw new InvalidArgumentException( $error->getMessage(), $error->getCode() );
}
throw new \InvalidArgumentException;
throw new InvalidArgumentException;
}
/* Set width/height */
@@ -66,14 +76,14 @@ class _Gd extends \IPS\Image
/**
* Create a new blank canvas image
*
* @param int $width Width
* @param int $height Height
* @param array $rgb Color to use for bg
* @return \IPS\Image
* @param int $width Width
* @param int $height Height
* @param array $rgb Color to use for bg
* @return Image
*/
public static function newImageCanvas( $width, $height, $rgb )
public static function newImageCanvas( int $width, int $height, array $rgb ): Image
{
$obj = new static( NULL, TRUE );
$obj = new static(NULL, TRUE);
$obj->type = 'png';
$obj->width = $width;
$obj->height = $height;
@@ -87,19 +97,19 @@ class _Gd extends \IPS\Image
/**
* Write text on our image
*
* @param string $text Text
* @param string $font Path to font to use
* @param int $size Size of text
* @param string $text Text
* @param string $font Path to font to use
* @param int $size Size of text
* @return void
* @note Some latin characters have inherent left padding, so when we want to center a letter visually we need to account for this
*/
public function write( $text, $font, $size)
public function write( string $text, string $font, int $size ) : void
{
$fontColor = imagecolorallocate( $this->image, 255, 255, 255 );
$box = imagettfbbox( $size, 0, $font, $text );
$x = \intval( ( imagesx( $this->image ) - abs( max( $box[2], $box[4] ) ) ) / 2 ) - ( ( $box[0] > 0 ) ? ( $box[0] / 2.5 ) : 0 );
$y = \intval( ( imagesy( $this->image ) + ( abs( $box[5] ) - abs( $box[1] ) ) ) / 2 );
$x = intval( ( imagesx( $this->image ) - abs( max( $box[2], $box[4] ) ) ) / 2 ) - ( ( $box[0] > 0 ) ? ( $box[0] / 2.5 ) : 0 );
$y = intval( ( imagesy( $this->image ) + ( abs( $box[5] ) - abs( $box[1] ) ) ) / 2 );
imagettftext( $this->image, $size, 0, $x, $y, $fontColor, $font, $text );
}
@@ -111,7 +121,7 @@ class _Gd extends \IPS\Image
*/
public function __destruct()
{
if( \is_resource( $this->image ) )
if( is_resource( $this->image ) )
{
imagedestroy( $this->image );
}
@@ -138,19 +148,19 @@ class _Gd extends \IPS\Image
break;
case 'jpeg':
$quality = \IPS\Settings::i()->image_jpg_quality ?: 85;
$quality = Settings::i()->image_jpg_quality ?: 85;
imagejpeg( $this->image, NULL, $quality );
break;
case 'png':
$quality = \IPS\Settings::i()->image_png_quality_gd ?: NULL;
$quality = Settings::i()->image_png_quality_gd ?: NULL;
imagepng( $this->image, NULL, $quality );
break;
case 'webp':
$quality = \IPS\Settings::i()->image_jpg_quality ?: 85;
$quality = Settings::i()->image_jpg_quality ?: 85;
imagewebp( $this->image, NULL, $quality );
break;
@@ -164,7 +174,7 @@ class _Gd extends \IPS\Image
*
* @return void
*/
protected function setAlpha()
protected function setAlpha() : void
{
/* Turn off alpha blending and turn on saving of alpha channel info (this requires turning off alpha blending) */
imagealphablending( $this->image, false );
@@ -174,40 +184,40 @@ class _Gd extends \IPS\Image
/**
* Resize
*
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @return void
*/
public function resize( $width, $height )
public function resize( int $width, int $height ) : void
{
return $this->_manipulate( $width, $height, FALSE );
$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)
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @return void
*/
public function crop( $width, $height )
public function crop( int $width, int $height ) : void
{
return $this->_manipulate( $width, $height, TRUE );
$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
* @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 )
public function _manipulate( int $width, int $height, bool $crop=FALSE ) : void
{
if ( $this->isAnimatedGif )
{
return $this->image;
return;
}
/* Create a new canvas */
@@ -280,13 +290,13 @@ class _Gd extends \IPS\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
* @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 )
public function cropToPoints( int $point1X, int $point1Y, int $point2X, int $point2Y ) : void
{
/* Create a new canvas */
$newImage = imagecreatetruecolor( ( $point2X - $point1X > 0 ) ? $point2X - $point1X : 0, ( $point2Y - $point1Y > 0 ) ? $point2Y - $point1Y : 0 );
@@ -332,12 +342,12 @@ class _Gd extends \IPS\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
* @param 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 )
public function impose( Image $image, int $x=0, int $y=0 ) : void
{
/* Turn on alpha blending for both images */
imagealphablending( $this->image, true );
@@ -350,10 +360,10 @@ class _Gd extends \IPS\Image
/**
* Rotate image
*
* @param int $angle Angle of rotation
* @param int $angle Angle of rotation
* @return void
*/
public function rotate( $angle )
public function rotate( int $angle ) : void
{
$this->image = imagerotate( $this->image, $angle, 0 );
@@ -361,34 +371,24 @@ class _Gd extends \IPS\Image
$this->width = imagesx( $this->image );
$this->height = imagesy( $this->image );
}
/**
* Get Image Orientation
* Flip this image vertically
*
* @return int|NULL
* @return void
*/
public function getImageOrientation()
public function flip(): void
{
if ( static::exifSupported() )
{
$exif = $this->parseExif();
if ( isset( $exif['IFD0.Orientation'] ) )
{
return $exif['IFD0.Orientation'];
}
}
return NULL;
imageflip( $this->image, IMG_FLIP_HORIZONTAL );
}
/**
* Set Image Orientation
*
* @param int $orientation The orientation
* @param int $orientation The orientation
* @return void
*/
public function setImageOrientation( $orientation )
public function setImageOrientation( int $orientation ) : void
{
/* Note, GD does not require orientation to be set after rotation */
}
@@ -398,9 +398,9 @@ class _Gd extends \IPS\Image
*
* @return bool
*/
public static function canWriteText()
public static function canWriteText(): bool
{
return (bool) \function_exists( 'imagettfbbox' );
return function_exists( 'imagettfbbox' );
}
/**
@@ -408,15 +408,20 @@ class _Gd extends \IPS\Image
*
* @return array
*/
public static function supportedExtensions()
public static function supportedExtensions(): array
{
$extensions = static::$imageExtensions;
if( isset( gd_info()['WebP Support'] ) and gd_info()['WebP Support'] and version_compare( PHP_VERSION, '7.3.0' ) >= 0 )
if( isset( gd_info()['WebP Support'] ) and gd_info()['WebP Support'] )
{
$extensions[] = 'webp';
}
if( isset( gd_info()['AVIF Support'] ) and gd_info()['AVIF Support'] )
{
$extensions[] = 'avif';
}
return $extensions;
}
}
+136 -121
View File
@@ -11,61 +11,72 @@
namespace IPS;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use InvalidArgumentException;
use IPS\Image\Gd;
use IPS\Image\Imagemagick;
use LogicException;
use function defined;
use function file_put_contents;
use function function_exists;
use function in_array;
use function strlen;
use function substr;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Image Class
*/
abstract class _Image
abstract class Image
{
/**
* @brief Image Extensions
*/
public static $imageExtensions = array( 'gif', 'jpeg', 'jpe', 'jpg', 'png' );
public static array $imageExtensions = array( 'gif', 'jpeg', 'jpe', 'jpg', 'png' );
/**
* @brief Image Mime Types
*/
public static $imageMimes = array( 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/webp' );
public static array $imageMimes = array( 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/webp', 'image/avif' );
/**
* @brief Allow EXIF processing
*/
public static $exifEnabled = TRUE;
public static bool $exifEnabled = TRUE;
/**
* Determine if EXIF extraction is supported
*
* @return bool
*/
public static function exifSupported()
public static function exifSupported(): bool
{
return \function_exists( 'exif_read_data' ) and static::$exifEnabled;
return function_exists( 'exif_read_data' ) and static::$exifEnabled;
}
/**
* @brief Has the image been automatically rotated?
*/
public $hasBeenRotated = FALSE;
public bool $hasBeenRotated = FALSE;
/**
* Create Object
*
* @param string $contents Contents
* @param bool $checkRotation Check image to correct rotation
* @return \IPS\Image
* @throws \InvalidArgumentException
* @return Imagemagick|Gd|Image
* @throws InvalidArgumentException
* @link https://en.wikipedia.org/wiki/List_of_file_signatures
*/
public static function create( string $contents, bool $checkRotation=TRUE )
public static function create( string $contents, bool $checkRotation=TRUE ): Imagemagick|Image|Gd
{
/* Work out the type */
$imageType = NULL;
$isAnimated = FALSE;
$signatures = array(
'gif' => array(
'47' . '49' . '46' . '38' . '37' . '61',
@@ -80,14 +91,20 @@ abstract class _Image
);
$bytesNeeded = max( array_merge( array_map( 'strlen', array_map( 'hex2bin', array_merge( $signatures['gif'], $signatures['jpeg'], $signatures['png'] ) ) ), array( 12 ) ) );
$fileHeader = \substr( $contents, 0, $bytesNeeded );
$fileHeader = substr( $contents, 0, $bytesNeeded );
/* Try webp first since it's special: RIFF, 4 bytes for size, WEBP: */
if ( bin2hex( \substr( $fileHeader, 0, 4 ) ) === '52' . '49' . '46' . '46' AND bin2hex( \substr( $fileHeader, 8, 4 ) ) === '57' . '45' . '42' . '50' )
if ( bin2hex( substr( $fileHeader, 0, 4 ) ) === '52' . '49' . '46' . '46' AND bin2hex( substr( $fileHeader, 8, 4 ) ) === '57' . '45' . '42' . '50' )
{
$imageType = 'webp';
}
/* AVIF has 3 bytes of 00, then another byte that varies, and then ftypavif */
if( bin2hex( substr( $fileHeader, 0, 3 ) ) === '00' . '00' . '00' AND bin2hex( substr( $fileHeader, 4, 8 ) ) === '66' . '74' . '79' . '70' . '61' . '76' . '69' . '66' )
{
$imageType = 'avif';
}
/* If it's not webp, try the rest of the image types */
if( !$imageType )
{
@@ -95,7 +112,7 @@ abstract class _Image
{
foreach ( $_signatures as $signature )
{
if ( bin2hex( \substr( $fileHeader, 0, \strlen( hex2bin( $signature ) ) ) ) === $signature )
if ( bin2hex( substr( $fileHeader, 0, strlen( hex2bin( $signature ) ) ) ) === $signature )
{
$imageType = $type;
break 2;
@@ -106,22 +123,22 @@ abstract class _Image
if ( $imageType === NULL )
{
throw new \InvalidArgumentException;
throw new InvalidArgumentException;
}
/* Create object */
if( \IPS\Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
if( Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
{
$obj = new Image\Imagemagick( $contents );
$obj = new Imagemagick( $contents );
}
else
{
$obj = new Image\Gd( $contents );
$obj = new Gd( $contents );
}
$obj->type = $imageType;
/* Animated? @see https://www.php.net/manual/en/function.imagecreatefromgif.php#102915 */
if ( $obj->type === 'gif' and (boolean) preg_match( '#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $contents ) )
if ( $obj->type === 'gif' and preg_match( '#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $contents ) )
{
$obj->isAnimatedGif = TRUE;
$obj->contents = $contents;
@@ -133,52 +150,33 @@ abstract class _Image
$obj->setExifData( $contents );
/* If the image is misoriented, attempt to automatically reorient */
$orientation = $obj->getImageOrientation();
/* Differences in orientation between GD and ImageMagick can cause auto-reorient to not work properly */
if ( !( $obj instanceof Image\Imagemagick ) )
if ( $orientation = $obj->getImageOrientation() )
{
switch ( $orientation )
{
case 3:
$obj->rotate( 180 );
$obj->hasBeenRotated = TRUE;
break;
$mirrored = in_array( $orientation, [2,4,5,7] );
$degrees = match ($orientation) {
3,4 => 180,
5,6 => 90,
7,8 => -90,
default => 0
};
case 6:
$obj->rotate( -90 );
$obj->hasBeenRotated = TRUE;
break;
case 8:
$obj->rotate( 90 );
$obj->hasBeenRotated = TRUE;
break;
}
}
else
{
switch( $orientation )
if ( $degrees !== 0 OR $mirrored )
{
case 3:
$obj->rotate( 180 );
$obj->hasBeenRotated = TRUE;
break;
case 6:
$obj->rotate( 90 );
$obj->hasBeenRotated = TRUE;
break;
case 8:
$obj->rotate( -90 );
$obj->hasBeenRotated = TRUE;
break;
$obj->rotate( $degrees );
if ( $mirrored )
{
$obj->flip();
}
$obj->hasBeenRotated = true;
}
/* ImageMagick requires orientation be reset when rotated */
if ( $degrees !== 0 OR $mirrored OR $obj instanceof Imagemagick )
{
$obj->setImageOrientation( 1 );
}
}
/* ImageMagick requires orientation be reset when rotated */
$obj->setImageOrientation( 1 );
}
/* Return */
@@ -188,42 +186,42 @@ abstract class _Image
/**
* @brief Type ('png', 'jpeg' or 'gif')
*/
public $type;
public ?string $type = NULL;
/**
* @brief Width
*/
public $width;
public ?int $width = null;
/**
* @brief Height
*/
public $height;
public ?int $height = null;
/**
* @brief Is this an animated gif?
*/
public $isAnimatedGif = FALSE;
public bool $isAnimatedGif = FALSE;
/**
* @brief Contents of the image file when animated gif
*/
public $contents = NULL;
public ?string $contents = NULL;
/**
* @brief EXIF data - has to be pulled and stored before GD manipulates image
*/
protected $exif = array();
protected array $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
* @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 Returns TRUE if it actually resized, or FALSE if it wasn't necessary
*/
public function resizeToMax( $maxWidth=NULL, $maxHeight=NULL, $retainRatio=TRUE )
public function resizeToMax(int $maxWidth=NULL, int $maxHeight=NULL, bool $retainRatio=TRUE ): bool
{
/* If the image is smaller than max we can skip */
if( ( $maxWidth == NULL or $this->width < $maxWidth ) and ( $maxHeight == NULL or $this->height < $maxHeight ) )
@@ -263,10 +261,10 @@ abstract class _Image
/**
* Add Watermark
*
* @param \IPS\Image $watermark The watermark
* @param Image $watermark The watermark
* @return void
*/
public function watermark( \IPS\Image $watermark )
public function watermark( Image $watermark ) : void
{
/* If it's too big, resize the watermark */
$watermark->resizeToMax( $this->width, $this->height );
@@ -279,16 +277,16 @@ abstract class _Image
* Parse file object to extract EXIF data
*
* @return array
* @throws \LogicException
* @throws LogicException
*/
public function parseExif()
public function parseExif(): array
{
if( !static::exifSupported() )
{
throw new \LogicException( 'NO_EXIF' );
throw new LogicException( 'NO_EXIF' );
}
if( !\in_array( $this->type, array( 'jpeg', 'jpg', 'jpe' ) ) )
if( !in_array( $this->type, array( 'jpeg', 'jpg', 'jpe' ) ) )
{
return array();
}
@@ -316,7 +314,7 @@ abstract class _Image
*
* @return array
*/
public function getExifData()
public function getExifData(): array
{
return $this->exif;
}
@@ -324,19 +322,19 @@ abstract class _Image
/**
* Get EXIF data, if possible
*
* @param string $contents Image contents
* @param string $contents Image contents
* @return void
*/
public function setExifData( $contents )
public function setExifData( string $contents ) : void
{
if( !\in_array( $this->type, array( 'jpeg', 'jpg', 'jpe' ) ) )
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 );
$temporary = tempnam( TEMP_DIRECTORY, 'exif' );
file_put_contents( $temporary, $contents );
$result = @exif_read_data( $temporary, NULL, TRUE );
@@ -352,32 +350,32 @@ abstract class _Image
/**
* Create a new blank canvas image
*
* @param int $width Width
* @param int $height Height
* @param array $rgb Color to use for bg
* @return \IPS\Image
* @param int $width Width
* @param int $height Height
* @param array $rgb Color to use for bg
* @return Image
*/
public static function newImageCanvas( $width, $height, $rgb )
public static function newImageCanvas( int $width, int $height, array $rgb ): Image
{
if( \IPS\Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
if( Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
{
return Image\Imagemagick::newImageCanvas( $width, $height, $rgb );
return Imagemagick::newImageCanvas( $width, $height, $rgb );
}
else
{
return Image\Gd::newImageCanvas( $width, $height, $rgb );
return Gd::newImageCanvas( $width, $height, $rgb );
}
}
/**
* Write text on our image
*
* @param string $text Text
* @param string $font Path to font to use
* @param int $size Size of text
* @param string $text Text
* @param string $font Path to font to use
* @param int $size Size of text
* @return void
*/
abstract public function write( $text, $font, $size );
abstract public function write( string $text, string $font, int $size ) : void;
/**
* Get Contents
@@ -389,74 +387,91 @@ abstract class _Image
/**
* Resize
*
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @return void
*/
abstract public function resize( $width, $height );
abstract public function resize( int $width, int $height ) : void;
/**
* Crop to a given width and height (will attempt to downsize first)
*
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @return void
*/
abstract public function crop( $width, $height );
abstract public function crop( int $width, int $height ) : void;
/**
* 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
* @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 );
abstract public function cropToPoints( int $point1X, int $point1Y, int $point2X, int $point2Y ) : void;
/**
* 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
* @param 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 );
abstract public function impose( Image $image, int $x=0, int $y=0 ) : void;
/**
* Rotate image
*
* @param int $angle Angle of rotation
* @param int $angle Angle of rotation
* @return void
*/
abstract public function rotate( $angle );
abstract public function rotate( int $angle ) : void;
/**
* @return void
*/
abstract public function flip():void;
/**
* Get Image Orientation
*
* @return int|NULL
*/
abstract public function getImageOrientation();
public function getImageOrientation(): ?int
{
if ( static::exifSupported() )
{
$exif = $this->parseExif();
if ( isset( $exif['IFD0.Orientation'] ) )
{
return (int) $exif['IFD0.Orientation'];
}
}
return null;
}
/**
* Set Image Orientation
*
* @param int $orientation The orientation
* @param int $orientation The orientation
* @return void
*/
abstract public function setImageOrientation( $orientation );
abstract public function setImageOrientation( int $orientation ) : void;
/**
* Can we write text reliably on an image?
*
* @return bool
*/
public static function canWriteText()
public static function canWriteText(): bool
{
/* Create object */
if( \IPS\Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
if( Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
{
return Image\Imagemagick::canWriteText();
}
@@ -471,9 +486,9 @@ abstract class _Image
*
* @return array
*/
public static function supportedExtensions()
public static function supportedExtensions(): array
{
if( \IPS\Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
if( Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
{
return Image\Imagemagick::supportedExtensions();
}
@@ -488,7 +503,7 @@ abstract class _Image
*
* @return bool
*/
public function isSquare() : bool
public function isSquare(): bool
{
return $this->width === $this->height;
}
+101 -70
View File
@@ -11,36 +11,50 @@
namespace IPS\Image;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use Imagick;
use ImagickDraw;
use ImagickException;
use ImagickPixel;
use InvalidArgumentException;
use IPS\Image;
use IPS\Settings;
use function count;
use function defined;
use function file_put_contents;
use function in_array;
use const IPS\TEMP_DIRECTORY;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Image Class - ImageMagick
*/
class _Imagemagick extends \IPS\Image
class Imagemagick extends Image
{
/**
* @brief Temporary filename
*/
protected $tempFile = NULL;
protected string|null|false $tempFile = NULL;
/**
* @brief Imagick object
*/
protected $imagick;
protected Imagick $imagick;
/**
* Constructor
*
* @param string|NULL $contents Contents
* @param bool $noImage We are creating a new instance of the object internally and are not passing an image string
* @param string|null $contents Contents
* @param bool $noImage We are creating a new instance of the object internally and are not passing an image string
* @return void
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct( $contents, $noImage=FALSE )
public function __construct( ?string $contents, bool $noImage=FALSE )
{
/* If we are just creating an instance of the object without passing image contents as a string, return now */
if( $noImage === TRUE )
@@ -48,22 +62,22 @@ class _Imagemagick extends \IPS\Image
return;
}
$this->tempFile = tempnam( \IPS\TEMP_DIRECTORY, 'imagick' );
\file_put_contents( $this->tempFile, $contents );
$this->tempFile = tempnam( TEMP_DIRECTORY, 'imagick' );
file_put_contents( $this->tempFile, $contents );
try
{
$this->imagick = new \Imagick( $this->tempFile );
$this->imagick = new Imagick( $this->tempFile );
/* Set quality (if image format is JPEG) */
if ( \in_array( mb_strtolower( $this->imagick->getImageFormat() ), array( 'jpg', 'jpeg', 'webp' ) ) )
if ( in_array( mb_strtolower( $this->imagick->getImageFormat() ), array( 'jpg', 'jpeg', 'webp' ) ) )
{
$this->imagick->setImageCompressionQuality( (int) \IPS\Settings::i()->image_jpg_quality ?: 85 );
$this->imagick->setImageCompressionQuality( (int) Settings::i()->image_jpg_quality ?: 85 );
}
}
catch ( \ImagickException $e )
catch ( ImagickException $e )
{
throw new \InvalidArgumentException( $e->getMessage(), $e->getCode() );
throw new InvalidArgumentException( $e->getMessage(), $e->getCode() );
}
/* Set width/height */
@@ -91,19 +105,19 @@ class _Imagemagick extends \IPS\Image
public function __toString()
{
/* If possible, retain the color profiles when stripping EXIF data */
if( \IPS\Settings::i()->imagick_strip_exif )
if( Settings::i()->imagick_strip_exif )
{
$imageColorProfiles = array();
try
{
$imageColorProfiles = $this->imagick->getImageProfiles( 'icc', true );
$imageColorProfiles = $this->imagick->getImageProfiles( 'icc' );
}
catch( \ImagickException $e ){}
catch( ImagickException $e ){}
$this->imagick->stripImage();
if( \count( $imageColorProfiles ) )
if( count( $imageColorProfiles ) )
{
foreach( $imageColorProfiles as $type => $profile )
{
@@ -111,22 +125,22 @@ class _Imagemagick extends \IPS\Image
{
$this->imagick->profileImage( $type, $profile );
}
catch( \ImagickException $e ){}
catch( ImagickException $e ){}
}
}
}
return (string) $this->imagick->getImagesBlob();
return $this->imagick->getImagesBlob();
}
/**
* Resize
*
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @return void
*/
public function resize( $width, $height )
public function resize( int $width, int $height ) : void
{
$format = $this->imagick->getImageFormat();
@@ -142,11 +156,11 @@ class _Imagemagick extends \IPS\Image
/* Needs ImageMagick 6.2.9 or higher for optimizeImageLayers */
try
{
$this->imagick = $this->imagick->optimizeImageLayers();
$this->imagick->optimizeImageLayers();
}
catch( \ImagickException $e )
catch( ImagickException $e )
{
$this->imagick = $this->imagick->deconstructImages();
$this->imagick->deconstructImages();
}
}
else
@@ -161,11 +175,11 @@ class _Imagemagick extends \IPS\Image
/**
* Crop to a given width and height (will attempt to downsize first)
*
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @param int $width Width (in pixels)
* @param int $height Height (in pixels)
* @return void
*/
public function crop( $width, $height )
public function crop( int $width, int $height ) : void
{
$this->imagick->cropThumbnailImage( $width, $height );
@@ -176,13 +190,13 @@ class _Imagemagick extends \IPS\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
* @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 )
public function cropToPoints( int $point1X, int $point1Y, int $point2X, int $point2Y ) : void
{
if( mb_strtolower( $this->imagick->getImageFormat() ) === 'gif' )
{
@@ -197,11 +211,11 @@ class _Imagemagick extends \IPS\Image
/* Needs ImageMagick 6.2.9 or higher for optimizeImageLayers */
try
{
$this->imagick = $this->imagick->optimizeImageLayers();
$this->imagick->optimizeImageLayers();
}
catch( \ImagickException $e )
catch( ImagickException $e )
{
$this->imagick = $this->imagick->deconstructImages();
$this->imagick->deconstructImages();
}
}
else
@@ -216,36 +230,44 @@ class _Imagemagick extends \IPS\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
* @param 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 )
public function impose( Image $image, int $x=0, int $y=0 ) : void
{
$this->imagick->compositeImage( $image->imagick, \Imagick::COMPOSITE_DEFAULT, $x, $y );
$this->imagick->compositeImage( $image->imagick, Imagick::COMPOSITE_DEFAULT, $x, $y );
}
/**
* Rotate image
*
* @param int $angle Angle of rotation
* @param int $angle Angle of rotation
* @return void
*/
public function rotate( $angle )
public function rotate( int $angle ) : void
{
$this->imagick->rotateImage( new \ImagickPixel('#00000000'), $angle );
$this->imagick->rotateImage( new ImagickPixel('#00000000'), $angle );
/* Set width/height */
$this->setDimensions();
}
/**
* @return void
*/
public function flip(): void
{
$this->imagick->flopImage();
}
/**
* Set the image width and height
*
* @return void
*/
protected function setDimensions()
protected function setDimensions() : void
{
/* If this is a gif, we need to coalesce the image in order to get the proper dimensions */
if ( mb_strtolower( $this->imagick->getImageFormat() ) === 'gif' )
@@ -263,14 +285,18 @@ class _Imagemagick extends \IPS\Image
*
* @return int|NULL
*/
public function getImageOrientation()
public function getImageOrientation(): int|NULL
{
try
{
if ( $orientation = parent::getImageOrientation() )
{
return $orientation;
}
/* This method does not exist in ImageMagick < 6.6.4 */
return ( method_exists( $this->imagick, 'getImageOrientation' ) ) ? $this->imagick->getImageOrientation() : NULL;
}
catch( \ImagickException $e )
catch( ImagickException $e )
{
return NULL;
}
@@ -279,10 +305,10 @@ class _Imagemagick extends \IPS\Image
/**
* Set image orientation
*
* @param int $orientation The orientation
* @param int $orientation The orientation
* @return void
*/
public function setImageOrientation( $orientation )
public function setImageOrientation( int $orientation ) : void
{
if( method_exists( $this->imagick, 'getImageOrientation' ) )
{
@@ -295,7 +321,7 @@ class _Imagemagick extends \IPS\Image
*
* @return bool
*/
public static function canWriteText()
public static function canWriteText(): bool
{
return TRUE;
}
@@ -303,19 +329,19 @@ class _Imagemagick extends \IPS\Image
/**
* Create a new blank canvas image
*
* @param int $width Width
* @param int $height Height
* @param array $rgb Color to use for bg
* @return \IPS\Image
* @param int $width Width
* @param int $height Height
* @param array $rgb Color to use for bg
* @return Image
*/
public static function newImageCanvas( $width, $height, $rgb )
public static function newImageCanvas( int $width, int $height, array $rgb ): Image
{
$obj = new static( NULL, TRUE );
$obj->imagick = new \Imagick();
$obj = new static(NULL, TRUE);
$obj->imagick = new Imagick();
$obj->width = $width;
$obj->height = $height;
$obj->type = 'png';
$pixel = new \ImagickPixel( "rgba({$rgb[0]}, {$rgb[1]}, {$rgb[2]}, 1)" );
$pixel = new ImagickPixel( "rgba({$rgb[0]}, {$rgb[1]}, {$rgb[2]}, 1)" );
$obj->imagick->newImage( $width, $height, $pixel );
$obj->imagick->setImageFormat( "png" );
@@ -326,20 +352,20 @@ class _Imagemagick extends \IPS\Image
/**
* Write text on our image
*
* @param string $text Text
* @param string $font Path to font to use
* @param int $size Size of text
* @param string $text Text
* @param string $font Path to font to use
* @param int $size Size of text
* @return void
*/
public function write( $text, $font, $size )
public function write( string $text, string $font, int $size ) : void
{
$draw = new \ImagickDraw();
$draw = new ImagickDraw();
$draw->setTextAntialias( true );
$draw->setGravity( \Imagick::GRAVITY_CENTER );
$draw->setGravity( Imagick::GRAVITY_CENTER );
$draw->setFont( $font );
$draw->setFontSize( $size );
$draw->setFillColor( "rgb( 255, 255, 255 )" );
$draw->setFillColor( new ImagickPixel( "rgba(255,255,255,1)" ) );
$this->imagick->annotateImage( $draw, 0, 0, 0, $text );
}
@@ -349,15 +375,20 @@ class _Imagemagick extends \IPS\Image
*
* @return array
*/
public static function supportedExtensions()
public static function supportedExtensions(): array
{
$extensions = static::$imageExtensions;
if( \in_array( 'WEBP', \Imagick::queryFormats() ) )
if( in_array( 'WEBP', Imagick::queryFormats() ) )
{
$extensions[] = 'webp';
}
if( in_array( 'AVIF', Imagick::queryFormats() ) )
{
$extensions[] = 'avif';
}
return $extensions;
}
}