PHP image processing class thumbnail plus watermark

Source: Internet
Author: User

Use of two image system functions

imagecopymerge- Copy and merge part of an image

imagecopyresampled-resampling and resizing copies of portions of images

/* How to know the size and type of the picture cannot confirm the calling function: Imagecreatefrompng/jpeg ... Can be left alone the width and height of the picture is known as an important function getimagesize () *//* want to manipulate the picture first the size of the picture, type information to get a watermark: is to copy the specified watermark to the target, and add transparency effect thumbnail: is to copy the big picture to the small size screen * * Class Imagetool{//imageinfo analysis picture Information//return Array () public static function Imageinfo ($image) {//Determine if the picture exists if (!file_ Exists ($image)) {return false;} $info = getimagesize ($image); if ($info = = False) {return false;} At this point the info analysis is an array of $img[' width ' = $info [0]; $img [' height '] = $info [1];//analysis image suffix $img[' ext '] = substr ($info [' MIME '], Strpos ($info [' MIME '], '/') +1); return $img;} /* Watermark string $DST $water watermark Small image $save not filled, the default replaces the original figure $alpha Transparency $pos watermark position 1234, clockwise */public static funct Ion Water ($DST, $water, $save =null, $pos =2, $alpha =50) {//Ensure that two images exist if (!file_exists ($DST) | |!file_exists ($water)) { return false;} 1. Ensure that the watermark is not larger than the picture to be manipulated $dinfo = Self::imageinfo ($DST); $winfo = Self::imageinfo ($water);//judging if ($winfo [' height '] > $ dinfo[' Height ' | | $winfo [' width '] > $dinfo [' width ']) {return false;} Two graphs, read on the canvas, but the picture may be PNG, possibly jpeg, what function to read $dfunc = ' imagecreatEfrom '. $dinfo [' ext ']; $wfunc = ' Imagecreatefrom '. $winfo [' ext '];if (!function_exists ($dfunc) | |!function_exists ($ Wfunc)) {return false;} Dynamic load function to create canvas $dim = $dfunc ($DST); Create the canvas to be manipulated $wim = $wfunc ($water); Create watermark canvas//calculate pasted coordinates based on Watermark location switch ($pos) {case 0;//upper Left $posx = 0; $posy = 0;break;case 1;//upper right corner $posx = $dinfo [' width ']-$winfo [' width ']; $posy = 0;break;case 2; lower left corner $posx = 0; $posy = $dinfo [' height ']-$winfo [' Height '];break;default://default lower right corner $posx = $dinfo [' width ']-$winfo [' width '] $posy = $dinfo [' height ']-$winfo [' Height '];} Where is the watermark?  Imagecopymerge (Resource $dst _im, resource $src _im, int $dst _x, int $dst _y, int $src _x, int $src _y, int $src _w, int $src _h, int $pct) Imagecopymerge ($dim, $wim, $posx, $posy, 0,0, $winfo [' width '], $winfo [' height '], $alpha); if (! $save) {$save = $DST; unlink ($DST);//delete original}//save picture $createfunc = ' image '. $dinfo [' ext ']; $createfunc ($dim, $save);//Destroy Picture Imagedestroy ($dim); Imagedestroy ($wim); return true;} /*thumb generate thumbnails, such as scaling, left white */public static function thumb ($DST, $save=null, $width =200, $height =200) {//First determine if the picture to be processed exists $dinfo = Self::imageinfo ($DST); if ($dinfo = = False) {return false;} Calculate the scale $calc = min ($width/$dinfo [' width '], $height/$dinfo [' height ']), or//create a canvas $dfunc = ' imagecreatefrom ' of the original diagram. $dinfo [' Ext ']; $dim = $dfunc ($DST);//create thumbnail drawing cloth $tim = Imagecreatetruecolor ($width, $height);//Create white-filled thumbnail drawing cloth $white = Imagecolorallocate ($tim, 255,255,255);//fill the thumbnail canvas imagefill ($tim, 0,0, $white);//copy and thumbnail $dwidth = (int) $dinfo [' Width ']*$ Calc; $dheight = (int) $dinfo [' Height ']* $calc; $paddingx = ($width-$dwidth)/2; $paddingy = ($height-$dheight)/2;imageco Pyresampled ($tim, $dim, $paddingx, $paddingy, 0,0, $dwidth, $dheight, $dinfo [' width '], $dinfo [' height ']);//Save picture if (!$ Save) {$save = $DST; unlink ($DST);} $createfun = ' image '. $dinfo [' ext ']; $createfun ($tim, $save); Imagedestroy ($dim); Imagedestroy ($tim); return true;}

To invoke a parameter:

Call  the Add watermark echo imagetool::water (' Original image address ', ' Add watermark image address ', ' generate picture ')? ' Ok ': false;
Call thumbnail echo imagetool::thumb (' original address ', ' Generate image address ', 200,200)? ' Ok ': false;

PHP image processing class thumbnail plus watermark

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.