PHP image Processing class instance _php skill

Source: Internet
Author: User
Tags php programming rand

This example describes the PHP image processing class. Share to everyone for your reference. as follows:

<?php/** * Image class */class Image {/** * @var string $fileName filename * @access private/private $fileName = '
 ';
 /** * @var GD Resource $imageResource original image * @access private/private $imageResource = NULL;
 /** * @var int $imageWidth original image wide * @access private/private $imageWidth = NULL;
 /** * @var int $imageHeight original image high * @access private/private $imageHeight = NULL;
 /** * @var int $imageType original image type * @access private/private $imageType = NULL;
 /** * @var int $imageWidth The original image wide * @access private */Public $width = NULL;
 /** * @var int $imageHeight The original image high * @access private */Public $height = NULL;
 /** * @var int $imageType original image type * @access private */Public $type = NULL;
 /** * @var int $newResource New image * @access private/private $newResource = NULL;
 /** * @var int $newResType New Image type * @access private/private $newResType = NULL; /** * constructor * @param string $fileName filename/Public function __construct ($fileName = NULL) {$tHis->filename = $fileName;
 if ($this->filename) {$this->getsrcimageinfo (); /** * Source image information * @access private * @return void */Private Function Getsrcimageinfo () {$info = $this->getima
 Geinfo ();
 $this->imagewidth = $info [0];
 $this->imageheight = $info [1];
 $this->imagetype = $info [2];
 $this->width = $info [0];
 $this->height = $info [1];
 $this->type = $info [2]; /** * Fetching Image information * @param string $fileName filename * @access private * @return Array/Private function Getimageinfo ($fil
 ename = NULL) {if ($fileName ==null) {$fileName = $this->filename;
 } $info = getimagesize ($fileName);
 return $info;  /** * Create source image GD Resources * @access Private * @return void/Private Function createsrcimage () {$this->imageresource
 = $this->createimagefromfile (); /** * with file creation image GD resources * @param string $fileName filename * @return GD resource/Public function Createimagefromfile ($fi Lename = NULL) {if (! $fileName) {$fileName = $this->fIlename;
 $imgType = $this->imagetype; } if (!is_readable ($fileName) | |!file_exists ($fileName)) {throw new Exception (' Unable to open file '). $fileName.
 '"');
 } if (! $imgType) {$imageInfo = $this->getimageinfo ($fileName);
 $imgType = $imageInfo [2];
 Switch ($imgType) {case imagetype_gif: $tempResource = Imagecreatefromgif ($fileName);
 Break
 Case Imagetype_jpeg: $tempResource = Imagecreatefromjpeg ($fileName);
 Break
 Case Imagetype_png: $tempResource = Imagecreatefrompng ($fileName);
 Break
 Case imagetype_wbmp: $tempResource = imagecreatefromwbmp ($fileName);
 Break
 Case IMAGETYPE_XBM: $tempResource = IMAGECREATEFROMXBM ($fileName);
 Break
 Default:throw new Exception (' Unsupport image type ');
 return $tempResource; /** * Change the image size * @param int $width wide * @param int $height high * @param string $flag Generally, the allowed screenshot is 4, and the screenshot is 1; Suppose you require an image that is 4:3 proportional: 4 = If too long automatically delete part of 0 = long width conversion to the parameter specified 1 = proportional scaling, automatic judgment too long or too wide, long width constraints in the parameter specified 2 = width for constraints of 3 = scale with a high constraint * @param string $ bgcolor if not null, the color specified with this parameter is used as the background color, and the image expands to the specified height, which should be an array; * @return string/Public function Resizeimage ($width, $height, $flag =1, $bgcolor =null) {$widthRatio = $width/$this-&
 Gt;imagewidth;
 $heightRatio = $height/$this->imageheight; Switch ($flag) {case 1:if ($this->imageheight < $height && $this->imagewidth < $width) {$endWidt
 H = $this->imagewidth;
 $endHeight = $this->imageheight;
 Return
 } elseif (($this->imageheight * $widthRatio) > $height) {$endWidth = Ceil ($this->imagewidth * $heightRatio);
 $endHeight = $height;
 else {$endWidth = $width;
 $endHeight = ceil ($this->imageheight * $widthRatio);
 } break;
 Case 2: $endWidth = $width;
 $endHeight = ceil ($this->imageheight * $widthRatio);
 Break
 Case 3: $endWidth = ceil ($this->imagewidth * $heightRatio);
 $endHeight = $height;
 Break
 Case 4: $endWidth 2 = $width;
 $endHeight 2 = $height; if ($this->imageheight < $height && $this->imagewidth < $width) {
 $endWidth = $this->imagewidth;
 $endHeight = $this->imageheight;
 Return
 } elseif (($this->imageheight * $widthRatio) < $height) {$endWidth = Ceil ($this->imagewidth * $heightRatio);
 $endHeight = $height;
 else {$endWidth = $width;
 $endHeight = ceil ($this->imageheight * $widthRatio);
 } break;
 Default: $endWidth = $width;
 $endHeight = $height;
 Break
 } if ($this->imageresource==null) {$this->createsrcimage ();
 } if ($bgcolor) {$this->newresource = Imagecreatetruecolor ($width, $height);
 $BG =imagecolorallocate ($this->newresource, $bgcolor [0], $bgcolor [1], $bgcolor [2]);
 Imagefilledrectangle ($this->newresource,0,0, $width, $height, $BG);
 $tox =ceil (($width-$endWidth)/2);
 $toy =ceil (($height-$endHeight)/2);
 if ($tox <0) $tox = 0;
 if ($toy <0) $toy = 0;
 }else if ($flag ==4) {$this->newresource = Imagecreatetruecolor ($endWidth 2, $endHeight 2);
 }else {$this->newresource = Imagecreatetruecolor ($endWidth, $endHeight); } $this->newrestype = $this->imagetype; Imagecopyresampled ($this->newresource, $this->imageresource, $tox, $toy, 0, 0, $endWidth, $endHeight, $this->
 ImageWidth, $this->imageheight); /** * Image watermark * @param string $waterContent The watermark content can be an image file name, or it can be text * @param int $pos position 0-9 can be an array * @param int $textFont Font characters, when the watermark content is text valid * @param string $textColor text color, when the watermark content is text valid * @return string/Public function watermark ($waterConte
 NT, $pos = 0, $textFont =5, $textColor = "#ffffff") {$isWaterImage = file_exists ($waterContent);
 if ($isWaterImage) {$waterImgRes = $this->createimagefromfile ($waterContent);
 $waterImgInfo = $this->getimageinfo ($waterContent);
 $waterWidth = $waterImgInfo [0];
 $waterHeight = $waterImgInfo [1];
 else {$waterText = $waterContent;
 $temp = @imagettfbbox (ceil ($textFont *2.5), 0, "./cour.ttf", $waterContent);
 if ($temp) {$waterWidth = $temp [2]-$temp [6];
 $waterHeight = $temp [3]-$temp [7];
 else {$waterWidth = 100;
 $waterHeight = 12; } if ($this->imaGeresource==null) {$this->createsrcimage (); 
 Switch ($pos) {case 0://random $posX = rand (0, ($this->imagewidth-$waterWidth)); 
 $posY = rand (0, ($this->imageheight-$waterHeight)); 
 Break 
 Case 1://1 is the top left $posX = 0; 
 $posY = 0; 
 Break 
 Case 2://2 is centered on the top $posX = ($this->imagewidth-$waterWidth)/2; 
 $posY = 0; 
 Break 
 Case 3://3 is the top right $posX = $this->imagewidth-$waterWidth; 
 $posY = 0; 
 Break 
 Case 4://4 is the middle left $posX = 0; 
 $posY = ($this->imageheight-$waterHeight)/2; 
 Break 
 Case 5://5 for middle Center $posX = ($this->imagewidth-$waterWidth)/2; 
 $posY = ($this->imageheight-$waterHeight)/2; 
 Break 
 Case 6://6 is the middle right $posX = $this->imagewidth-$waterWidth; 
 $posY = ($this->imageheight-$waterHeight)/2; 
 Break 
 Case 7://7 is the bottom left $posX = 0; 
 $posY = $this->imageheight-$waterHeight; 
 Break 
 Case 8://8 is centered at bottom $posX = ($this->imagewidth-$waterWidth)/2; 
 $posY = $this->imageheight-$waterHeight;Break 
 Case 9://9 is the bottom right $posX = $this->imagewidth-$waterWidth-20; 
 $posY = $this->imageheight-$waterHeight-10; 
 Break 
 default://Random $posX = rand (0, ($this->imagewidth-$waterWidth)); 
 $posY = rand (0, ($this->imageheight-$waterHeight));   
 Break 
 } imagealphablending ($this->imageresource, true);  
 if ($isWaterImage) {imagecopy ($this->imageresource, $waterImgRes, $posX, $posY, 0, 0, $waterWidth, $waterHeight); 
 else {$R = Hexdec (substr ($textColor, 1,2)); 
 $G = Hexdec (substr ($textColor, 3,2)); 
 $B = Hexdec (substr ($textColor, 5));
 $textColor = Imagecolorallocate ($this->imageresource, $R, $G, $B);     
 Imagestring ($this->imageresource, $textFont, $posX, $posY, $waterText, $textColor);
 } $this->newresource = $this->imageresource;
 $this->newrestype = $this->imagetype; /** * Generate a CAPTCHA picture * @param int $width wide * @param string $height high * @param int $length length * @param int $validType 0= number , 1 = letter, 2= number plus letter * @param string $textCOlor text color * @param string $backgroundColor background color * @return void/Public Function imagevalidate ($width, $height, $leng th = 4, $validType = 1, $textColor = ' #000000 ', $backgroundColor = ' #ffffff ') {if ($validType ==1) {$validString = ' abc
 Defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';
 $validLength = 52;
 } elseif ($validType ==2) {$validString = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
 $validLength = 62;
 else {$validString = ' 123456789 ';
 $validLength = 9;
 } srand ((int) time ());
 $valid = ';
 For ($i =0 $i < $length; $i + +) {$valid. = $validString {rand (0, $validLength-1)};
 } $this->newresource = Imagecreate ($width, $height);
 $bgR = Hexdec (substr ($backgroundColor, 1,2));
 $bgG = Hexdec (substr ($backgroundColor, 3,2));
 $bgB = Hexdec (substr ($backgroundColor, 5,2));
 $backgroundColor = Imagecolorallocate ($this->newresource, $bgR, $bgG, $bgB);
 $tR = Hexdec (substr ($textColor, 1,2));
 $tG = Hexdec (substr ($textColor, 3,2)); $tB = Hexdec (substR ($textColor, 5,2));
 $textColor = Imagecolorallocate ($this->newresource, $tR, $tG, $tB); For ($i =0 $i <strlen ($valid); $i + +) {imagestring ($this->newresource,5, $i * $width/$length +3,2, $valid [$i],$ 
 TextColor);
 } $this->newrestype = Imagetype_jpeg;
 return $valid; /** * Display output image * @return void/Public Function display ($fileName = ', $quality =100) {$imgType = $this->newresty
 Pe
    $IMAGESRC = $this->newresource;
 Switch ($imgType) {case Imagetype_gif:if ($fileName = = ") {header (' content-type:image/gif ');
 } imagegif ($IMAGESRC, $fileName, $quality);
 Break
 Case Imagetype_jpeg:if ($fileName = = ") {header (' content-type:image/jpeg ');
 } imagejpeg ($IMAGESRC, $fileName, $quality);
 Break
 Case Imagetype_png:if ($fileName = = ") {header (' content-type:image/png ');
 Imagepng ($IMAGESRC);
 else {imagepng ($IMAGESRC, $fileName);
 } break;
 Case Imagetype_wbmp:if ($fileName = = ") {header (' content-type:image/wbmp '); Imagewbmp ($IMAGESRC, $fileNaMe, $quality);
 Break
 Case Imagetype_xbm:if ($fileName = = ") {header (' content-type:image/xbm ');
 } IMAGEXBM ($IMAGESRC, $fileName, $quality);
 Break
 Default:throw new Exception (' Unsupport image type ');
 } Imagedestroy ($IMAGESRC); /** * Save image * @param int $fileNameType file name Type 0 Use the original filename, 1 use the specified file name, 2 in the original filename plus suffix, 3 generate random file name * @param string $folder folder path is null and The original file is the same * @param string $param parameter $filenametype 1 is the filename 2 o'clock suffix * @return void */Public Function Save ($fileNameType = 0, $fo Lder = NULL, $param = ' _miniature ') {if ($folder ==null) {$folder = DirName ($this->filename).
 Directory_separator;
 $fileExtName = Filesystem::fileext ($this->filename, true);
 $fileBesicName = Filesystem::getbasicname ($this->filename, false);
 Switch ($fileNameType) {Case 1: $newFileName = $folder. $param;
 Break
 Case 2: $newFileName = $folder. $fileBesicName. $param. $fileExtName;
 Break
 Case 3: $tmp = Date (' Ymdhis ');
 $fileBesicName = $tmp;
 $i = 0; while (File_exists ($folder. $fileBesicNAme. $fileExtName)) {$fileBesicName = $tmp. $i;
 $i + +;
 $newFileName = $folder. $fileBesicName. $fileExtName;
 Break
 Default: $newFileName = $this->filename;
 Break
 $this->display ($newFileName);
 return $newFileName; }}?>

I hope this article will help you with your PHP programming.

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.