Currently only three functions are implemented: 1: Image Zoom, 2: Picture clipping, 3: Add picture watermark
In instantiation, different functions are implemented by passing different values to the second parameter
Copy CodeThe code is as follows:
Include "image.class.php";
$image =new Image ("2.png", 1, "+", "$", "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 Picture watermark feature
$image->outimage ();
?>
PHP code
Copy CodeThe code is as follows:
/* 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/324918.html www.bkjia.com true http://www.bkjia.com/PHPjc/324918.html techarticle currently only three functions are implemented: 1: Image Zoom, 2: Picture clipping, 3: Plus picture watermark in the instantiation, by giving the second parameter a different value, so that the different functions of replication ...