PHP common Image processing Class _php tutorial

Source: Internet
Author: User
We often in the operation of the image will have a watermark thumbnail and watermark can be assigned to the optional, 1 for the left, 2 for the left, 3 for about, 4 for the middle left, 5 for middle, 6 for the right, 7 for the next, 8 for the next, 9 for the next right, 0 for the random position
The code is as follows Copy Code

/* Known Issues: 1. In the picture zoom function, use the Imagecreatetruecolor function to create the canvas and use the transparent processing algorithm, but the PNG format picture is not transparent. Creating a canvas with the Imagecreate function solves this problem, but the number of image colors is too small to scale
*
*
*type Value:
* (1): Represents the use of the image zoom function, at this time, $value 1 represents the width of the scaled picture, $value 2 for the height of the image after zooming
* (2): Represents the use of the picture clipping function, at this time, the $value 1 represents the coordinates of the clipping start point, example: From the origin point is "0,0" is preceded by the x axis is the y axis, the middle with, separated, $value 2 means the width and height of the crop, also is the form of "20,20" use
* (3): Represents the use of the image watermark function, at this time, $value 1 for the watermark image file name, $value 2 for the watermark in the picture position, there are 10 values can be selected, 1 for the left, 2 for the left, 3 for around, 4 for the left, 5 for the middle, 6 for the right, 7 for the next, 8 for next, 9 for lower right, 0 for random position
*
*/
Class image{
Private $types; Use the function number, 1 for the picture zoom function 2 for the picture clipping function 3, for picture and picture watermark function
private $imgtype;//The format of the picture
Private $image; Picture Resources
private $width;//Picture width
private $height;//Picture height
Private $value 1;//$value 1 represents a different value depending on the type value passed
Private $value 2;//$value 2 represents a different value depending on the type value passed
private $endaddress;//Address after output + file name
function __construct ($imageaddress, $types, $value 1= "", $value 2= "", $endaddress) {
$this->types= $types;
$this->image= $this->imagesources ($imageaddress);
$this->width= $this->imagesizex ();
$this->height= $this->imagesizey ();
$this->value1= $value 1;
$this->value2= $value 2;
$this->endaddress= $endaddress;
}
function Outimage () {//output different functions depending on the type value passed in
Switch ($this->types) {
Case 1:
$this->scaling ();
Break
Case 2:
$this->clipping ();
Break
Case 3:
$this->imagewater ();
Break
Default
return false;
}
}
Private Function Imagewater () {//Add image watermark function
Use function to get the length and width of watermark file
$imagearrs = $this->getimagearr ($this->value1);
The calling function calculates where the watermark is loaded
$positionarr = $this->position ($this->value2, $imagearrs [0], $imagearrs [1]);
Add watermark
Imagecopy ($this->image, $this->imagesources ($this->value1), $positionarr [0], $positionarr [1], 0, 0, $ Imagearrs[0], $imagearrs [1]);
Call output method to save
$this->output ($this->image);
}
Private Function clipping () {//Picture clipping function
Assigns the passed in values to the variable separately
List ($src _x, $src _y) =explode (",", $this->value1);
List ($dst _w, $dst _h) =explode (",", $this->value2);
if ($this->width < $src _x+ $DST _w | | $this->height < $SRC _y+ $dst _h) {//The judgment is that the limit cannot be intercepted outside the picture
return false;
}
Create a new canvas resource
$newimg =imagecreatetruecolor ($dst _w, $dst _h);
to crop
Imagecopyresampled ($newimg, $this->image, 0, 0, $src _x, $src _y, $dst _w, $dst _h, $dst _w, $dst _h);
Call output method to save
$this->output ($NEWIMG);
}
Private Function Scaling () {//Picture zoom function
Get the width and height of equal to zoom
$this-Proimagesize ();
Scale according to the parameters and call the output function to save the processed file
$this->output ($this->imagescaling ());
}
Private Function Imagesources ($imgad) {//Get picture type and open image resource
$imagearray = $this->getimagearr ($imgad);
Switch ($imagearray [2]) {
Case 1://gif
$this->imgtype=1;
$img =imagecreatefromgif ($imgad);
Break
Case 2://jpeg
$this->imgtype=2;
$img =imagecreatefromjpeg ($imgad);
Break
Case 3://png
$this->imgtype=3;
$img =imagecreatefrompng ($imgad);
Break
Default
return false;
}
return $img;
}
Private Function Imagesizex () {//Get picture width
Return Imagesx ($this->image);
}
Private Function Imagesizey () {//Get picture height
Return Imagesy ($this->image);
}
Private Function Proimagesize () {//compute and more than scaled picture width and height
if ($this->value1 && ($this->width < $this->height)) {//equal ratio scaling algorithm
$this->value1=round (($this->value2/$this->height) * $this->width);
}else{
$this->value2=round (($this->value1/$this->width) * $this->height);
}
}
Private Function imagescaling () {//Image zoom function, return the processed image resource
$newimg =imagecreatetruecolor ($this->value1, $this->value2);
$tran =imagecolortransparent ($this->image);//processing transparent algorithm
if ($tran >= 0 && $tran < imagecolorstotal ($this->image)) {
$tranarr =imagecolorsforindex ($this->image, $tran);
$newcolor =imagecolorallocate ($newimg, $tranarr [' Red '], $tranarr [' Green '], $tranarr [' Blue '];
Imagefill ($newimg, 0, 0, $newcolor);
Imagecolortransparent ($newimg, $newcolor);
}
Imagecopyresampled ($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this- >height);
return $newimg;
}
Private function Output ($image) {//Output image
Switch ($this->imgtype) {
Case 1:
Imagegif ($image, $this->endaddress);
Break
Case 2:
Imagejpeg ($image, $this->endaddress);
Break
Case 3:
Imagepng ($image, $this->endaddress);
Break
Default
return false;
}
}
Private Function Getimagearr ($imagesou) {//Return image attribute array method
Return getimagesize ($imagesou);
}
Private function position ($num, $width, $height) {//returns the coordinates of a position based on the number passed in, $width and $height represent the width and height of the inserted image, respectively
Switch ($num) {
Case 1:
$positionarr [0]=0;
$positionarr [1]=0;
Break
Case 2:
$positionarr [0]= ($this->width-$width)/2;
$positionarr [1]=0;
Break
Case 3:
$positionarr [0]= $this->width-$width;
$positionarr [1]=0;
Break
Case 4:
$positionarr [0]=0;
$positionarr [1]= ($this->height-$height)/2;
Break
Case 5:
$positionarr [0]= ($this->width-$width)/2;
$positionarr [1]= ($this->height-$height)/2;
Break
Case 6:
$positionarr [0]= $this->width-$width;
$positionarr [1]= ($this->height-$height)/2;
Break
Case 7:
$positionarr [0]=0;
$positionarr [1]= $this->height-$height;
Break
Case 8:
$positionarr [0]= ($this->width-$width)/2;
$positionarr [1]= $this->height-$height;
Break
Case 9:
$positionarr [0]= $this->width-$width;
$positionarr [1]= $this->height-$height;
Break
Case 0:
$positionarr [0]=rand (0, $this->width-$width);
$positionarr [1]=rand (0, $this->height-$height);
Break
}
return $positionarr;
}
function __destruct () {
Imagedestroy ($this->image);
}
}
?>

http://www.bkjia.com/PHPjc/632967.html www.bkjia.com true http://www.bkjia.com/PHPjc/632967.html techarticle we often in the image operation will have to have a watermark thumbnail and watermark can be assigned to the optional, 1 for the left, 2 for the left, 3 for about, 4 for the left, 5 for the middle, 6 for the middle ...

  • 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.