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

+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;
}
}