PHP Image processing class code sharing _php skills

Source: Internet
Author: User
Currently only implemented three functions: 1: Picture scaling, 2: Picture cropping, 3: Add image watermark
In instantiation, different functions are achieved by passing a different value to the second parameter
Copy Code code as follows:

<?php
Include "image.class.php";
$image =new Image ("2.png", 1, "the", "the", "5.png"); Using the picture zoom feature
$image =new Image ("2.png", 2, "0,0", "50,50", "5.png"); Using the picture clipping feature
$image =new Image ("2.png", 3, "1.png", "0", "5.png"); Use the Add image watermark function
$image->outimage ();
?>

PHP code
Copy Code code as follows:

<?php
/* Known Issues: 1. In the picture scaling feature, use the Imagecreatetruecolor function to create a canvas and use a transparent processing algorithm, but the picture in PNG format cannot be transparent. Creating a canvas with the Imagecreate function solves this problem, but there are too few picture colors to scale out
*
*
*type Value:
* (1): On behalf of the image zoom function, at this point, $value 1 represents the width of the picture after zooming, $value 2 represents the height of the picture after zooming
* (2): Represents the use of the picture cropping feature, at which point the $value 1 represents the coordinates of the crop start point, for example: "0,0" is preceded by the x axis followed by the y-axis, intermediate, delimited, $value 2 represents the width and height of the crop and is also used as "20,20"
* (3): On behalf of the use of the image watermark function, at this time, $value 1 represents the image of the watermark file name, $value 2 represents the position of the watermark in the picture, there are 10 values to choose from, 1 represents the left, 2 represents the left, 3 is about, 4 represents the left, 5 represents in the middle, 6 in the right, 7 for the next, 8 represents the lower right, and 0 represents the random position.
*
*/
Class image{
Private $types; Use the function number, 1 for the picture zoom function 2 for the picture clipping function 3, the picture adds the 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;//Depending on the type value being passed, $value 1 represents different values, respectively.
Private $value 2;//Depending on the type value being passed, $value 2 represents different values, respectively.
Private $endaddress//output after the address + filename
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 functionality 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
Using functions to get the length and width of the watermark file
$imagearrs = $this->getimagearr ($this->value1);
Call function to calculate 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 Save
$this->output ($this->image);
}
Private Function clipping () {//Picture clipping function
Assigns the incoming value to the variable
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 restriction 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 Save
$this->output ($NEWIMG);
}
Private Function Scaling () {//Picture zoom function
Gets the width and height of the equal ratio scaling
$this-> proimagesize ();
Scale according to 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 () {//calculation, etc. than scaled picture's width and height
if ($this->value1 && ($this->width < $this->height)) {//Equal 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 scaling function, returning the processed image resource
$newimg =imagecreatetruecolor ($this->value1, $this->value2);
$tran =imagecolortransparent ($this->image);//Process 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) {//Export 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 Property array method
Return getimagesize ($imagesou);
}
Private function position ($num, $width, $height) {//returns the coordinates of a position based on the incoming number, $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);
}
}
?>

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.