2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A wrapper class for GD-based images, with lots of manipulation functions.
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage filesystem
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class GD extends Object {
|
|
|
|
protected $gd, $width, $height;
|
2008-06-25 07:22:56 +02:00
|
|
|
protected $quality;
|
|
|
|
|
|
|
|
protected static $default_quality = 75;
|
2010-10-19 07:04:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default image quality.
|
|
|
|
* @param quality int A number from 0 to 100, 100 being the best quality.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_default_quality($quality) {
|
2010-10-19 07:04:14 +02:00
|
|
|
if(is_numeric($quality) && (int) $quality >= 0 && (int) $quality <= 100) {
|
|
|
|
self::$default_quality = (int) $quality;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($filename = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// If we're working with image resampling, things could take a while. Bump up the time-limit
|
2009-06-28 04:36:46 +02:00
|
|
|
increase_time_limit_to(300);
|
2008-11-17 23:59:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($filename) {
|
|
|
|
// We use getimagesize instead of extension checking, because sometimes extensions are wrong.
|
|
|
|
list($width, $height, $type, $attr) = getimagesize($filename);
|
|
|
|
switch($type) {
|
2008-11-17 23:59:17 +01:00
|
|
|
case 1: if(function_exists('imagecreatefromgif')) $this->setGD(imagecreatefromgif($filename)); break;
|
|
|
|
case 2: if(function_exists('imagecreatefromjpeg')) $this->setGD(imagecreatefromjpeg($filename)); break;
|
2012-04-06 16:19:30 +02:00
|
|
|
case 3: if(function_exists('imagecreatefrompng')) {
|
|
|
|
$img = imagecreatefrompng($filename);
|
|
|
|
imagesavealpha($img, true); // save alphablending setting (important)
|
|
|
|
$this->setGD($img);
|
|
|
|
break;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-06-25 07:22:56 +02:00
|
|
|
|
|
|
|
$this->quality = self::$default_quality;
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2010-10-19 07:04:00 +02:00
|
|
|
|
|
|
|
public function setGD($gd) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->gd = $gd;
|
|
|
|
$this->width = imagesx($gd);
|
|
|
|
$this->height = imagesy($gd);
|
|
|
|
}
|
2010-10-19 07:04:00 +02:00
|
|
|
|
|
|
|
public function getGD() {
|
|
|
|
return $this->gd;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the image quality, used when saving JPEGs.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setQuality($quality) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->quality = $quality;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize an image to cover the given width/height completely, and crop off any overhanging edges.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function croppedResize($width, $height) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->gd) return;
|
|
|
|
|
|
|
|
$width = round($width);
|
|
|
|
$height = round($height);
|
|
|
|
|
2012-03-26 06:21:14 +02:00
|
|
|
// Check that a resize is actually necessary.
|
|
|
|
if ($width == $this->width && $height == $this->height) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
|
|
|
|
|
|
|
// Preserves transparency between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
|
|
|
|
|
|
|
$destAR = $width / $height;
|
|
|
|
if ($this->width > 0 && $this->height > 0 ){
|
|
|
|
// We can't divide by zero theres something wrong.
|
|
|
|
|
|
|
|
$srcAR = $this->width / $this->height;
|
|
|
|
|
|
|
|
// Destination narrower than the source
|
|
|
|
if($destAR < $srcAR) {
|
|
|
|
$srcY = 0;
|
|
|
|
$srcHeight = $this->height;
|
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$srcWidth = round( $this->height * $destAR );
|
|
|
|
$srcX = round( ($this->width - $srcWidth) / 2 );
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Destination shorter than the source
|
|
|
|
} else {
|
|
|
|
$srcX = 0;
|
|
|
|
$srcWidth = $this->width;
|
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$srcHeight = round( $this->width / $destAR );
|
|
|
|
$srcY = round( ($this->height - $srcHeight) / 2 );
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
imagecopyresampled($newGD, $this->gd, 0,0, $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);
|
2010-05-25 05:53:12 +02:00
|
|
|
}
|
|
|
|
$output = clone $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
$output->setGD($newGD);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes the image to fit within the given region.
|
|
|
|
* Behaves similarly to paddedResize but without the padding.
|
|
|
|
* @todo This method isn't very efficent
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fittedResize($width, $height) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$gd = $this->resizeByHeight($height);
|
|
|
|
if($gd->width > $width) $gd = $gd->resizeByWidth($width);
|
|
|
|
return $gd;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function hasGD() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->gd ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize an image, skewing it as necessary.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resize($width, $height) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->gd) return;
|
|
|
|
|
|
|
|
$width = round($width);
|
|
|
|
$height = round($height);
|
|
|
|
|
2012-03-26 06:21:14 +02:00
|
|
|
// Check that a resize is actually necessary.
|
|
|
|
if ($width == $this->width && $height == $this->height) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$width && !$height) user_error("No dimensions given", E_USER_ERROR);
|
|
|
|
if(!$width) user_error("Width not given", E_USER_ERROR);
|
|
|
|
if(!$height) user_error("Height not given", E_USER_ERROR);
|
|
|
|
|
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
|
|
|
|
|
|
|
// Preserves transparency between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
|
|
|
|
|
|
|
imagecopyresampled($newGD, $this->gd, 0,0, 0, 0, $width, $height, $this->width, $this->height);
|
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
$output->setGD($newGD);
|
2007-09-14 03:48:38 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:51:53 +02:00
|
|
|
/**
|
|
|
|
* Rotates image by given angle.
|
|
|
|
*
|
|
|
|
* @param angle
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2007-09-14 03:51:53 +02:00
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function rotate($angle) {
|
2007-09-14 03:48:38 +02:00
|
|
|
if(!$this->gd) return;
|
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
if(function_exists("imagerotate")) {
|
2007-09-14 03:52:50 +02:00
|
|
|
$newGD = imagerotate($this->gd, $angle,0);
|
|
|
|
} else {
|
|
|
|
//imagerotate is not included in PHP included in Ubuntu
|
|
|
|
$newGD = $this->rotatePixelByPixel($angle);
|
|
|
|
}
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2007-09-14 03:48:38 +02:00
|
|
|
$output->setGD($newGD);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:52:50 +02:00
|
|
|
/**
|
|
|
|
* Rotates image by given angle. It's slow because makes it pixel by pixel rather than
|
|
|
|
* using built-in function. Used when imagerotate function is not available(i.e. Ubuntu)
|
|
|
|
*
|
|
|
|
* @param angle
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2007-09-14 03:52:50 +02:00
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function rotatePixelByPixel($angle) {
|
2007-09-14 03:52:50 +02:00
|
|
|
$sourceWidth = imagesx($this->gd);
|
|
|
|
$sourceHeight = imagesy($this->gd);
|
|
|
|
if ($angle == 180) {
|
|
|
|
$destWidth = $sourceWidth;
|
|
|
|
$destHeight = $sourceHeight;
|
|
|
|
} else {
|
|
|
|
$destWidth = $sourceHeight;
|
|
|
|
$destHeight = $sourceWidth;
|
|
|
|
}
|
|
|
|
$rotate=imagecreatetruecolor($destWidth,$destHeight);
|
|
|
|
imagealphablending($rotate, false);
|
|
|
|
for ($x = 0; $x < ($sourceWidth); $x++) {
|
|
|
|
for ($y = 0; $y < ($sourceHeight); $y++) {
|
|
|
|
$color = imagecolorat($this->gd, $x, $y);
|
|
|
|
switch ($angle) {
|
|
|
|
case 90:
|
|
|
|
imagesetpixel($rotate, $y, $destHeight - $x - 1, $color);
|
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
imagesetpixel($rotate, $destWidth - $x - 1, $destHeight - $y - 1, $color);
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
imagesetpixel($rotate, $destWidth - $y - 1, $x, $color);
|
|
|
|
break;
|
|
|
|
default: $rotate = $this->gd;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rotate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-14 03:51:53 +02:00
|
|
|
/**
|
|
|
|
* Crop's part of image.
|
|
|
|
*
|
|
|
|
* @param top y position of left upper corner of crop rectangle
|
|
|
|
* @param left x position of left upper corner of crop rectangle
|
|
|
|
* @param width rectangle width
|
|
|
|
* @param height rectangle height
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2007-09-14 03:51:53 +02:00
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function crop($top, $left, $width, $height) {
|
2007-09-14 03:48:38 +02:00
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
|
|
|
imagecopyresampled($newGD, $this->gd, 0, 0, $left, $top, $width, $height, $width, $height);
|
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2007-09-14 03:48:38 +02:00
|
|
|
$output->setGD($newGD);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2007-09-14 03:51:53 +02:00
|
|
|
/**
|
|
|
|
* Method return width of image.
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2007-09-14 03:51:53 +02:00
|
|
|
* @return integer width.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getWidth() {
|
2007-09-14 03:51:53 +02:00
|
|
|
return $this->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method return height of image.
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2007-09-14 03:51:53 +02:00
|
|
|
* @return integer height
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getHeight() {
|
2007-09-14 03:51:53 +02:00
|
|
|
return $this->height;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize an image by width. Preserves aspect ratio.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resizeByWidth( $width ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$heightScale = $width / $this->width;
|
|
|
|
return $this->resize( $width, $heightScale * $this->height );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize an image by height. Preserves aspect ratio
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resizeByHeight( $height ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$scale = $height / $this->height;
|
|
|
|
return $this->resize( $scale * $this->width, $height );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize the image by preserving aspect ratio. By default, it will keep the image inside the maxWidth and maxHeight
|
|
|
|
* Passing useAsMinimum will make the smaller dimension equal to the maximum corresponding dimension
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resizeRatio( $maxWidth, $maxHeight, $useAsMinimum = false ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$widthRatio = $maxWidth / $this->width;
|
|
|
|
$heightRatio = $maxHeight / $this->height;
|
|
|
|
|
|
|
|
if( $widthRatio < $heightRatio )
|
|
|
|
return $useAsMinimum ? $this->resizeByHeight( $maxHeight ) : $this->resizeByWidth( $maxWidth );
|
|
|
|
else
|
|
|
|
return $useAsMinimum ? $this->resizeByWidth( $maxWidth ) : $this->resizeByHeight( $maxHeight );
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function color_web2gd($image, $webColor) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(substr($webColor,0,1) == "#") $webColor = substr($webColor,1);
|
|
|
|
$r = hexdec(substr($webColor,0,2));
|
|
|
|
$g = hexdec(substr($webColor,2,2));
|
|
|
|
$b = hexdec(substr($webColor,4,2));
|
|
|
|
|
|
|
|
return imagecolorallocate($image, $r, $g, $b);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize to fit fully within the given box, without resizing. Extra space left around
|
|
|
|
* the image will be padded with the background color.
|
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
* @param backgroundColour
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->gd) return;
|
|
|
|
|
|
|
|
$width = round($width);
|
|
|
|
$height = round($height);
|
|
|
|
|
2012-03-26 06:21:14 +02:00
|
|
|
// Check that a resize is actually necessary.
|
|
|
|
if ($width == $this->width && $height == $this->height) {
|
|
|
|
return $this;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
|
|
|
|
|
|
|
// Preserves transparency between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
|
|
|
|
|
|
|
$bg = GD::color_web2gd($newGD, $backgroundColor);
|
|
|
|
imagefilledrectangle($newGD, 0, 0, $width, $height, $bg);
|
|
|
|
|
|
|
|
$destAR = $width / $height;
|
|
|
|
if ($this->width > 0 && $this->height > 0) {
|
|
|
|
// We can't divide by zero theres something wrong.
|
|
|
|
|
|
|
|
$srcAR = $this->width / $this->height;
|
|
|
|
|
|
|
|
// Destination narrower than the source
|
|
|
|
if($destAR > $srcAR) {
|
|
|
|
$destY = 0;
|
|
|
|
$destHeight = $height;
|
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$destWidth = round( $height * $srcAR );
|
|
|
|
$destX = round( ($width - $destWidth) / 2 );
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Destination shorter than the source
|
|
|
|
} else {
|
|
|
|
$destX = 0;
|
|
|
|
$destWidth = $width;
|
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$destHeight = round( $width / $srcAR );
|
|
|
|
$destY = round( ($height - $destHeight) / 2 );
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
imagecopyresampled($newGD, $this->gd, $destX, $destY, 0, 0, $destWidth, $destHeight, $this->width, $this->height);
|
2010-05-25 05:53:12 +02:00
|
|
|
}
|
|
|
|
$output = clone $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
$output->setGD($newGD);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the image greyscale
|
|
|
|
* $rv = red value, defaults to 38
|
|
|
|
* $gv = green value, defaults to 36
|
|
|
|
* $bv = blue value, defaults to 26
|
|
|
|
* Based (more or less entirely, with changes for readability) on code from http://www.teckis.com/scriptix/thumbnails/teck.html
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function greyscale($rv=38, $gv=36, $bv=26) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$width = $this->width;
|
|
|
|
$height = $this->height;
|
|
|
|
$newGD = imagecreatetruecolor($this->width, $this->height);
|
|
|
|
|
|
|
|
$rt = $rv + $bv + $gv;
|
|
|
|
$rr = ($rv == 0) ? 0 : 1/($rt/$rv);
|
|
|
|
$br = ($bv == 0) ? 0 : 1/($rt/$bv);
|
|
|
|
$gr = ($gv == 0) ? 0 : 1/($rt/$gv);
|
2009-04-29 01:52:15 +02:00
|
|
|
for($dy = 0; $dy < $height; $dy++) {
|
|
|
|
for($dx = 0; $dx < $width; $dx++) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$pxrgb = imagecolorat($this->gd, $dx, $dy);
|
|
|
|
$heightgb = ImageColorsforIndex($this->gd, $pxrgb);
|
|
|
|
$newcol = ($rr*$heightgb['red']) + ($br*$heightgb['blue']) + ($gr*$heightgb['green']);
|
|
|
|
$setcol = ImageColorAllocate($newGD, $newcol, $newcol, $newcol);
|
|
|
|
imagesetpixel($newGD, $dx, $dy, $setcol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
$output->setGD($newGD);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function makeDir($dirname) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!file_exists(dirname($dirname))) $this->makeDir(dirname($dirname));
|
2007-10-26 01:07:24 +02:00
|
|
|
if(!file_exists($dirname)) mkdir($dirname, Filesystem::$folder_create_mask);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function writeTo($filename) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->makeDir(dirname($filename));
|
|
|
|
|
|
|
|
if($filename) {
|
|
|
|
if(file_exists($filename)) list($width, $height, $type, $attr) = getimagesize($filename);
|
|
|
|
|
|
|
|
if(file_exists($filename)) unlink($filename);
|
|
|
|
|
|
|
|
$ext = strtolower(substr($filename, strrpos($filename,'.')+1));
|
|
|
|
if(!isset($type)) switch($ext) {
|
|
|
|
case "gif": $type = 1; break;
|
|
|
|
case "jpeg": case "jpg": case "jpe": $type = 2; break;
|
|
|
|
default: $type = 3; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the extension does not exist, the file will not be created!
|
|
|
|
|
|
|
|
switch($type) {
|
|
|
|
case 1: imagegif($this->gd, $filename); break;
|
|
|
|
case 2: imagejpeg($this->gd, $filename, $this->quality); break;
|
|
|
|
|
|
|
|
// case 3, and everything else
|
|
|
|
default:
|
|
|
|
// Save them as 8-bit images
|
|
|
|
// imagetruecolortopalette($this->gd, false, 256);
|
|
|
|
imagepng($this->gd, $filename); break;
|
|
|
|
}
|
|
|
|
if(file_exists($filename)) @chmod($filename,0664);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|