Php image watermark adding, compression, and cutting encapsulation class implementation ,. Php image watermark adding, compression, and cutting encapsulation class implementation. php's operations on image files are mainly based on the GD Library extension. When we frequently use php to operate images, it will naturally encapsulate the php image watermark adding, compression, and cutting encapsulation class implementation,
Php's operations on image files mainly use the GD Library extension. When we frequently use php to operate images, we will naturally encapsulate many functions. Otherwise, we will write too much repeated code. When there are many functions related to images, we can consider sorting out these functions, so we have the idea of encapsulating them into classes.
Image operationsFour steps:
1. open an image
2. operate images
3. output image
4. destroy images
1, 3, 4 three steps should be written each time, and each time is similar. You only need to perform image operations. Images are often operated by one or more major GD functions.
<? 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 * @ param [type] $ content * @ param [type] $ size text size * @ param [type] $ col text color (quaternary group) * @ param array $ low.o N location * @ param integer $ angle skew angle * @ return [type] */public function fontMark ($ font, $ content, $ size, $ col, $ location, $ angle = 0) {$ col = 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 watermark image position in the original image * @ param [Type] $ pct transparency * @ return [type] */public function imageMark ($ imageMark, $ dst, $ pct) {$ info2 = getimagesize ($ imageMark ); $ type = image_type_to_extension ($ info2 ['2'], false); $ func2 = "imagecreatefrom ". $ type; $ water = $ func2 ($ imageMark); imagecopymerge ($ this-> image, $ water, $ dst [0], $ dst [1], 0, 0, $ info2 ['0'], $ info2 ['1'], $ pct); imagedestroy ($ water );} /*** compress the image ** @ param [type] $ thumbSize: compress the image 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-> info ['1']); imagedestroy ($ this-> image); $ this-> image = $ imageThumb ;} /*** crop an image ** @ param [type] $ cutSize crop size * @ param [type] $ location crop position * @ return [type] [descrip Tion] */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 ;} /*** display image ** @ return [type] [description] */public function show () {header ("content-type :". $ this-> info ['Mime ']); $ Funn = "image ". $ this-> type; $ funn ($ this-> image );} /*** save image ** @ param [type] $ newname new image name * @ return [type] [description] */public function save ($ newname) {header ("content-type :". $ this-> info ['Mime ']); $ funn = "image ". $ this-> type; $ funn ($ this-> image, $ newname. '. '. $ this-> type);} public function _ destruct () {imagedestroy ($ this-> image) ;}}?>
If you need other operations, you just need to add them to the class ~~ I hope you can master it.
Ghost, php mainly uses the GD library to expand operations on image files. When we frequently use php to operate images, it will naturally encapsulate the image...