PHP Image Processing Class

Source: Internet
Author: User
Tags php class rtrim

<?php
/**
File:image.class.php class name is image
Image processing class, you can complete the various types of images to zoom, add picture watermark and clipping operation.
Http://www.lai18.com
*/
Class Image {
/* Picture saved path */
Private $path;

/**
* Instance Image object when passing an image of a path, the default value is the current directory
* @param string $path can specify the path to process the picture
*/
function __construct ($path = "./") {
$this->path = RTrim ($path, "/"). " /";
}

/**
* Zoom to the specified image
* @param string $name is the name of the picture that needs to be processed
* @param int $width the width after zooming
* @param int $height the height after scaling
* @param string $QZ is the prefix of the new picture
* @return Mixed is the image name after scaling, the failure returns false;
*/
function Thumb ($name, $width, $height, $QZ = "Th_") {
/* Get picture width, height, and type information */
$imgInfo = $this->getinfo ($name);
/* Resources for background images */
$SRCIMG = $this->getimg ($name, $imgInfo);
/* Get new picture size */
$size = $this->getnewsize ($name, $width, $height, $imgInfo);
/* Get a new picture resource */
$NEWIMG = $this->kidofimage ($srcImg, $size, $imgInfo);
/* Save the thumbnail image and return the name of the new thumbnail using the private method of this class, prefixed with "th_" */
return $this->createnewimage ($NEWIMG, $qz. $name, $imgInfo);
}

/**
* Add a watermark to a picture
* @param string $groundName background image, that is, images that need to be watermarked, only support gif,jpg,png format temporarily.
* @param string $waterName image watermark, that is, as a watermark image, temporarily support gif,jpg,png format only
* @param int $waterPos watermark position, there are 10 kinds of states, 0 is random position;
* 1 for the top left, 2 for the top center, 3 for the top right;
* 4 for the middle of the left, 5 for the Middle center, 6 for the middle right;
* 7 for the bottom of the left, 8 for the bottom center, 9 for the bottom of the right;
* @param string $QZ The file name of the image after the watermark is prefixed with the original file name
* @return Mixed is the name of the image after the watermark is generated, the failure returns false
*/
function WaterMark ($groundName, $waterName, $waterPos =0, $QZ = "Wa_") {
/* Gets the watermark picture is the current path, or the path is specified */
$curpath = RTrim ($this->path, "/"). " /";
$dir = DirName ($waterName);
if ($dir = = ".") {
$wpath = $curpath;
}else{
$wpath = $dir. " /";
$waterName = basename ($waterName);
}
/* watermark picture and background image must be present */
if (File_exists ($curpath. $groundName) && file_exists ($wpath. $waterName)) {
$groundInfo = $this->getinfo ($groundName); Get background information
$waterInfo = $this->getinfo ($waterName, $dir); Get watermark Picture Information
/* If the background is smaller than the watermark picture, it will be covered by the watermark */
if (! $pos = $this->position ($groundInfo, $waterInfo, $waterPos)) {
Echo ' watermark should not be smaller than the background image! ‘;
return false;
}

$GROUNDIMG = $this->getimg ($groundName, $groundInfo); Get Background Image Resources
$WATERIMG = $this->getimg ($waterName, $waterInfo, $dir); Get Watermark Picture Resource

/* Call the private method to copy the watermark image to the background image at the specified location */
$GROUNDIMG = $this->copyimage ($groundImg, $WATERIMG, $pos, $waterInfo);
/* With this class of private methods, save the Add water picture and return the name of the new picture, the default is "Wa_" prefix */
return $this->createnewimage ($GROUNDIMG, $qz. $groundName, $groundInfo);

}else{
echo ' picture or watermark picture does not exist! ‘;
return false;
}
}

/**
* Crop a picture of a specified area in a large background image
* @param string $name A background picture to cut
* @param int $x cut the position of the picture to the left
* @param int $y The position at the top of the cut picture
* @param int $width width of picture clipping
* @param int $height height of picture clipping
* @param string $QZ The name prefix of the new picture
* @return Mixed the image name after clipping, the failure returns false;
*/
function Cut ($name, $x, $y, $width, $height, $QZ = "Cu_") {
$imgInfo = $this->getinfo ($name); Get picture information
/* Cropped position cannot exceed background image range */
if (($x + $width) > $imgInfo [' width ']) | | (($y + $height) > $imgInfo [' Height ']) {
echo "Clipping position is beyond the background image range!";
return false;
}

$back = $this->getimg ($name, $imgInfo); Get Picture Resources
/* Create a resource that can save the cropped picture */
$cutimg = Imagecreatetruecolor ($width, $height);
/* Crop the picture using the imagecopyresampled () function */
Imagecopyresampled ($cutimg, $back, 0, 0, $x, $y, $width, $height, $width, $height);
Imagedestroy ($back);
/* Save the clipping chart and return the name of the new picture through the private method of this class, prefixed by "cu_" */
return $this->createnewimage ($cutimg, $qz. $name, $imgInfo);
}

/* Private method used internally to determine the location of the watermark image */
Private function position ($groundInfo, $waterInfo, $waterPos) {
/* Images that need to be watermarked are smaller in length or width than watermarks, and cannot generate watermarks */
if ($groundInfo ["width"]< $waterInfo ["width"]) | | ($groundInfo ["Height"]< $waterInfo ["height]]) {
return false;
}
Switch ($waterPos) {
Case 1://1 for top left
$posX = 0;
$posY = 0;
Break
Case 2://2 is centered on the top
$posX = ($groundInfo ["width"]-$waterInfo ["width"])/2;
$posY = 0;
Break
Case 3://3 for top right
$posX = $groundInfo ["width"]-$waterInfo ["width"];
$posY = 0;
Break
Case 4://4 for the middle left
$posX = 0;
$posY = ($groundInfo ["height"]-$waterInfo ["height"])/2;
Break
Case 5://5 Middle Center
$posX = ($groundInfo ["width"]-$waterInfo ["width"])/2;
$posY = ($groundInfo ["height"]-$waterInfo ["height"])/2;
Break
Case 6://6 for the middle right
$posX = $groundInfo ["width"]-$waterInfo ["width"];
$posY = ($groundInfo ["height"]-$waterInfo ["height"])/2;
Break
Case 7://7 for bottom left
$posX = 0;
$posY = $groundInfo ["height"]-$waterInfo ["height"];
Break
Case 8://8 is centered on the bottom
$posX = ($groundInfo ["width"]-$waterInfo ["width"])/2;
$posY = $groundInfo ["height"]-$waterInfo ["height"];
Break
Case 9://9 for bottom right
$posX = $groundInfo ["width"]-$waterInfo ["width"];
$posY = $groundInfo ["height"]-$waterInfo ["height"];
Break
Case 0:
Default://Random
$posX = rand (0, ($groundInfo ["width"]-$waterInfo ["width"]);
$posY = rand (0, ($groundInfo ["height"]-$waterInfo ["height"]);
Break
}
Return Array ("PosX" = $posX, "PosY" and "= $posY");
}

/* Private method used internally to get the picture's property information (width, height, and type) */
Private Function GetInfo ($name, $path = ".") {
$spath = $path = = "."? RTrim ($this->path, "/"). " /": $path. ' /‘;
$data = getimagesize ($spath. $name);
$imgInfo ["width"] = $data [0];
$imgInfo ["height"] = $data [1];
$imgInfo ["type"] = $data [2];

return $imgInfo;
}

/* Private method used internally to create resources that support various image formats (Jpg,gif,png three) */
Private Function Getimg ($name, $imgInfo, $path = '. ') {
$spath = $path = = "."? RTrim ($this->path, "/"). " /": $path. ' /‘;
$srcPic = $spath. $name;
Switch ($imgInfo ["type"]) {
Case 1://gif
$img = Imagecreatefromgif ($srcPic);
Break
Case 2://jpg
$img = Imagecreatefromjpeg ($srcPic);
Break
Case 3://png
$img = Imagecreatefrompng ($srcPic);
Break
Default
return false;
Break
}
return $img;
}

/* Private method used internally, returns the width and height of the scaled image, if the original is less than the size of the zoom */
Private Function Getnewsize ($name, $width, $height, $imgInfo) {
$size ["width"] = $imgInfo ["width"]; The width of the original picture
$size ["height"] = $imgInfo ["Height"]; The height of the original picture
if ($width < $imgInfo ["width"]) {
$size ["width"]= $width; The width of the zoom is reset if it is smaller than the original.
}
if ($height < $imgInfo ["height"]) {
$size ["height"] = $height; The height of the zoom is reset if it is smaller than the original.
}
/* Equal scaling algorithm */
if ($imgInfo ["width"]* $size ["width"] > $imgInfo ["Height"] * $size ["height"]) {
$size ["height"] = round ($imgInfo ["Height"]* $size ["width"]/$imgInfo ["width]];
}else{
$size ["width"] = round ($imgInfo ["width"]* $size ["Height"]/$imgInfo ["height"]);
}
return $size;
}

/* Private method used internally to save the image and preserve the original picture format */
Private Function Createnewimage ($NEWIMG, $newName, $imgInfo) {
$this->path = RTrim ($this->path, "/"). " /";
Switch ($imgInfo ["type"]) {
Case 1://gif
$result = Imagegif ($newImg, $this->path. $newName);
Break
Case 2://jpg
$result = imagejpeg ($newImg, $this->path. $newName);
Break
Case 3://png
$result = Imagepng ($newImg, $this->path. $newName);
Break
}
Imagedestroy ($NEWIMG);
return $newName;
}

/* Private method used internally, to copy images when Watermark is added */
Private Function Copyimage ($GROUNDIMG, $WATERIMG, $pos, $waterInfo) {
Imagecopy ($GROUNDIMG, $WATERIMG, $pos ["PosX"], $pos ["PosY"], 0, 0, $waterInfo ["width"], $waterInfo ["height"]);
Imagedestroy ($WATERIMG);
return $GROUNDIMG;
}

/* Private method used internally, handle the picture with transparency intact */
Private Function Kidofimage ($SRCIMG, $size, $imgInfo) {
$NEWIMG = Imagecreatetruecolor ($size ["width"], $size ["height"]);
$OTSC = Imagecolortransparent ($SRCIMG);
if ($otsc >= 0 && $otsc < imagecolorstotal ($srcImg)) {
$transparentcolor = Imagecolorsforindex ($srcImg, $OTSC);
$newtransparentcolor = Imagecolorallocate (
$NEWIMG,
$transparentcolor [' Red '],
$transparentcolor [' Green '],
$transparentcolor [' Blue ']
);
Imagefill ($newImg, 0, 0, $newtransparentcolor);
Imagecolortransparent ($NEWIMG, $newtransparentcolor);
}
Imagecopyresized ($NEWIMG, $srcImg, 0, 0, 0, 0, $size ["width"], $size ["height"], $imgInfo ["width"], $imgInfo ["height"]);
Imagedestroy ($SRCIMG);
return $NEWIMG;
}
}

PHP Image Processing Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.