--- /dev/null
+<?php
+
+class IPF_Image
+{
+ private $image, $width, $height, $type, $path;
+
+ public static function load($path)
+ {
+ $imageInfo = getimagesize($path);
+ if (!$imageInfo)
+ throw new Exception('Cannot open '.$path.' image file');
+
+ $type = $imageInfo[2];
+
+ if ($type == IMAGETYPE_JPEG)
+ $image = imagecreatefromjpeg($path);
+ else if ($type == IMAGETYPE_GIF)
+ $image = imagecreatefromgif($path);
+ else if ($type == IMAGETYPE_PNG)
+ $image = imagecreatefrompng($path);
+ else
+ throw new Exception('Unknown image format '.$path);
+
+ return new IPF_Image($image, $imageInfo[0], $imageInfo[1], $type, $path);
+ }
+
+ public static function create($width, $height)
+ {
+ $image = imagecreatetruecolor($width, $height);
+ return new IPF_Image($image, $width, $height);
+ }
+
+ public function __construct($image, $width=null, $height=null, $type=null, $path=null)
+ {
+ $this->image = $image;
+ $this->width = ($width !== null) ? $width : imagesx($image);
+ $this->height = ($height !== null) ? $height : imagesy($image);
+ $this->type = $type;
+ $this->path = $path;
+ }
+
+ public function __destruct()
+ {
+ imagedestroy($this->image);
+ }
+
+ private static function detectType($filename)
+ {
+ if (preg_match('/\.je?pg$/i', $filename))
+ return IMAGETYPE_JPEG;
+ if (preg_match('/\.gif$/i', $filename))
+ return IMAGETYPE_GIF;
+ if (preg_match('/\.png$/i', $filename))
+ return IMAGETYPE_PNG;
+ return null;
+ }
+
+ public function save($filename=null)
+ {
+ $type = null;
+ if ($filename) {
+ $type = self::detectType($filename);
+ } else {
+ $filename = $this->path;
+ if ($this->type)
+ $type = $this->type;
+ else
+ $type = self::detectType($filename);
+ }
+
+ if (!$filename)
+ throw new Exception('No filename given.');
+
+ if ($type == IMAGETYPE_JPEG)
+ imagejpeg($this->image, $filename);
+ else if ($type == IMAGETYPE_GIF)
+ imagegif($this->image, $filename);
+ else if ($type == IMAGETYPE_PNG)
+ imagepng($this->image, $filename);
+ else
+ throw new Exception('Unknown file type.');
+ }
+
+ private function color($color)
+ {
+ return imagecolorallocatealpha($this->image,
+ ($color >> 16) & 0xFF,
+ ($color >> 8) & 0xFF,
+ $color & 0xFF,
+ ($color >> 24) & 0x7F);
+ }
+
+ public function fill($x, $y, $width, $height, $color)
+ {
+ imagefilledrectangle($this->image, $x, $y, $width, $height, $this->color($color));
+ }
+
+ public function rotate($angle, $color=0x7F000000)
+ {
+ $image = imagerotate($this->image, $angle, $color);
+ return new IPF_Image($image);
+ }
+
+ public function copy(IPF_Image $image, $dstX, $dstY)
+ {
+ imagecopy($this->image, $image->image, $dstX, $dstY, 0, 0, $image->width, $image->height);
+ }
+
+ public function copyPart(IPF_Image $image, $dstX, $dstY, $srcX, $srcY, $width, $height)
+ {
+ imagecopy($this->image, $image->image, $dstX, $dstY, $srcX, $srcY, $width, $height);
+ }
+
+ public function copyScale(IPF_Image $image, $dstX=0, $dstY=0, $dstWidth=null, $dstHeight=null, $srcX=0, $srcY=0, $srcWidth=null, $srcHeight=null)
+ {
+ if ($dstWidth === null)
+ $dstWidth = $this->width;
+ if ($dstHeight === null)
+ $dstHeight = $this->height;
+ if ($srcWidth === null)
+ $srcWidth = $image->width;
+ if ($srcHeight === null)
+ $srcHeight = $image->height;
+ imagecopyresampled($this->image, $image->image, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
+ }
+
+ public function diagonalWatermark(IPF_Image $watermark)
+ {
+ $result = IPF_Image::create($this->width, $this->height);
+ $result->copy($this, 0, 0);
+
+ $w_repeat = ceil(hypot($this->width, $this->height) / (float)$watermark->width);
+ $angle = atan2($this->height, $this->width);
+ $wmr = $watermark->rotate(-$angle * 180 / M_PI);
+
+ $dx = $watermark->width * cos($angle);
+ $dy = $watermark->width * sin($angle);
+
+ for ($i = 0; $i < $w_repeat; ++$i) {
+ $result->copy($wmr, $dx * ($i - 0.5), $dy * ($i - 0.5));
+ }
+ return $result;
+ }
+
+ public function fitWidth($width, $expand=true, $shrink=true)
+ {
+ if ($this->width == $width)
+ return $this;
+ if (!$expand && $this->width < $width)
+ return $this;
+ if (!$shrink && $this->width > $width)
+ return $this;
+
+ $height = $width * $this->height / $this->width;
+
+ $result = IPF_Image::create($width, $height);
+ $result->copyScale($this);
+ return $result;
+ }
+
+ public function fitHeight($height, $expand=true, $shrink=true)
+ {
+ if ($this->height == $height)
+ return $this;
+ if (!$expand && $this->height < $height)
+ return $this;
+ if (!$shrink && $this->height > $height)
+ return $this;
+
+ $width = $height * $this->width / $this->height;
+
+ $result = IPF_Image::create($width, $height);
+ $result->copyScale($this);
+ return $result;
+ }
+
+ public function fit($width, $height, $expand=true, $shrink=true)
+ {
+ if (!$expand && $this->width < $width && $this->height < $height)
+ return $this;
+ if (!$shrink && $this->width > $width && $this->height > $height)
+ return $this;
+
+ if ($this->height * $width >= $this->width * $height) {
+ $w = $height * $this->width / $this->height;
+ $h = $height;
+ } else {
+ $w = $width;
+ $h = $width * $this->height / $this->width;
+ }
+
+ $result = IPF_Image::create($w, $h);
+ $result->copyScale($this);
+ return $result;
+ }
+
+ public function thumbnailCrop($width, $height, $gravityX=0.5, $gravityY=0.5)
+ {
+ if ($this->height * $width >= $this->width * $height) {
+ $w = $this->width;
+ $h = $this->width * $height / $width;
+ } else {
+ $w = $this->height * $width / $height;
+ $h = $this->height;
+ }
+ $x = ($this->width - $w) * $gravityX;
+ $y = ($this->height - $h) * $gravityY;
+
+ $result = IPF_Image::create($width, $height);
+ $result->copyScale($this, 0, 0, $width, $height, $x, $y, $w, $h);
+ return $result;
+ }
+
+ public function thumbnailFill($width, $height, $color=0x7F000000, $gravityX=0.5, $gravityY=0.5)
+ {
+ if ($this->height * $width >= $this->width * $height) {
+ $w = $height * $this->width / $this->height;
+ $h = $height;
+ } else {
+ $w = $width;
+ $h = $width * $this->height / $this->width;
+ }
+ $x = ($width - $w) * $gravityX;
+ $y = ($height - $h) * $gravityY;
+
+ $result = IPF_Image::create($width, $height);
+ $result->fill(0, 0, $width, $height, $color);
+ $result->copyScale($this, $x, $y, $w, $h);
+ return $result;
+ }
+}
+
+++ /dev/null
-<?php
-
-class IPF_Image_Thumbnail {
-
- protected $Source, $Thumbnail;
- protected $ThumbnailWidth, $ThumbnailHeight;
- protected $SourceWidth, $SourceHeight, $SourceType;
- protected $file_permission, $dir_permission;
- protected $sourceRemove;
-
- public function __construct($source, $width=null, $height=null, $thumbnail=null, $sourceRemove=false, $dir_permission=null, $file_permission=null){
- $this->Source = $source;
- if ($thumbnail)
- $this->Thumbnail = $thumbnail;
- else
- $this->Thumbnail = $source;
-
- if (($width==null) && ($height==null))
- throw new IPF_Exception_Image(__('Please Specify width or height'));
-
- $this->ThumbnailWidth = $width;
- $this->ThumbnailHeight = $height;
-
- if ($dir_permission)
- $this->dir_permission = $dir_permission;
- else
- $this->dir_permission = IPF::get('dir_permission');
-
- if ($file_permission)
- $this->file_permission = $file_permission;
- else
- $this->file_permission = IPF::get('file_permission');
-
- $this->sourceRemove = $sourceRemove;
- }
-
- public function execute($keepTransparency=false){
- $ImageInfo = getimagesize($this->Source);
- if(!$ImageInfo)
- throw new IPF_Exception_Image(sprintf(__('Cannot open %s image file'), $this->Source));
-
- $this->SourceWidth = $ImageInfo[0];
- $this->SourceHeight = $ImageInfo[1];
- $this->SourceType = $ImageInfo[2];
-
- if($this->SourceType==IMAGETYPE_JPEG)
- $im = ImageCreateFromJPEG($this->Source);
- else if($this->SourceType==IMAGETYPE_GIF)
- $im = ImageCreateFromGIF($this->Source);
- else if($this->SourceType==IMAGETYPE_PNG)
- $im = ImageCreateFromPNG($this->Source);
- else
- throw new IPF_Exception_Image(sprintf(__('Unknown image format %s'), $this->Source));
-
- if($this->ThumbnailWidth)
- $c1 = $this->SourceWidth/abs($this->ThumbnailWidth);
- else
- $c1 = 0;
-
- if($this->ThumbnailHeight)
- $c2 = $this->SourceHeight/abs($this->ThumbnailHeight);
- else
- $c2 = 0;
-
- $c = $c1>$c2 ? $c1 : $c2;
-
- if($c<=1){
- $this->ThumbnailWidth = $this->SourceWidth;
- $this->ThumbnailHeight = $this->SourceHeight;
- if($this->Source<>$this->Thumbnail)
- if (!@copy($this->Source, $this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot copy %s to %s'), $this->Source, $this->Thumbnail));
- }
- else{
- if($this->ThumbnailWidth<0 and $this->SourceWidth/$c<(-$this->ThumbnailWidth))
- $c = $this->SourceWidth/(-$this->ThumbnailWidth);
- if($this->ThumbnailHeight<0 and $this->SourceHeight/$c<(-$this->ThumbnailHeight))
- $c = $this->SourceHeight/(-$this->ThumbnailHeight);
-
- $this->ThumbnailWidth = $this->SourceWidth/$c;
- $this->ThumbnailHeight = $this->SourceHeight/$c;
-
- $tn = imagecreatetruecolor($this->ThumbnailWidth, $this->ThumbnailHeight);
-
- if ($keepTransparency && $this->SourceType==IMAGETYPE_PNG)
- {
- imagealphablending($tn, false);
- imagefill($tn, 0, 0, imagecolortransparent($tn, imagecolorallocatealpha($tn, 0, 0, 0, 127)));
- imagesavealpha($tn, true);
- }
-
- imagecopyresampled(
- $tn, $im, 0, 0, 0, 0,
- $this->ThumbnailWidth, $this->ThumbnailHeight,
- $this->SourceWidth, $this->SourceHeight
- );
- if ($this->sourceRemove){
- if (!@unlink($this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot delete %s'), $this->Thumbnail));
- }
- $dir_thumbnail = dirName($this->Thumbnail);
- if (!IPF_Utils::makeDirectories(dirName($this->Thumbnail), $this->dir_permission))
- throw new IPF_Exception_Image(sprintf(__('Cannot create path %s'), $dir_thumbnail));
-
- if($this->SourceType==IMAGETYPE_JPEG){
- if (!ImageJPEG($tn, $this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot create JPEG %s'), $this->Thumbnail));
- }
- else if($this->SourceType==IMAGETYPE_GIF){
- if (!ImageGIF($tn, $this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot create GIF %s'), $this->Thumbnail));
- }
- else if($this->SourceType==IMAGETYPE_PNG){
- if (!ImagePNG($tn, $this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot create PNG %s'), $this->Thumbnail));
- }
- else
- throw new IPF_Exception_Image(sprintf(__('Unknown image format %s'), $this->Source));
-
- if (!@chmod($this->Thumbnail, $this->file_permission))
- throw new IPF_Exception_Image(sprintf(__('Cannot change permission %s'), $this->Thumbnail));
- }
- }
-
- public function execDumb(){
- $ImageInfo = getimagesize($this->Source);
- if(!$ImageInfo)
- throw new IPF_Exception_Image(sprintf(__('Cannot open %s image file'), $this->Source));
-
- $this->SourceWidth = $ImageInfo[0];
- $this->SourceHeight = $ImageInfo[1];
- $this->SourceType = $ImageInfo[2];
-
- if($this->SourceType==IMAGETYPE_JPEG)
- $im = ImageCreateFromJPEG($this->Source);
- else if($this->SourceType==IMAGETYPE_GIF)
- $im = ImageCreateFromGIF($this->Source);
- else if($this->SourceType==IMAGETYPE_PNG)
- $im = ImageCreateFromPNG($this->Source);
- else
- throw new IPF_Exception_Image(sprintf(__('Unknown image format %s'), $this->Source));
-
- $tn = imagecreatetruecolor($this->ThumbnailWidth, $this->ThumbnailHeight);
-
- imagecopyresampled(
- $tn, $im, 0, 0, 0, 0,
- $this->ThumbnailWidth, $this->ThumbnailHeight,
- $this->SourceWidth, $this->SourceHeight
- );
-
- if ($this->sourceRemove){
- if (!@unlink($this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot delete %s'), $this->Thumbnail));
- }
- $dir_thumbnail = dirName($this->Thumbnail);
- if (!IPF_Utils::makeDirectories(dirName($this->Thumbnail), $this->dir_permission))
- throw new IPF_Exception_Image(sprintf(__('Cannot create path %s'), $dir_thumbnail));
-
- if($this->SourceType==IMAGETYPE_JPEG){
- if (!ImageJPEG($tn, $this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot create JPEG %s'), $this->Thumbnail));
- }
- else if($this->SourceType==IMAGETYPE_GIF){
- if (!ImageGIF($tn, $this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot create GIF %s'), $this->Thumbnail));
- }
- else if($this->SourceType==IMAGETYPE_PNG){
- if (!ImagePNG($tn, $this->Thumbnail))
- throw new IPF_Exception_Image(sprintf(__('Cannot create PNG %s'), $this->Thumbnail));
- }
- else
- throw new IPF_Exception_Image(sprintf(__('Unknown image format %s'), $this->Source));
-
- if (!@chmod($this->Thumbnail, $this->file_permission))
- throw new IPF_Exception_Image(sprintf(__('Cannot change permission %s'), $this->Thumbnail));
- }
-}
-?>