Common PHP Image Processing class (watermark, equal to zoom, fixed aspect) share,
Common PHP Image Processing class (watermark, equal to zoom, fixed aspect) share
<?php//php Add watermark & scale thumbnail & fixed height & fixed width class. Class image_process{public $source;//original public $source _width; Original width public $source _height; Original height public $source _type_id; Public $orign _name; Public $orign _dirname; Incoming original 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); }//To determine the format of the image file, return PHP-readable code public Function Judgetype ($type, $source) {if ($type = = 1) {return IMAGECREATEF Romgif ($source); GIF}else if ($type = = 2) {return imagecreatefromjpeg ($source),//jpg}else if ($type = = 3) {return IM Agecreatefrompng ($source); PNG}else{return false; }}//Generate watermarkPicture 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,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 width public 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); }//Scale picture public function Scaleimage ($scale) {$width = Floor ($this->source_width * $scale); $height = Floor ($this->source_height * $scale); $this->tinyimage ($width, $height); }//Create 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 ($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $thi S->source_width, $this->source_height); }else{imagecopyresized ($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_h eight); } $newPic = $this->orign_dirname. ' \thumb_ '. Time (). ' _ '. $width. ' _ ". $height.". $this->typelist[$this->source_type_id]; if ($this->saveimage ($tinyImage, $newPic)) {Imagedestroy ($tinyImage); Imagedestroy ($handle); }}//Save picture Private Function SaveImage ($image, $url) {if (imagejpeg ($image, $url)) {return true; }}} $imgHandle = new image_process (' D:\AppServ\www\test\getimg\14061907445601.jpg '); $imgHandle->watermakeimage (' D:\AppServ\www\test\getimg\shougongke.png '); Generate watermark Picture//$imgHandle->fixsizeimage (200,150); Fixed length picture $imgHandle->scaleimage (0.2); Equal proportional scaling?>
Example two:
<?php/** * * Image processing Class * @author Fc_lamp * @internal features include: watermark, thumbnail */class img{//Picture format Private $exts = array (' jpg ', ' jpeg ', ' gif ', ' BMP ', ' PNG '); /** * * * @throws Exception * * Public Function __construct () {if (! function_exists (' Gd_info ')) {throw new Excepti On (' Load GD library failed! ' ); }}/** * * cropping compression * @param $src _img Pictures * @param $save images created after _img * @param $option parameter options, including: $maxwidth wide $maxheight High * ARRA Y (' width ' =>xx, ' height ' =>xxx) * @internal * We generally compress the image method, the image generated when the picture is too long or too wide * will be "squashed", for this should be cut 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 length and width cannot be less than 0 '); } $org _ext = $this->is_img ($src _img); if (! $org _ext [' flag ']) {return $org _ext;}//If there is a save path, determine if the path is correct if (! Empty ($save _img)) {$f = $this->check_dir ($save _img); if (! $f [' flag ']) {return $f; }}//Get the appropriate method $org _funcs = $this->get_img_funcs ($org _ext [' msg ']); Get the original size $source = $org _funcs [' Create_func '] ($src _img); $src _w = Imagesx ($source); $src _h = Imagesy ($source); Adjust the original image (keep the picture in its original shape) $dst _scale = $option [' height ']/$option [' width ']; The aspect ratio of the target image $src _scale = $src _h/$src _w; Original 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 {//over-width $h = intval ($src _h); $w = Intval ($h/$DST _scale); $x = ($src _w-$w)/2; $y = 0; }//Trim $croped = Imagecreatetruecolor ($w, $h); Imagecopy ($croped, $source, 0, 0, $x, $y, $src _w, $src _h); Zoom $scale = $option [' width ']/$w; $target = Imagecreatetruecolor ($option [' width '], $option [' height ']); $final _w = intval ($w * $scale); $final _h = intval ($h * $scale); Imagecopyresampled ($target, $croped, 0, 0, 0, 0, $final _w, $final _h, $w, $h); Imagedestroy ($croped); Output (save) Picture if (! empty ($save _img)) {$org _funcs [' Save_func '] ($target, $save _img);} else {header ($org _funcs [' header ']); $org _funcs [' Save_func '] ($target); } Imagedestroy ($target); Return Array (' flag ' = = True, ' msg ' = = '); }/** * * Equal scale image * @param $src _img Original picture * @param $save where the _img needs to be saved * @param $option parameter Set Array (' Width ' =>xx, ' height ' =&G T;XXX) * */function Resize_image ($src _img, $save _img = ", $option) {$org _ext = $this->is_img ($src _img); if (! $o Rg_ext [' flag '] {return $org _ext;}//If there is a save path, determine if the path is correct if (! Empty ($save _img)) {$f = $this->check_dir ($save _ IMG); if (! $f [' flag ']) {return $f; }}//Get the appropriate method $org _funcs = $this->get_img_funcs ($org _ext [' msg ']); Get 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 = $option [' 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, $newwidth, $newheight, $src _w, $src _h); }}//Output (save) Picture if (! empty ($save _img)) {$org _funcs [' Save_func '] ($newim, $save _img);} else {header ($org _funCS [' header ']); $org _funcs [' Save_func '] ($newim); } Imagedestroy ($newim); Return Array (' flag ' = = True, ' msg ' = = '); /** * * Generate watermark Picture * @param $org _img Original image * @param $mark _img watermark Tagged Image * @param $save _img When its directory does not exist, try to create a directory * @param array $opti Some basic settings for watermark include: * x: Horizontal position of watermark, default value minus watermark Width * y: Vertical position of watermark, default value minus watermark Height * alpha:alpha value (control transparency), default is */Public function water _mark ($org _img, $mark _img, $save _img = ", $option = Array ()) {//check picture $org _ext = $this->is_img ($org _img); if (! $or G_ext [' flag ']) {return $org _ext;} $mark _ext = $this->is_img ($mark _img); if (! $mark _ext [' flag ']) {return $mark _ext;}//If there is a save path, determine if the path is correct if (! Empty ($save _img)) {$f = $this->check_di R ($save _img); if (! $f [' flag ']) {return $f; }}//Get the corresponding 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 watermark picture coordinates $mark _img_im_x = 0; $mark _img_im_y = 0; Copy watermark Picture High width $mark _img_w = imagesx ($mark _img_im); $mark _img_h = Imagesy ($mark _img_im); $org _img_w = imagesx ($org _img_im); $org _img_h = imagesx ($org _img_im); The resultant point coordinates $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 > or $alpha < 0)? : $alpha; Merged picture Imagecopymerge ($org _img_im, $mark _img_im, $org _img_im_x, $org _img_im_y, $mark _img_im_x, $mark _img_im_y, $mark _ Img_w, $mark _img_h, $alpha); Output (save) Picture 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 Canvas Imagedestroy ($org _img_im); Imagedestroy ($mark _img_im); Return Array (' flag ' = = True, ' msg ' = = '); }/** * * check picture * @param unknown_type $img _path * @return Array (' flag ' =>true/false, ' msg ' =>ext/error message) */private Func tion is_img ($img _path) {if (! file_exists ($img _path)) {return array (' flag ' = = False, ' msg ' = = ' Load picture $img _path Failed! " ); } $ext = Explode ('. ', $img _path); $ext = Strtolower (end ($ext)); if (! In_array ($ext, $this->exts)) {return array (' flag ' = = False, ' msg ' = ') ' picture $img the _path format is incorrect! " ); } Return Array (' flag ' = = True, ' msg ' = $ext); }/** * * Returns the correct picture 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 ' = ') ' picture saved $dir cannot be created! " ); }} return Array (' flag ' = = True, ' msg ' = ' = '); }}if (! empty ($_files [' Test '] [' tmp_name '])) {//Example $IMG = new img ();//Original $name = Explode ('. ', $_files [' Test '] [' N Ame ']); $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 mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1019459.html www.bkjia.com true http://www.bkjia.com/PHPjc/1019459.html techarticle Common PHP Image processing class (watermark, equal to zoom, fixed aspect) sharing, commonly used PHP image processing class (watermark, equal than zoom, fixed aspect) share PHP//php add watermark fixed ...