PHP image watermark Add, compress, cut wrapper class implementation, _php tutorial

Source: Internet
Author: User

PHP image watermark Add, compress, cut the wrapper class implementation,


PHP operation of the image file is mainly to use the GD library extension. When we frequently use PHP to manipulate images, we naturally encapsulate many functions, otherwise we will write too many repetitive code. When there are a lot of related functions of the picture, we can consider the function also to tidy up, so there is the idea of encapsulation into class.
The operation of the picture is mainly four steps :
1. Open the picture
2. Operation Picture
3. Output image
4. Destroying pictures
1,3,4 three steps each time to write, each time is similar. The only step that really needs to be flexible is to manipulate the picture. Operation pictures are often done by 1 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 font * @param [type] $content content * @param [type] $size text size * @param [type ] $col text color (four-tuple array) * @param array $location position * @param integer $angle tilt 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 the position of the watermark picture in the original picture * @param [type] $pct transparency * @return [Type] */Public Function ImageMark ($imageMark, $DST, $pct) {    $info 2=getimagesize ($imageMark);    $type =image_type_to_extension ($info 2[' 2 '],false);    $func 2= "Imagecreatefrom". $type;    $water = $func 2 ($imageMark);    Imagecopymerge ($this->image, $water, $DST [0], $DST [1], 0, 0, $info 2[' 0 '], $info 2[' 1 '], $pct);  Imagedestroy ($water); }/** * Compress picture * @param [type] $thumbSize compress picture size * @return [type] [description] */Public Function thumb ($thu        mbsize) {$imageThumb =imagecreatetruecolor ($thumbSize [0], $thumbSize [1]); Imagecopyresampled ($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize [0], $thumbSize [1], $this->info[' 0 '], $this-&    gt;info[' 1 ']);    Imagedestroy ($this->image);  $this->image= $imageThumb;    }/** * Crop picture * @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; }/** * Show image * @return [type] [description] */Public Function Show () {header ("Content-type:". $this->info[' m    IME ']);    $funn = "image". $this->type;  $funn ($this->image);     }/** * Save picture * @param [type] $newname new picture 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); }}?>

is not this operation is very simple, if you need other operations, only need to add to this class is good ~ ~ I hope you can master.

http://www.bkjia.com/PHPjc/1052063.html www.bkjia.com true http://www.bkjia.com/PHPjc/1052063.html techarticle PHP Image watermark Add, compress, cut the encapsulation class implementation, PHP operation of the image file is mainly to use the GD library extension. When we frequently use PHP to manipulate images, we naturally encapsulate ...

  • 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.