This article mainly introduces how to add, compress, and cut php image watermarks. For more information, see
This article mainly introduces how to add, compress, and cut php image watermarks. For more information, see
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.
The procedure for image operations is as follows:
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.
<? Phpclass 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 $ 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 functio N 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] [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 ;} /*** display image ** @ return [type] [description] */public function show () {header ("content-type :". $ this-> info ['mime ']); $ funn = "image ". $ this-> type; $ funn ($ this-> image);}/*** warranty 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.