Common php image processing class (Watermark, proportional scaling, fixed height and width) sharing, _ PHP Tutorial

Source: Internet
Author: User
Tags imagecopy
Common php image processing class (Watermark, proportional scaling, fixed height and width) sharing ,. Common php image processing (Watermark, proportional scaling, fixed height and width) sharing and common php image processing (Watermark, proportional scaling, fixed height and width) share phpPHP watermark adding fixed common php image processing class (Watermark, proportional scaling, fixed height and width,

Common php image processing class (Watermark, proportional scaling, fixed height and width) sharing

<? Php // PHP add watermark & proportional thumbnail & fixed height & fixed width class. Class Image_process {public $ source; // public $ source_width; // public $ source_height; // public $ source_type_id; public $ orign_name; public $ orign_dirname; // input the source image path public function _ construct ($ source) {$ this-> typeList = array (1 => 'GIF', 2 => 'jpg ', 3 => 'PNG '); $ ginfo = getimagesize ($ source); $ this-> source_width = $ ginfo [0]; $ this-> source_height = $ ginfo [1]; $ this-> source_type_id = $ ginfo [2]; $ This-> orign_url = $ source; $ this-> orign_name = basename ($ source); $ this-> orign_dirname = dirname ($ source );} // determine the format of the image file and return the recognizable PHP code public function judgeType ($ type, $ source) {if ($ type = 1) {return imagecreatefromgif ($ source); // gif} else if ($ type = 2) {return imagecreatefromjpeg ($ source ); // jpg} else if ($ type = 3) {return imagecreatefrompng ($ source); // png} else {return false ;}// generate a watermark image Public function waterMakeImage ($ logo) {$ linfo = getimagesize ($ logo); $ logo_width = $ linfo [0]; $ logo_height = $ linfo [1]; $ logo_type_id = $ linfo [2]; $ sourceHandle = $ this-> judgeType ($ this-> source_type_id, $ this-> orign_url ); $ logoHandle = $ this-> judgeType ($ logo_type_id, $ logo); if (! $ SourceHandle |! $ LogoHandle) {return false;} $ x = ($ this-> source_width-$ logo_width)/2; $ y = ($ this-> source_height-$ logo_height)/2; imagecopy ($ sourceHandle, $ logoHandle, $ x, $ y, 0, $ logo_width, $ logo_height); $ newPic = $ this-> orign_dirname. '\ water _'. time (). '. '. $ this-> typeList [$ this-> source_type_id]; if ($ this-> saveImage ($ sourceHandle, $ newPic) {imagedestroy ($ sourceHandle ); imagedestroy ($ logoHandle) ;}// fixed height and width pu Blic function fixSizeImage ($ width, $ height) {if ($ width> $ this-> source_width) $ this-> source_width; if ($ height> $ this-> source_height) $ this-> source_height; if ($ width = false) {$ width = floor ($ this-> source_width/($ this-> source_height/$ height ));} if ($ height = false) {$ height = floor ($ this-> source_height/($ this-> source_width/$ width ));} $ this-> tinyImage ($ width, $ height);} // proportional scaling Image public Function scaleImage ($ scale) {$ width = floor ($ this-> source_width * $ scale); $ height = floor ($ this-> source_height * $ scale ); $ this-> tinyImage ($ width, $ height);} // Create a thumbnail public function tinyImage ($ width, $ height) {$ tinyImage = imagecreatetruecolor ($ width, $ height); $ handle = $ this-> judgeType ($ this-> source_type_id, $ this-> orign_url); if (function_exists ('imagecopyresampled ') {imagecopyresampled ($ tinyIm Age, $ handle, 0, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height);} else {imagecopyresized ($ tinyImage, $ handle, 0, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height);} $ newPic = $ this-> orign_dirname. '\ thumb _'. time (). '_'. $ width. "_". $ height. ". ". $ this-> typeList [$ this-> source_type_id]; if ($ this-> saveImage ($ tinyImage, $ newPic) {imagedestroy ($ tinyImage); imagedestr Oy ($ handle) ;}}// save the image private function saveImage ($ image, $ url) {if (imagejpeg ($ image, $ url )) {return true ;}}$ imgHandle = new Image_process ('d: \ AppServ \ www \ test \ getimg \ 14061097445601.jpg '); // $ imgHandle-> waterMakeImage ('d: \ AppServ \ www \ test \ getimg \ shougongke.png '); // Generate the watermark image // $ imgHandle-> fixSizeImage (200,150 ); // fixed-length image $ imgHandle-> scaleImage (0.2); // proportional scaling?>

Example 2:

<? Php/*** image processing class * @ author FC_LAMP * @ internal features include: watermark, thumbnail */class Img {// image format private $ exts = array ('jpg ', 'jpeg', 'GIF', 'bmp ', 'PNG '); /***** @ throws Exception */public function _ construct () {if (! Function_exists ('gd _ info') {throw new Exception ('loading gd library failed! ') ;}}/***** Cropping and compression * @ param $ src_img image * @ param $ save_img the image generated * @ param $ option parameter options, including: $ maxwidth width $ maxheight height * array ('width' => xx, 'height' => xxx) * @ internal * our general image compression method, the image * generated when the image is too long or too wide will be squashed. for this, the first cropping and then proportional compression method */public function thumb_img ($ src_img, $ save_img = '', $ option) {if (empty ($ option ['width']) or empty ($ option ['height']) {return array ('flag' => False, 'MSG '=>' original The image length and width cannot be less than 0 ');} $ org_ext = $ this-> is_img ($ src_img); if (! $ Org_ext ['flag']) {return $ org_ext;} // if a save path exists, check whether the path is correct. if (! Empty ($ save_img) {$ f = $ this-> check_dir ($ save_img); if (! $ F ['flag']) {return $ f ;}// obtain the corresponding method $ org_funcs = $ this-> get_img_funcs ($ org_ext ['MSG ']); // obtain the original size $ source = $ org_funcs ['create _ func'] ($ src_img); $ src_w = imagesx ($ source); $ src_h = imagesy ($ source ); // Adjust the original image (retain the original image shape and crop the image) $ dst_scale = $ option ['height']/$ option ['width']; // target image aspect ratio $ src_scale = $ src_h/$ src_w; // source image aspect ratio if ($ src_scale> = $ dst_scale) {// too high $ w = intval ($ src_w); $ H = intval ($ dst_scale * $ w); $ x = 0; $ y = ($ src_h-$ h)/3 ;} else {// too wide $ h = intval ($ src_h); $ w = intval ($ h/$ dst_scale); $ x = ($ src_w-$ w)/2; $ y = 0;} // crop $ croped = imagecreatetruecolor ($ w, $ h); imagecopy ($ croped, $ source, 0, 0, $ x, $ y, $ src_w, $ src_h); // scale $ scale = $ option ['width']/$ w; $ target = imagecreatetruecolor ($ option ['width'], $ option ['height']); $ fi Nal_w = intval ($ w * $ scale); $ final_h = intval ($ h * $ scale); imagecopyresampled ($ target, $ croped, 0, 0, 0, 0, 0, $ final_w, $ final_h, $ w, $ h); imagedestroy ($ croped); // output (save) image if (! Empty ($ save_img) {$ org_funcs ['SAVE _ func'] ($ target, $ save_img);} else {header ($ org_funcs ['head']); $ org_funcs ['SAVE _ func'] ($ target);} imagedestroy ($ target); return array ('flag' => True, 'MSG '=> '');} /***** proportional scaling image * @ param $ src_img original image * @ param $ save_img location to be saved * @ param $ option parameter setting array ('width' => xx, 'height' => xxx) **/function resize_image ($ src_img, $ save_img = '', $ Option) {$ org_ext = $ this-> is_img ($ src_img); if (! $ Org_ext ['flag']) {return $ org_ext;} // if a save path exists, check whether the path is correct. if (! Empty ($ save_img) {$ f = $ this-> check_dir ($ save_img); if (! $ F ['flag']) {return $ f ;}// obtain the corresponding method $ org_funcs = $ this-> get_img_funcs ($ org_ext ['MSG ']); // obtain the original size $ source = $ org_funcs ['create _ func'] ($ src_img); $ src_w = imagesx ($ source); $ src_h = imagesy ($ source ); if ($ option ['width'] & $ src_w> $ option ['width']) | ($ option ['height'] & $ src_h> $ option ['height']) {if ($ option ['width'] & $ src_w> $ option ['width']) {$ widthratio = $ op Tion ['width']/$ src_w; $ resizewidth_tag = true;} if ($ option ['height'] & $ src_h> $ option ['height']) {$ heightratio = $ option ['height']/$ src_h; $ resizeheight_tag = true;} if ($ resizewidth_tag & $ resizeheight_tag) {if ($ widthratio <$ heightratio) $ ratio = $ widthratio; else $ ratio = $ heightratio;} if ($ resizewidth_tag &&! $ Resizeheight_tag) $ ratio = $ widthratio; if ($ resizeheight_tag &&! $ Resizewidth_tag) $ ratio = $ heightratio; $ newwidth = $ src_w * $ ratio; $ newheight = $ src_h * $ ratio; if (function_exists ("imagecopyresampled ")) {$ newim = imagecreatetruecolor ($ newwidth, $ newheight); imagecopyresampled ($ newim, $ source, 0, 0, 0, 0, $ newwidth, $ newheight, $ src_w, $ src_h);} else {$ newim = imagecreate ($ newwidth, $ newheight); imagecopyresized ($ newim, $ source, 0, 0, 0, 0, 0, $ Newwidth, $ newheight, $ src_w, $ src_h) ;}/// output (save) image if (! Empty ($ save_img) {$ org_funcs ['SAVE _ func'] ($ newim, $ save_img);} else {header ($ org_funcs ['head']); $ org_funcs ['SAVE _ func'] ($ newim);} imagedestroy ($ newim); return array ('flag' => True, 'MSG '=> '');} /***** generate watermark image * @ param $ org_img original image * @ param $ mark_img watermark Mark image * @ param $ save_img when its directory does not exist, you will try to create the directory * @ param array $ option. some basic settings for the watermark include: * x: horizontal position of the watermark. the default value is the value after the width of the watermark * y: vertical position of the watermark The value * alpha: alpha after the watermark height is subtracted (transparency control). The default value is 50 */public function water_mark ($ org_img, $ mark_img, $ save_img = '', $ option = array () {// check the image $ org_ext = $ this-> is_img ($ org_img); if (! $ Org_ext ['flag']) {return $ org_ext;} $ mark_ext = $ this-> is_img ($ mark_img); if (! $ Mark_ext ['flag']) {return $ mark_ext;} // if a save path exists, check whether the path is correct. if (! Empty ($ save_img) {$ f = $ this-> check_dir ($ save_img); if (! $ F ['flag']) {return $ f ;}// obtain the canvas $ org_funcs = $ this-> get_img_funcs ($ org_ext ['MSG ']); $ org_img_im = $ org_funcs ['create _ func'] ($ org_img); $ mark_funcs = $ this-> get_img_funcs ($ mark_ext ['MSG ']); $ mark_img_im = $ mark_funcs ['create _ func'] ($ mark_img); // Copy the watermark image coordinate $ mark_img_im_x = 0; $ mark_img_im_y = 0; // Copy the watermark image height and width $ mark_img_w = imagesx ($ mark_img_im); $ mark_img_h = imagesy ($ mark_im G_im); $ org_img_w = imagesx ($ org_img_im); $ org_img_h = imagesx ($ org_img_im); // combine and generate coordinate points $ x = $ org_img_w-$ mark_img_w; $ org_img_im_x = isset ($ option ['x'])? $ Option ['x']: $ x; $ org_img_im_x = ($ org_img_im_x> $ org_img_w or $ org_img_im_x <0 )? $ X: $ org_img_im_x; $ y = $ org_img_h-$ mark_img_h; $ org_img_im_y = isset ($ option ['Y'])? $ Option ['Y']: $ y; $ org_img_im_y = ($ org_img_im_y> $ org_img_h or $ org_img_im_y <0 )? $ Y: $ org_img_im_y; // alpha $ alpha = isset ($ option ['alpha'])? $ Option ['alpha']: 50; $ alpha = ($ alpha> 100 or $ alpha <0 )? 50: $ alpha; // merge images imagecopymerge ($ org_img_im, $ mark_img_im, $ org_img_im_x, $ tags, $ mark_img_im_x, $ tags, $ mark_img_w, $ mark_img_h, $ alpha ); // output (save) the image if (! Empty ($ save_img) {$ org_funcs ['SAVE _ func'] ($ org_img_im, $ save_img);} else {header ($ org_funcs ['header']); $ org_funcs ['SAVE _ func'] ($ org_img_im);} // destroy the canvas imagedestroy ($ org_img_im); imagedestroy ($ mark_img_im ); return array ('flag' => True, 'MSG '=> '');} /***** check the image * @ param unknown_type $ img_path * @ return array ('flag' => true/false, 'MSG '=> ext/error message) */private function is_im G ($ img_path) {if (! File_exists ($ img_path) {return array ('flag' => False, 'MSG '=> "loading image $ img_path failed! ") ;}$ Ext = explode ('.', $ img_path); $ ext = strtolower (end ($ ext); if (! In_array ($ ext, $ this-> exts) {return array ('flag' => False, 'MSG '=> "The format of image $ img_path is incorrect! ");} Return array ('flag' => True, 'MSG '=> $ ext );} /***** return the correct image function * @ param unknown_type $ ext */private function get_img_funcs ($ ext) {// Select switch ($ ext) {case 'jpg ': $ header = 'content-Type: image/jpeg '; $ createfunc = 'imagecreatefromjpeg'; $ savefunc = 'imagejpeg '; break; case 'jpeg ': $ header = 'content-Type: image/jpeg '; $ createfunc = 'imagecreatefromjpeg'; $ savefunc = 'imagejpeg '; break; Case 'GIF': $ header = 'content-Type: image/GIF'; $ createfunc = 'imagecreatefromgif '; $ savefunc = 'imagegif'; break; case 'bmp ': $ header = 'content-Type: image/bmp '; $ createfunc = 'imagecreatefrombmp'; $ savefunc = 'imagebmp'; break; default: $ header = 'content-Type: image/png '; $ createfunc = 'imagecreatefrompng'; $ savefunc = 'imagepng ';} return array ('SAVE _ func' => $ savefunc, 'create _ func' => $ Createfunc, 'header' => $ header);}/*** check and try to create a directory * @ param $ save_img */private function check_dir ($ save_img) {$ dir = dirname ($ save_img); if (! Is_dir ($ dir) {if (! Mkdir ($ dir, 0777, true) {return array ('flag' => False, 'MSG '=> "The image storage directory $ dir cannot be created! ") ;}} Return array ('flag' => True, 'MSG '=>'') ;}} if (! Empty ($ _ FILES ['test'] ['tmp _ name']) {// example $ img = new Img (); // original image $ name = explode ('. ', $ _ FILES ['test'] ['name']); $ org_img = 'd:/test. '. end ($ name); move_uploaded_file ($ _ FILES ['test'] ['tmp _ name'], $ org_img ); $ option = array ('width' =>$ _ POST ['width'], 'height' =>$ _ POST ['height']); if ($ _ POST ['type'] = 1) {$ s = $ img-> resize_image ($ org_img, '', $ option );} else {$ img-> thumb_img ($ org_img, '', $ option);} unlink ($ org_img );}

The above is all the content of this article. I hope you will like it.

Watermark (Watermark, proportional scaling, fixed height and width) sharing, common php image processing class (Watermark, proportional scaling, fixed height and width) sharing php // PHP adding watermark fixed...

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.