PHPThe operation of the image file is mainly using the GD library extension. When we frequently use
PHPWhen you manipulate a picture, you naturally encapsulate many functions, or you will write too many duplicate code. When there are many related functions to the picture, we can consider these
PHPFunctions are also collated, so there is the idea of encapsulation into classes. Let's take a look at the following how to encapsulate it!
The operation of the picture is mainly four steps:
Open picture
Manipulating pictures
Output picture
Destroying pictures
1,3,4 three steps each time to write, each time is similar. The only step that really needs to be flexible is to manipulate the picture. Operation pictures are often done by 1 or more major GD functions.
This article encapsulates four kinds of methods inside the class, the text watermark (Imagettftext ()), the Picture watermark (Imagecopymerge ()), the picture compress, the picture shearing (imagecopyresampled ()), the other commonly used GD function does not repeat. Directly on the code:
<?php class image{Private $info; Private $image; Public $type; Public function construct ($SRC) {$this->info=getimagesize ($SRC); $this->type=image_type_to_extension ($this->info[' 2 '],false); $fun = "imagecreatefrom{$this->type}"; $this->image= $fun ($SRC); }/** * Text watermark * @param [type] $font font * @param [type] $content content * @param [type] $size Text Size * @param [type] $col text color (four-tuple array) * @param array $location position * @param integer $angle tilt angle * @return [Type] */Public Function Fontmark ($font, $content, $size, $col, $location, $angle =0) {$c Ol=imagecolorallocatealpha ($this->image, $col [' 0 '], $col [' 1 '], $col [' 2 '], $col [' 3 ']); Imagettftext ($this->image, $size, $angle, $location [' 0 '], $location [' 1 '], $col, $font, $content); }/** * Image watermark * @param [type] $imageMark watermark image Address * @param [type] $dst the position of the watermark picture in the original picture * @pAram [Type] $pct transparency * @return [Type] */Public Function ImageMark ($imageMark, $DST, $pct) { $info 2=getimagesize ($imageMark); $type =image_type_to_extension ($info 2[' 2 '],false); $func 2= "Imagecreatefrom". $type; $water = $func 2 ($imageMark); Imagecopymerge ($this->image, $water, $DST [0], $DST [1], 0, 0, $info 2[' 0 '], $info 2[' 1 '], $pct); Imagedestroy ($water); }/** * Compress picture * @param [type] $thumbSize compress picture size * @return [type] [description] */public function Thumb ($thumbSize) {$imageThumb =imagecreatetruecolor ($thumbSize [0], $thumbSize [1]); Imagecopyresampled ($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize [0], $thumbSize [1], $this->info[' 0 '], $this-& gt;info[' 1 ']); Imagedestroy ($this->image); $this->image= $imageThumb; }/** * Crop picture * @param [type] $cutSize crop size * @param [type] $location crop position * @return [type] [descRiption] */Public Function cut ($cutSize, $location) {$imageCut =imagecreatetruecolor ($cutSize [0], $cutSize [1 ]); Imagecopyresampled ($imageCut, $this->image, 0, 0, $location [0], $location [1], $cutSize [0], $cutSize [1], $cutSize [0] , $cutSize [1]); Imagedestroy ($this->image); $this->image= $imageCut; }/** * Show image * @return [type] [description] */Public Function Show () {header ("Content-type:"). $th Is->info[' mime '); $funn = "image". $this->type; $funn ($this->image); }/** * Save picture * @param [type] $newname new picture name * @return [Type] [description] * * Public function Save ($ne Wname) {Header ("Content-type:". $this->info[' mime ')]; $funn = "image". $this->type; $funn ($this->image, $newname. $this->type); } public Function Destruct () {Imagedestroy ($this->image); }}?>