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