Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+136
-121
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user