This example for everyone to share the PHP watermark and thumbnail Production code, the use of object-oriented methods to achieve common image format jpg,png,gif watermark and thumbnail production, for your reference, the specific content as follows
<?phpheader (' content-type:text/html;charset=utf-8 ');/* To change this license header, choose License Headers in Proj ECT Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. *///add watermark to the Image class water{//Open watermark private $watermark _on = ' 1 '; Public $water _img; Watermark Location Public $pos = 1; Compressed than public $pct = 80; Transparency public $quality = 80; Public $text = ' fun net zlblog.sinaapp.com '; Public $size = 12; Public $color = ' #000000 '; Public $font = ' Font.ttf '; The production of thumb//default thumbnail function opens private $thumb _on = 1; How to generate thumbnails public $thumb _type = 1; The width of the generated thumbnail public $thumb _width; The height of the generated thumbnail public $thumb _height; The suffix name of the generated thumbnail is public $thumb _fix = ' _DQ '; The thumbnail function handles the public function thumb ($img, $outfile = ", $t _type=", $t _w= ", $t _h=") {//Verify that the picture meets the requirements if (! $this->check ($img ) || ! $this->thumb_on) return FALSE; Defines the initial value of the thumbnail $t _type = $t _type? $t _type: $this->thumb_type; $t _w = $t _w? $t _w: $this->thumb_width; $t _h = $t _h? $t _h: $this->thumb_height; Get information about the original image $img _info = getimagesize ($img); $img _w = $img _info[0]; $img _h = $img _info[1]; Gets the file suffix of the image type $img _type = image_type_to_extension ($img _info[2]); Get the relevant dimensions $thumb _size = $this->thumb_size ($img _w, $img _h, $t _w, $t _h, $t _type); Determine the original image type//use a custom function to achieve the determination of the type of picture $func = "Imagecreatefrom". substr ($img _type, 1); $res _img = $func ($img); Thumbnail resource Edit Picture resource Moon if ($img _type = = '. gif ' | | $img _type = = '. png ') {$res _thumb = imagecreate ($thumb _size[0], $thumb _ SIZE[1]); $color = Imagecolorallocate ($res _thumb, 255, 0, 0); }else{$res _thumb = Imagecreatetruecolor ($thumb _size[0], $thumb _size[1]); }//Make thumbnails if (function_exists ("imagecopyresampled")) {imagecopyresampled ($res _thumb, $res _img, 0, 0, 0, 0, $thumb _size[0], $thumb _size[1], $thumb _size[2], $thumb _size[3]); }else{imagecopyresized ($res _thumb, $res _img, 0, 0, 0, 0, $thumb _size[0], $thumb _size[1], $thumb _size[2], $thumb _size[3] ); }//Handle transparent Color if ($img _type = = '. gif ' | | $img _type = = '.PNG ') {imagecolortransparent ($res _thumb, $color); }//Configure output file name $outfile = $outfile? $outfile: $outfile. substr ($img, 0,strripos ($img, '. ')). $this->thumb_fix. $img _type; The saved output of the file $func = "image". substr ($img _type, 1); $func ($res _thumb, $outfile); if (Isset ($res _thumb)) Imagedestroy ($res _thumb); if (Isset ($res _img)) Imagedestroy ($res _img); return $outfile; } Public Function watermark ($img, $pos = ", $out _img=", $water _img= ", $text =") {if (! $this->check ($img) | |! $this-&G T;WATERMARK_ON) return false; $water _img = $water _img? $water _img: $this->water_img; The opening state of the watermark $waterimg _on = $this->check ($water _img)? 1:0; Determine if the artwork is operating $out _img = $out _img? $out _img: $img; Determine the location of the watermark $pos = $pos? $pos: $this->pos; Watermark Text $text = $text? $text: $this->text; $img _info = getimagesize ($img); $img _w = $img _info[0]; $img _h = $img _info[1]; Determine the type of watermark picture if ($waterimg _on) {$w _info = getimagesize ($water _img); $w _w = $w _info[0]; $w _h = $w_INFO[1]; if ($img _w < $w _w | | $img _h < $w _h) return false; Switch ($w _info[2]) {Case 1: $w _img = imagecreatefromgif ($water _img); Break Case 2: $w _img = imagecreatefromjpeg ($water _img); Break Case 3: $w _img = imagecreatefrompng ($water _img); Break }}else{if (Empty ($text) | | | strlen ($this->color)!=7) return FALSE; $text _info = Imagettfbbox ($this->size, 0, $this->font, $text); $w _w = $text _info[2]-$text _info[6]; $w _h = $text _info[3]-$text _info[7]; }//create original source switch ($img _info[2]) {Case 1: $res _img = Imagecreatefromgif ($img); Break Case 2: $res _img = Imagecreatefromjpeg ($img); Break Case 3: $res _img = Imagecreatefrompng ($img); Break }//Determine the location of the watermark switch ($pos) {Case 1: $x = $y = 25; Break Case 2: $x = ($img _w-$w _w)/2; $y = 25; Break Case 3: $x = $img _w-$w _w; $y = 25; Break Case 4: $x = 25; $y = ($img _h-$w _h)/2; Break Case5: $x = ($img _w-$w _w)/2; $y = ($img _h-$w _h)/2; Break Case 6: $x = $img _w-$w _w; $y = ($img _h-$w _h)/2; Break Case 7: $x = 25; $y = $img _h-$w _h; Break Case 8: $x = ($img _w-$w _w)/2; $y = $img _h-$w _h; Break Case 9: $x = $img _w-$w _w; $y = $img _h-$w _h; Break Default: $x = Mt_rand ($img _w-$w _w); $y = Mt_rand ($img _h-$w _h); }//write the picture resource if ($waterimg _on) {imagecopymerge ($res _img, $w _img, $x, $y, 0, 0, $w _w, $w _h, $this->pct); }else{$r = Hexdec (substr ($this->color,)); $g = Hexdec (substr ($this->color, 3,2)); $b = Hexdec (substr ($this->color, 5,2)); $color = Imagecolorallocate ($res _img, $r, $g, $b); Imagettftext ($res _img, $this->size, 0, $x, $y, $color, $this->font, $text); }//Generate picture type switch ($img _info[2]) {case 1:imagecreatefromgif ($res _img, $out _img); Break Case 2://imagecreatefromjpeg ($res _img, $out _img); Imagejpeg ($res _img, $out _img); Break Case3:imagepng ($res _img, $out _img); Break } if (Isset ($res _img)) Imagedestroy ($res _img); if (Isset ($w _img)) Imagedestroy ($w _img); return TRUE;} Verify that the picture exists with the private function check ($img) {$type = Array ('. jpg ', '. jpeg ', '. png ', '. gif '); $img _type = Strtolower (STRRCHR ($img, '. ')); Return extension_loaded (' gd ') && file_exists ($img) && In_array ($img _type, $type); }//Get the relevant proportions of the thumbnail//get to the processing type of the picture private function thumb_size ($img _w, $img _h, $t _w, $t _h, $t _type) {//define thumbnail size $w = $t _w; $h = $t _h; Defines the original size of the picture $cut _w = $img _w; $cut _h = $img _h; When you want the target image to be smaller than the thumbnail size, if ($img _w <= $t _w && $img _h < $t _h) {$w = $img _w; $h = $img _h; }else{if (!empty ($t _type) && $t _type>0) {switch ($t _type) {//When width is fixed case 1: $h = $t _ w/$img _w* $img _h; Break Case 2: $w = $t _h/$img _h* $img _w when height is fixed; Break Width fixed, height cut case 3: $cut _h = $img _w/$t _w* $t _h; Break Height fixed, width cutCut case 4: $cut _w = $img _h/$t _h* $t _w; Break Proportional scaling Default:if (($img _w/$t _w) > ($img _h/$t _h) {$h = $t _w/$img _w* $t _h; }elseif (($img _w/$t _w) < ($img _h/$t _h)) {$w = $t _h/$img _h* $t _w; }else{$w = $t _w; $h = $t _h; }}}} $arr [0] = $t _w; $arr [1] = $t _h; $arr [2] = $cut _w; $arr [3] = $cut _h; return $arr; }}
The above is the whole content of this article, I hope that you learn PHP programming help.
The above describes the PHP date format PHP Implementation of common image format watermark and thumbnail production object-oriented, including the PHP date format content, I hope that the PHP tutorial interested in a friend helpful.