Version 4.2.0

This commit is contained in:
Neo committed 2025-12-19 05:44:59 -08:00
1 parent 0dc2aee5ed
commit 96997ddd8e
2074 files changed
+125454 -55780

No files matched your search

+101 -28
View File
@@ -1,12 +1,11 @@
<?php
/**
* @brief Image Class - GD
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
* @copyright (c) Invision Power Services, Inc.
* @license https://www.invisioncommunity.com/legal/standards/
* @package Invision Community
* @since 19 Feb 2013
* @version SVN_VERSION_NUMBER
*/
namespace IPS\Image;
@@ -31,12 +30,19 @@ class _Gd extends \IPS\Image
/**
* Constructor
*
* @param string $contents Contents
* @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
*/
public function __construct( $contents )
public function __construct( $contents, $noImage=FALSE )
{
/* If we are just creating an instance of the object without passing image contents as a string, return now */
if( $noImage === TRUE )
{
return;
}
/* Create the resource */
$this->image = @imagecreatefromstring( $contents );
if ( $this->image === FALSE )
@@ -54,8 +60,48 @@ class _Gd extends \IPS\Image
$this->height = imagesy( $this->image );
/* Try to maintain any transparency */
imagealphablending( $this->image, true );
imagesavealpha( $this->image, true );
$this->setAlpha();
}
/**
* 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
*/
public static function newImageCanvas( $width, $height, $rgb )
{
$obj = new static( NULL, TRUE );
$obj->type = 'png';
$obj->width = $width;
$obj->height = $height;
$obj->image = imagecreatetruecolor( $width, $height );
$bgColor = imagecolorallocatealpha( $obj->image, $rgb[0], $rgb[1], $rgb[2], 1 );
imagefill( $obj->image, 0, 0, $bgColor );
return $obj;
}
/**
* Write text on our image
*
* @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)
{
$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 );
imagettftext( $this->image, $size, 0, $x, $y, $fontColor, $font, $text );
}
/**
@@ -78,6 +124,8 @@ class _Gd extends \IPS\Image
*/
public function __toString()
{
$this->setAlpha();
ob_start();
switch( $this->type )
{
@@ -104,6 +152,18 @@ class _Gd extends \IPS\Image
return ob_get_clean();
}
/**
* Resets alpha transparency preservation
*
* @return void
*/
protected function setAlpha()
{
/* Turn off alpha blending and turn on saving of alpha channel info (this requires turning off alpha blending) */
imagealphablending( $this->image, false );
imagesavealpha( $this->image, true );
}
/**
* Resize
*
@@ -159,14 +219,13 @@ class _Gd extends \IPS\Image
imagefill( $newImage, 0, 0, $transindex );
}
break;
case 'jpeg':
imagealphablending( $newImage, TRUE );
break;
case 'png':
imagealphablending( $newImage, FALSE );
imagesavealpha( $newImage, TRUE );
case 'jpg':
/* We need to fill the background as transparent. If we copy a watermark image here (resizing it down for instance) and it has
transparency, then the background color will show through, so it needs to be transparent too. */
$transparent = imagecolorallocatealpha( $newImage, 0, 0, 0, 127 );
imagefill( $newImage, 0, 0, $transparent );
break;
}
@@ -197,7 +256,7 @@ class _Gd extends \IPS\Image
/* 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;
@@ -205,6 +264,8 @@ class _Gd extends \IPS\Image
/* Set width/height */
$this->width = imagesx( $this->image );
$this->height = imagesy( $this->image );
$this->setAlpha();
}
/**
@@ -232,14 +293,13 @@ class _Gd extends \IPS\Image
imagefill( $newImage, 0, 0, $transindex );
}
break;
case 'jpeg':
imagealphablending( $newImage, TRUE );
break;
case 'png':
imagealphablending( $newImage, FALSE );
imagesavealpha( $newImage, TRUE );
case 'jpg':
/* We need to fill the background as transparent. If we copy a watermark image here (resizing it down for instance) and it has
transparency, then the background color will show through, so it needs to be transparent too. */
$transparent = imagecolorallocatealpha( $newImage, 0, 0, 0, 127 );
imagefill( $newImage, 0, 0, $transparent );
break;
}
@@ -253,6 +313,8 @@ class _Gd extends \IPS\Image
/* Set width/height */
$this->width = imagesx( $this->image );
$this->height = imagesy( $this->image );
$this->setAlpha();
}
/**
@@ -265,6 +327,11 @@ class _Gd extends \IPS\Image
*/
public function impose( $image, $x=0, $y=0 )
{
/* Turn on alpha blending for both images */
imagealphablending( $this->image, true );
imagealphablending( $image->image, true );
/* Copy the image over - typically a watermark */
imagecopy( $this->image, $image->image, $x, $y, 0, 0, $image->width, $image->height );
}
@@ -281,10 +348,6 @@ class _Gd extends \IPS\Image
/* 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 );
}
/**
@@ -317,4 +380,14 @@ class _Gd extends \IPS\Image
{
/* Note, GD does not require orientation to be set after rotation */
}
/**
* Can we write text reliably on an image?
*
* @return bool
*/
public static function canWriteText()
{
return (bool) function_exists( 'imagettfbbox' );
}
}