PHP Image manipulation class almost every options in this class.
Following features are included in this PHP class.
<?php
/*
Written by: Jailani M, http://www.W3Lessons.com
w3_image Image Manipulation Class
*/
class w3_image {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 127));
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 127));
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function data_uri($image_type=IMAGETYPE_JPEG) {
imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 127));
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
ob_start();
if( $image_type == IMAGETYPE_JPEG ) {
$mime="image/jpeg";
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
$mime="image/gif";
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
$mime="image/png";
imagepng($this->image);
}
$image_data= ob_get_clean();
$base64=base64_encode($image_data);
return "data:$mime;base64,$base64";
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
function crop($left,$top,$width,$height)
{
// create a blank image having the same width and height as the crop area
// this will be our cropped image
$cropped_image = imagecreatetruecolor($width, $height);
$bgwhite = imagecolorallocatealpha($cropped_image,255,255,255,0);
imagefilledrectangle($cropped_image, 0, 0,$width,$height, $bgwhite);
// copy the crop area from the source image to the blank image created above
imagecopy($cropped_image, $this->image, 0, 0, $left, $top,$width, $height);
$this->image = $cropped_image;
}
function addWatermark($watermark_image,$position="B_RIGHT")
{
// T_LEFT, T_RIGHT, B_LEFT, B_RIGHT
ImageAlphaBlending($this->image, true);
// Same Size PNG Watermark Image with Transparency
$logoImage = ImageCreateFromPNG($watermark_image);
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
if($position == "T_LEFT")
{
$dst_x = 0;
$dst_y = 0;
}elseif($position == "T_RIGHT")
{
$dst_x = $this->getWidth()-$logoW;
$dst_y = 0;
}elseif($position == "B_LEFT")
{
$dst_x = 0;
$dst_y = $this->getHeight()-$logoH;
}elseif($position == "B_RIGHT")
{
$dst_x = $this->getWidth()-$logoW;
$dst_y = $this->getHeight()-$logoH;
}
// were you see the two 1's this is the offset from the top-left hand corner!
ImageCopy($this->image, $logoImage, $dst_x, $dst_y, 0, 0, $logoW, $logoH);
}
function fitToSize($width,$height)
{
// create a blank image having the same width and height fitToSize
// this will be our final image
$fitToImage = imagecreatetruecolor($width, $height);
$bgwhite = imagecolorallocatealpha($fitToImage,255,255,255,0);
imagefilledrectangle($fitToImage, 0, 0,$width,$height, $bgwhite);
// resize image based on fit to size in both height and width
$this->resizeToWidth($width);
if($this->getHeight() > $height)
$this->resizeToHeight($height);
$_x = ($width - $this->getWidth())/2;
$_w = $this->getWidth();
$_y = ($height - $this->getHeight()) / 2;
$_h = $this->getHeight();
imagecopy($fitToImage,$this->image,$_x,$_y,0,0,$_w,$_h);
$this->image = $fitToImage;
}
}