PHP creates image thumbnails, watermarks, and fonts. php website creation tutorial: php website background creation tutorial: phpcms template creation

Source: Internet
Author: User
Tags php website
Make an image. php: PHP creates image thumbnails, watermarks, and fonts. The following is a Class I provide. The three functions are encapsulated below: imgSrc $ imgS below is a Class I provide, which encapsulates these three features:

 ImgSrc = $ imgSrc; $ this-> init ();}/*** initialization operation */private function init () {// Obtain the image information (obtain the basic information of the image you want to process through the method provided by the GD library) $ this-> info = getimagesize ($ this-> imgSrc ); // Obtain the image type by using the image number $ this-> type = image_type_to_extension ($ this-> info [2], false ); // create an image with the same image type in the memory $ fun = "imagecreatefrom {$ this-> type }"; // copy the image to the memory $ this-> image = $ fun ($ this-> imgSrc ); $ this-> showOrSaveFunc = "image {$ this-> type}";}/*** add a font to the image * @ param $ fo Ntfile font file path * @ param $ text content * @ param $ red component * @ param $ green component * @ param $ blue component * @ param $ alpha transparency *@ param $ angle offset angle * @ param $ size font size * @ param $ x distance from the left * @ param $ y distance from the right */public function addFont ($ fontfile, $ text, $ red, $ green, $ blue, $ alpha, $ angle, $ size, $ x, $ y) {$ color = imagecolorallocatealpha ($ this-> image, $ red, $ green, $ blue, $ alpha); imagettftext ($ this-> image, $ size, $ angle, $ X, $ y, $ color, $ fontfile, $ text);}/*** browser output image Type */private function setOutputHeader () {header ("Content-Type: ". $ this-> info ['Mime ']);}/*** output the image to the browser */public function outputimagetobroader () {$ this-> setOutputHeader (); $ this-> excShowOrSaveFunc ($ this-> showOrSaveFunc);}/*** output or save image */private function excShowOrSaveFunc ($ type, $ file = '') {if ($ file = '') {$ type ($ this-> image);} else {$ type ($ this-> image, $ file) ;}}/*** Save the file to the local * @ param local path * @ param file name */public function outputImageToStorage ($ filepath, $ filename) {$ this-> setOutputHeader (); $ this-> excShowOrSaveFunc ($ this-> showOrSaveFunc, $ filepath. $ filename. '. '. $ this-> type );} /*** add a watermark to the image * @ param $ waterMarkPath watermark image path * @ param $ dst_x distance from the watermark on the left of the source image * @ param $ dst_y distance from the watermark on the top of the source image * @ param $ pct watermark transparency */public function addWaterMark ($ waterMarkPath, $ dst_x, $ dst_y, $ pct ){ $ WaterInfo = getimagesize ($ waterMarkPath); $ waterType = image_type_to_extension ($ waterInfo [2], false); $ waterFun = "imagecreatefrom {$ waterType }"; $ waterMarkImage = $ waterFun ($ waterMarkPath); // imagecopymerge (dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct) // pct transparency imagecopymerge ($ this-> image, $ waterMarkImage, $ dst_x, $ dst_y, 0, 0, $ waterInfo [0], $ waterInfo [1], $ pct); imagedestroy ($ waterMar KImage);}/*** generate image thumbnails * @ param $ mWidth width to be compressed */public function createThumbnail ($ mWidth) {$ width = $ this-> info [0]; $ height = $ this-> info [1]; if ($ mWidth >=$ width) {return ;} $ mHeight = $ height * $ mWidth/$ width; // 1. create a true color image with a width of 300 and a height of 200 in the memory $ imageThumb = imagecreatetruecolor ($ mWidth, $ mHeight); // 2. the core step is to copy the source image to the new positive color image and compress it according to a certain proportion. // imagecopyresampled (dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, Dst_h, src_w, src_h) imagecopyresampled ($ imageThumb, $ this-> image, 0, 0, 0, 0, $ mWidth, $ mHeight, $ width, $ height ); $ this-> image = $ imageThumb;}/** destroy image resources */public function destroy () {imagedestroy ($ this-> image) ;}}?>

The usage is as follows:

 addFont("consola.ttf", "raid", 0, 0, 0, 50, 0, 45, 20, 20);$imageHelper->addWaterMark('pic2.jpg', 0, 0, 50);*/$imageHelper->createThumbnail(300);// $imageHelper->outputImageToBrowser();$imageHelper->outputImageToStorage('','testImageHelperAddFont');$imageHelper->destroy(); ?>

The above section describes how to use PHP to create image thumbnails, add watermarks, and add fonts, including image production and php 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.