Php image watermark adding, compression, and cutting encapsulation

Source: Internet
Author: User
: This article describes how to add, compress, and cut php image watermarks. For more information about PHP tutorials, 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 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.

This article encapsulates four methods in the class: text watermark (imagettftext (), image watermark (imagecopymerge (), image compression, image cutting (imagecopyresampled ()), the rest of the commonly used GD functions will not be described in detail. Directly run the code:

 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 ($ im AgeMark); $ 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], $ t HumbSize [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]); im Agecopyresampled ($ 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 Title * @ 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, just add them to this class ~~

The above describes how to add, compress, and cut watermarks to php images, including some content. I hope my friends who are interested in PHP tutorials will be helpful.

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.