PHP production image thumbnail, add watermark, add font PHP website production tutorial PHP website background Production Tutorial Phpcms template system

Source: Internet
Author: User
Tags php website save file
Here is a class I provided, which encapsulates these three features:

 IMGSRC = $IMGSRC; $this->init ();} /*** initialization Operation */private function init () {//Get picture information (through the GD library method, get the basic information of the picture you want to process) $this->info = getimagesize ($this IMGSRC);//The type of image is obtained by the number of the image $this->type = Image_type_to_extension ($this->info[2], false);// Create an image in memory like our image type $fun = "imagecreatefrom{$this->type}";//Copy the picture into memory $this->image = $fun ($this->imgsrc); This->showorsavefunc = "image{$this->type}";} /*** Add fonts to pictures * @param $fontfile font file path * @param $text text content * @param $red Red component * @param $green Green component * @param $blue Blue component * @param $alpha Transparency * @param $angle Offset angle * @param $size Font size * @param distance from the left * $x @param distance from the right */public function AddFont ($y , $text, $red, $green, $blue, $alpha, $angle, $size, $x, $y) {$color = Imagecolorallocatealpha ($this->image, $red, $gre En, $blue, $alpha); Imagettftext ($this->image, $size, $angle, $x, $y, $color, $fontfile, $text);} /*** browser output Picture type */private function Setoutputheader () {Header ("Content-type:". $this->info[' mime ']);} /*** output image to browser */public functiOn Outputimagetobrowser () {$this->setoutputheader (); $this->excshoworsavefunc ($this->showorsavefunc);} /*** output or Save picture */private function Excshoworsavefunc ($type, $file = ') {if ($file = = ') {$type ($this->image);} else {$typ E ($this->image, $file);}} /*** save file to local * @param local path * @param file name */public function outputimagetostorage ($filepath, $filename) {$this Setoutputheader (); $this->excshoworsavefunc ($this->showorsavefunc, $filepath. $filename. $this->type);} /*** to add a watermark to a picture * @param $waterMarkPath watermark Image Path * @param $dst _x watermark distance from the left of the original image * @param $dst the distance from the upper side of the _y watermark * @param $pct The transparency of the watermark */publ IC 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 ($waterMarkImage);} /*** Generate picture thumbnails * @param $mWidth the width you want to compress */public function Createthumbnail ($mWidth) {$width = $this->info[0]; $height = $thi S->info[1];if ($mWidth >= $width) {return;} $mHeight = $height * $mWidth/$width;//1. Create a true color image with a width of 300 and 200 in memory $imagethumb = Imagecreatetruecolor ($mWidth, $mHeight) //2. Core steps, copy the original image to the newly created positive color picture, and compress the//imagecopyresampled (Dst_image, Src_image, dst_x, dst_y, src_x, src_y, Dst_w, dst_ in a certain proportion) H, Src_w, Src_h) imagecopyresampled ($imageThumb, $this->image, 0, 0, 0, 0, $mWidth, $mHeight, $width, $height); $this-&G T;image = $imageThumb;} /** Destroy Picture resource */public function Destroy () {Imagedestroy ($this->image);}} ?>

Here's how to use it:

 
  AddFont ("Consola.ttf", "Raid", 0, 0, 0, 0, (), $imageHelper->addwatermark (' pic2.jpg ', 0, 0,); */$imageHel Per->createthumbnail ()//$imageHelper->outputimagetobrowser (); $imageHelper->outputimagetostorage (' ', ' Testimagehelperaddfont '); $imageHelper->destroy ();?>

The above introduces the PHP production image thumbnail, add watermark, plus font, including the production of pictures, PHP content, I hope that the PHP tutorial interested friends have helped.

  • Related Article

    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.