Version 4.2.0
This commit is contained in:
1 parent
0dc2aee5ed
commit
96997ddd8e
2074 files changed
+125454
-55780
No files matched your search
+101
-28
@@ -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' );
|
||||
}
|
||||
}
|
||||
+55
-9
@@ -1,12 +1,11 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Image Class
|
||||
* @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;
|
||||
@@ -117,7 +116,6 @@ abstract class _Image
|
||||
if( \IPS\Image::exifSupported() )
|
||||
{
|
||||
/* Differences in orientation between GD and ImageMagick can cause auto-reorient to not work properly */
|
||||
/* @see http://community.invisionpower.com/4bugtrack/rc1-imagemagick-and-vertical-photos-r2952 */
|
||||
$orientation = $obj->getImageOrientation();
|
||||
|
||||
if ( !( $obj instanceof Image\Imagemagick ) )
|
||||
@@ -251,10 +249,10 @@ abstract class _Image
|
||||
* @return void
|
||||
*/
|
||||
public function watermark( \IPS\Image $watermark )
|
||||
{
|
||||
{
|
||||
/* If it's too big, resize the watermark */
|
||||
$watermark->resizeToMax( $this->width, $this->height );
|
||||
|
||||
|
||||
/* Impose */
|
||||
$this->impose( $watermark, $this->width - $watermark->width, $this->height - $watermark->height );
|
||||
}
|
||||
@@ -332,7 +330,37 @@ abstract class _Image
|
||||
|
||||
$this->exif = $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
if( \IPS\Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
|
||||
{
|
||||
return Image\Imagemagick::newImageCanvas( $width, $height, $rgb );
|
||||
}
|
||||
else
|
||||
{
|
||||
return Image\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
|
||||
* @return void
|
||||
*/
|
||||
abstract public function write( $text, $font, $size );
|
||||
|
||||
/**
|
||||
* Get Contents
|
||||
*
|
||||
@@ -401,4 +429,22 @@ abstract class _Image
|
||||
* @return void
|
||||
*/
|
||||
abstract public function setImageOrientation( $orientation );
|
||||
|
||||
/**
|
||||
* Can we write text reliably on an image?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function canWriteText()
|
||||
{
|
||||
/* Create object */
|
||||
if( \IPS\Settings::i()->image_suite == 'imagemagick' and class_exists( 'Imagick', FALSE ) )
|
||||
{
|
||||
return Image\Imagemagick::canWriteText();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Image\Gd::canWriteText();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Image Class - ImageMagick
|
||||
* @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 06 Mar 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Image;
|
||||
@@ -26,7 +25,7 @@ class _Imagemagick extends \IPS\Image
|
||||
/**
|
||||
* @brief Temporary filename
|
||||
*/
|
||||
protected $tempFile;
|
||||
protected $tempFile = NULL;
|
||||
|
||||
/**
|
||||
* @brief Imagick object
|
||||
@@ -36,12 +35,19 @@ class _Imagemagick 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;
|
||||
}
|
||||
|
||||
$this->tempFile = tempnam( \IPS\TEMP_DIRECTORY, 'imagick' );
|
||||
\file_put_contents( $this->tempFile, $contents );
|
||||
|
||||
@@ -55,7 +61,7 @@ class _Imagemagick extends \IPS\Image
|
||||
}
|
||||
|
||||
/* Set quality (if image format is JPEG) */
|
||||
if ( in_array( $this->imagick->getImageFormat(), array( 'jpg', 'jpeg' ) ) )
|
||||
if ( in_array( mb_strtolower( $this->imagick->getImageFormat() ), array( 'jpg', 'jpeg' ) ) )
|
||||
{
|
||||
$this->imagick->setImageCompressionQuality( (int) \IPS\Settings::i()->image_jpg_quality ?: 85 );
|
||||
}
|
||||
@@ -71,7 +77,10 @@ class _Imagemagick extends \IPS\Image
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
unlink( $this->tempFile );
|
||||
if( $this->tempFile !== NULL )
|
||||
{
|
||||
unlink( $this->tempFile );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,7 +113,15 @@ class _Imagemagick extends \IPS\Image
|
||||
$frame->thumbnailImage( $width, $height );
|
||||
}
|
||||
|
||||
$this->imagick = $this->imagick->deconstructImages();
|
||||
/* Needs ImageMagick 6.2.9 or higher for optimizeImageLayers */
|
||||
try
|
||||
{
|
||||
$this->imagick = $this->imagick->optimizeImageLayers();
|
||||
}
|
||||
catch( \ImagickException $e )
|
||||
{
|
||||
$this->imagick = $this->imagick->deconstructImages();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -151,7 +168,15 @@ class _Imagemagick extends \IPS\Image
|
||||
$frame->setImagePage($point2X - $point1X, $point2Y - $point1Y, 0, 0);
|
||||
}
|
||||
|
||||
$this->imagick = $this->imagick->deconstructImages();
|
||||
/* Needs ImageMagick 6.2.9 or higher for optimizeImageLayers */
|
||||
try
|
||||
{
|
||||
$this->imagick = $this->imagick->optimizeImageLayers();
|
||||
}
|
||||
catch( \ImagickException $e )
|
||||
{
|
||||
$this->imagick = $this->imagick->deconstructImages();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -232,4 +257,58 @@ class _Imagemagick extends \IPS\Image
|
||||
$this->imagick->setImageOrientation($orientation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Can we write text reliably on an image?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function canWriteText()
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->imagick = new \Imagick();
|
||||
$obj->width = $width;
|
||||
$obj->height = $height;
|
||||
$obj->type = 'png';
|
||||
$pixel = new \ImagickPixel( "rgba({$rgb[0]}, {$rgb[1]}, {$rgb[2]}, 1)" );
|
||||
|
||||
$obj->imagick->newImage( $width, $height, $pixel );
|
||||
$obj->imagick->setImageFormat( "png" );
|
||||
|
||||
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
|
||||
*/
|
||||
public function write( $text, $font, $size )
|
||||
{
|
||||
$draw = new \ImagickDraw();
|
||||
$draw->setTextAntialias( true );
|
||||
$draw->setGravity( \Imagick::GRAVITY_CENTER );
|
||||
$draw->setFont( $font );
|
||||
$draw->setFontSize( $size );
|
||||
|
||||
$draw->setFillColor( "rgb( 255, 255, 255 )" );
|
||||
|
||||
$this->imagick->annotateImage( $draw, 0, 0, 0, $text );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user