This PHP script can be used to perform several operations to manipulate images. The main class can perform many types of image manipulation operations, such as resize images, add alpha channel, flip, rotate, add watermark, apply effects like blur, darkness, brightness, contrast, gamma, gray scale, negate, sepia, sketch, smooth, emboss, edge detection, sharpen, soften, and other types of convolution.
<?php
class UploadComponent extends Object {
var $imageData = array();
var $destinationDir = NULL;
var $thumbDestinationDir = NULL;
var $imageSize = array(500,500);
var $cropImageSize = array(90,90);
var $tag = 'width';
var $imageExt = array('jpg','gif','png','bmp','jpeg');
function initialize($arr)
{
$this->imageData = $arr;
}
function isImageFile()
{
$ext = $this->imageExtension();
if(in_array(strtolower($ext),$this->imageExt))
{
return true;
}
else
{
return false;
}
}
function isValidImage()
{
if($this->imageData['error'] == 0 && $this->imageData['name'] <> '')
{
if($this->isImageFile())
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
function imageExtension()
{
return trim(substr($this->imageData['name'],strpos($this->imageData['name'],'.')+1,strlen($this->imageData['name'])));
}
function upload($thumb=false)
{
$this->__upload($this->destinationDir,$this->imageSize[0],$this->imageSize[1]);
if($thumb)
{
$this->__upload($this->thumbDestinationDir,$this->cropImageSize[0],$this->cropImageSize[1]);
}
}
function __upload($filethumb,$Twidth,$Theight)
{
list($width,$height,$type,$attr)=getimagesize($this->imageData['tmp_name']);
switch($type)
{
case 1:
$img = imagecreatefromgif($this->imageData['tmp_name']);
break;
case 2:
$img=imagecreatefromjpeg($this->imageData['tmp_name']);
break;
case 3:
$img=imagecreatefrompng($this->imageData['tmp_name']);
break;
}
if($this->tag == "width") //width contraint
{
$Theight=round(($height/$width)*$Twidth);
}
elseif($this->tag == "height") //height constraint
{
$Twidth=round(($width/$height)*$Theight);
}
else
{
if($width > $height)
$Theight=round(($height/$width)*$Twidth);
else
$Twidth=round(($width/$height)*$Theight);
}
$thumb=imagecreatetruecolor($Twidth,$Theight);
if(imagecopyresampled($thumb,$img,0,0,0,0,$Twidth,$Theight,$width,$height))
{
switch($type)
{
case 1:
imagegif($thumb,$filethumb);
break;
case 2:
imagejpeg($thumb,$filethumb,100);
break;
case 3:
imagepng($thumb,$filethumb);
break;
}
chmod($filethumb,0666);
return true;
}
}
}
?>
&nbsp;
&nbsp;
class UploadComponent extends Object {
var $imageData = array();
var $destinationDir = NULL;
var $thumbDestinationDir = NULL;
var $imageSize = array(500,500);
var $cropImageSize = array(90,90);
var $tag = 'width';
var $imageExt = array('jpg','gif','png','bmp','jpeg');
function initialize($arr)
{
$this->imageData = $arr;
}
function isImageFile()
{
$ext = $this->imageExtension();
if(in_array(strtolower($ext),$this->imageExt))
{
return true;
}
else
{
return false;
}
}
function isValidImage()
{
if($this->imageData['error'] == 0 && $this->imageData['name'] <> '')
{
if($this->isImageFile())
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
function imageExtension()
{
return trim(substr($this->imageData['name'],strpos($this->imageData['name'],'.')+1,strlen($this->imageData['name'])));
}
function upload($thumb=false)
{
$this->__upload($this->destinationDir,$this->imageSize[0],$this->imageSize[1]);
if($thumb)
{
$this->__upload($this->thumbDestinationDir,$this->cropImageSize[0],$this->cropImageSize[1]);
}
}
function __upload($filethumb,$Twidth,$Theight)
{
list($width,$height,$type,$attr)=getimagesize($this->imageData['tmp_name']);
switch($type)
{
case 1:
$img = imagecreatefromgif($this->imageData['tmp_name']);
break;
case 2:
$img=imagecreatefromjpeg($this->imageData['tmp_name']);
break;
case 3:
$img=imagecreatefrompng($this->imageData['tmp_name']);
break;
}
if($this->tag == "width") //width contraint
{
$Theight=round(($height/$width)*$Twidth);
}
elseif($this->tag == "height") //height constraint
{
$Twidth=round(($width/$height)*$Theight);
}
else
{
if($width > $height)
$Theight=round(($height/$width)*$Twidth);
else
$Twidth=round(($width/$height)*$Theight);
}
$thumb=imagecreatetruecolor($Twidth,$Theight);
if(imagecopyresampled($thumb,$img,0,0,0,0,$Twidth,$Theight,$width,$height))
{
switch($type)
{
case 1:
imagegif($thumb,$filethumb);
break;
case 2:
imagejpeg($thumb,$filethumb,100);
break;
case 3:
imagepng($thumb,$filethumb);
break;
}
chmod($filethumb,0666);
return true;
}
}
}
?>
&nbsp;
&nbsp;