(Advanced article) PHP Common image processing class

Source: Internet
Author: User
Tags explode
(Advanced article) PHP common image processing Class

<?php/* Known issues: 1. In the picture Zoom feature, 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 images zoomed out is too small * * *type value: * (1): Represents the use of the image zoom function, at this point, $value 1 represents the width of the scaled picture, $value 2 represents the height of the scaled picture * (2): Represents the use of the picture clipping function, at this point, $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 for the width and height of the crop, the same 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 of the file name, $value 2 for the watermark in the picture position, there are 10 values can be selected, 1 for the left, 2 for 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 lower right, 0 for the random position * */class image{private $types;//Use the function number, 1 for the picture zoom function 2 for the picture clipping function 3, for the picture and image watermark function private $imgtype;//Picture format private $image; Picture resources private $width;//Picture width private $height;//Picture height private $value 1;//depending on the type value passed, $value 1 represents a different value for the private $value 2;// Depending on the type value passed, $value 2 represents a different value, private $endaddress;//output 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 () {///depending on the type value passed in, output a different function 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 picture watermark function//Use function to get the length and width of the 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]); Watermark Imagecopy ($this->image, $this->imagesources ($this->value1), $positionarr [0], $positionarr [1], 0, 0, $ Imagearrs[0], $imagearrs [1]); Call the output method to save the $this->output ($this->image); The private Function clipping () {///Picture clipping function//assigns the passed in values 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) {//This judgment is to limit not to intercept the image outside to return false;}//Create New The canvas resource $newimg =imagecreatetruecolor ($dst _w, $dst _h); To crop the IMAGecopyresampled ($newimg, $this->image, 0, 0, $src _x, $src _y, $dst _w, $dst _h, $dst _w, $dst _h); Call the output method to save the $this->output ($newimg); } Private Function Scaling () {//Picture zoom function//Get equal width and height $this-proimagesize ()///////////////////////////////////////// Output ($this->imagescaling ()); The Private Function imagesources ($imgad) {//Gets the picture type and opens the 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 retur N Imagesy ($this->image); } Private Function Proimagesize () {//Calculate the width and height of the picture as compared to the scaled ($this->value1 && ($this->width < $this Height) {//equal to the 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, returns 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-& Gt;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 switch ($num) {Case 1: $positionarr [0]=0; $] of the inserted image 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);}} ?>

Above is (Advanced article) PHP commonly used image processing class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.