Common php Image Processing (watermark, proportional scaling, fixed height and width) sharing,
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.