Common PHP Image Processing class

Source: Internet
Author: User
Tags create directory explode imagecopy imagejpeg

This article mainly summarizes two commonly used PHP image processing class (watermark, scaling, fixed width), very simple and practical, the need for small partners can refer to the

Common PHP Image Processing class (watermark, equal ratio scaling, fixed width) sharing

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 <?php//php Add watermark & Proportional thumbnails & fixed height & fixed width class. Class image_process{Public $source//original artwork public $source _width//original width public $source _height;//original height public $source _type_i D Public $orign _name; Public $orign _dirname;  //Incoming Artwork 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);  //Judge the format of the file of the picture, return the encoded public function Judgetype ($type, $source) of the PHP can be identified {if ($type = = 1) {Returns Imagecreatefromgif ($ SOURCE); GIF}else if ($type = = 2) {return imagecreatefromjpeg ($source);//jpg}else if ($type = = 3) {return imagecreatefrompng ($so Urce); PNG}else{return false;}  //Generate Watermark Picture 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);  //Proportional scaling picture public function scaleimage ($scale) {$width = Floor ($this->source_width * $scale); $height = Floor ($t His->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, $this-& Gt;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); Imagedestroy ($handle);}} Save Picture Private Function SaveImage ($image, $url) {if (IMAgejpeg ($image, $url)) {return true;}} } $imgHandle = new image_process (' d:appservwwwtestgetimg14061907445601.jpg '); $imgHandle->watermakeimage (' d:appservwwwtestgetimgshougongke.png '); Generate watermark Picture//$imgHandle->fixsizeimage (200,150); Fixed length picture $imgHandle->scaleimage (0.2); Equal scale scaling?>

Example two:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 30 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359-360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 <?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 exc Eption (' Loading GD library failed! ' ); }  /** * * cropping compression * @param $src _img picture * @param $save _img picture * @param $option parameter options, including: $maxwidth width $maxheight height * Array (' width ' =>xx, ' height ' =>xxx) * @internal * Our general compression image method, the picture is too long or too wide to generate the picture * will be "compressed", for this should be used first cut after the 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 image 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->chec K_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 original shape cropped image) $dst _scale = $option [' height ']/$option [' width ']; The aspect ratio of target image $src _scale = $src _h/$src _w; The 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 {//too wide $h = intval ($src _h) $w = Intval ($h/$dst _scale)   $x = ($src _w-$w)/2; $y = 0;}//Trim $CR OpEd = 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 ' => ');  /** * Scaling image * @param $src _img Original picture * @param $save where _img needs to be saved * @param $option parameters set Array (' Width ' =>xx, ' Heig HT ' =>xxx ' */function Resize_image ($src _img, $save _img = ', $option) {$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 ']) {retur n $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 [' He Ight '] && $src _h > $option [' height ']) {$heightratio = $option [' height ']/$src _h; $resizeheight _tag = true; }   if ($resizewidth _tag && $resizeheight _tag) {if ($widthratio < $heightratio) $ratio = $widthratio; els e $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); imagecopyr Esampled ($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, $newhe ight, $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 [' s Ave_func '] ($newim); } Imagedestroy ($newim); Return Array (' flag ' => True, ' msg ' => ');  /** * * Generate watermark Picture * @param $org _img Original image * @param $mark _img watermark Mark image * @param $save _img When its directory does not exist, try to create a directory * @param array $option some basic settings for watermarks include: * x: Watermark horizontal position, default to minus the watermark width after the value * y: Watermark vertical position, default to minus the watermark height after the value of * Alpha:alpha value (control transparency), the default is */Public function Water_mark ($org _img, $mark _img, $save _img = ", $option = Array ()) {//check picture $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 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 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 image 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);  //Synthetic generation 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;  //Merge pictures 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 ' => ');  }  /** * * * @param unknown_type $img _path * @return Array (' flag ' =>true/false, ' msg ' =>ext/error message) * * Private Function is_img ($img _path) {if (! file_exists ($img _path)) {return array (' flag ' => False, ' msg ' => ' load map Film $img _path failure! " ); } $ext = Explode ('. ', $img _path); $ext = Strtolower (end ($ext)); if (! In_array ($ext, $this->exts)) {return array (' flag ' => False, ' msg ' => ' picture $img _path is not in the correct format!) " ); } 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 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 save directory $dir cannot be created!) " ); } } Return Array (' flag ' => True, ' msg ' => '); }   if (! empty ($_files [' Test '] [' tmp_name '])} { Example $img = new img (); Artwork $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 the entire contents of this article, I hope you can enjoy.

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.