PHP generated verification code, thumbnails and Watermark map class sharing, Verification code watermark
Encapsulates a class that generates verification codes, thumbnails, and watermarks to share with everyone
<?phpclass image{session_start (); Verification code class static public function verify ($code, $width =75, $height =25, $n =4) {header ("content-type:image/png"); Create Canvas $img =imagecreatetruecolor ($width, $height); Set Background color $bgcolor =imagecolorallocate ($img, Mt_rand (200,255), Mt_rand (200,255), Rand (200,255)); Fills the background color Imagefill ($img, 0,0, $bgcolor); Draw five arcs for ($i =0; $i <5; $i + +) {$arccolor =imagecolorallocate ($img, Mt_rand (200,255), Mt_rand (200,255), Rand (200,25 5)); Imagearc ($img, Mt_rand (5, ($width-5)), Mt_rand (5, ($height-5)), Mt_rand (5, ($width-5)), Mt_rand (5, ($height-5)), Mt_ Rand (0,360), Mt_rand (0,360), $arccolor); }//Draw 100 dots for ($i =0; $i <100; $i + +) {$pixelcolor =imagecolorallocate ($img, Mt_rand (200,255), Mt_rand (200,255), ran D (200,255)); Imagesetpixel ($img, Mt_rand (1, ($width-1)), Mt_rand (1, ($height-1)), $pixelcolor); }//Draw five line segments for ($i =0; $i <5; $i + +) {$linecolor =imagecolorallocate ($img, Mt_rand (200,255), Mt_rand (200,255), Rand (2 00,255)); Imageline($img, Mt_rand (1, ($width-1)), Mt_rand (1, ($height-1)), Mt_rand (1, ($width-1)), Mt_rand (1, ($height-1)), $linecolor); }//Set Border color $bdcolor =imagecolorallocate ($img, Mt_rand (150,200), Mt_rand (150,200), Rand (150,200)); Draws a rectangle without a fill border imagerectangle ($img, 0,0, ($width-1), ($height-1), $bdcolor); Set the Authenticode string $str = '; for ($i =1; $i <= $n; $i + +) {$str. =substr (Str_shuffle ($code), 0, 1); } $_session[' a ']= $str; for ($i =0; $i < $n; $i + +) {$textcolor =imagecolorallocate ($img, Mt_rand (100,150), Mt_rand (100,150), Mt_rand (100,150 )); Imagettftext ($img, 1/($n + 1) * $width, Mt_rand ( -20,20), ((1-$n/(5+ $n)) * $width/($n-1) + $i *1/($n + 1) * $width), 1/2* ($ height+3/5* $height), $textcolor, ' 1.ttf ', substr ($str, $i, 1)); }//Print image imagepng ($img); Release Resource Imagedestroy ($IMG); return $str; }//Thumbnail class//* $source need thumbnail image */static public function thumbnail ($source, $deletesource =false, $width =180) {$info =get ImageSize ($source); The GetImageSize method obtains detailed information about the image $createFun =str_replace ('/', ' CreateFrom ', $info [' mime ']); Change/Replace in [' MIME '] in GetImageSize to Imagecreatefromjpeg method $src = $createFun ($source); Create image $dst _w= $width; The width of the thumbnail $dst _h= $width/$info [0]* $info [1]; Calculates the high $dst =imagecreatetruecolor of thumbnails ($dst _w, $dst _h); Create a new True color image imagecopyresampled ($DST, $SRC, 0,0,0,0, $dst _w, $dst _h, $info [' 0 '], $info [' 1 ']); Resample copy part of image and resize $saveFun =str_replace ('/', ' ', $info [' mime ']); Set Save function $ext =strrchr ($source, '. '); STRRCHR finds the last occurrence of a specified character in a string, returning the remaining string including itself $thumbnailName =str_replace ($ext, ', $source). ' _thumbnail '. $ext; Set save path if (! $deletesource) {$saveFun ($DST, $thumbnailName); Save Image}else{$saveFun ($DST, $thumbnailName); Unlink ($source); Delete original} Imagedestroy ($SRC); Destruction of resources Imagedestroy ($DST); Destruction of resources return $thumbnailName; }//Watermark class */* $srcing watermark * $dsting Original */static public function watermark ($dstimg, $srcimg) {$dstinfo =getimagesize ($d STIMG); The GetImageSize method gets the details of the picture (this method returns a detailedFine array) $srcinfo =getimagesize ($srcimg); $CREATEDST =str_replace ('/', ' CreateFrom ', $dstinfo [' mime ']); The/substitution of the MIME value in the array returned by the GetImageSize method results in Imagecreatefromjpeg $createsrc =str_replace ('/', ' CreateFrom ', $srcinfo [' MIME ']); $DST = $CREATEDST ($dstimg); Replace with imagecreatefromjpeg function, create an image $dst _w=imagesx ($DST); Gets the wide $dst _h=imagesy ($DST) of the large image; Gets the high $src of the large image = $createsrc ($srcimg); Replace with imagecreatefromjpeg function, create an image $src _w=imagesx ($SRC); Gets the width $src _h=imagesy ($src) of the watermark map; Get the High $watermaker =imagecopy ($DST, $src, ($dst _w-$src _w), ($dst _h-$src _h), 0,0, $src _w, $src _h) of the watermark map; Copy the watermark on a larger image $saveFun =str_replace ('/', ', $dstinfo [' mime ']); Replace the slash in [' MIME '] in the getimagesize return array with a null, substituting a value of imagejpeg, which is the Save function $ext =strrchr ($dstimg, '. '); STRRCHR finds the last occurrence of a specified character in a string, returning the remaining string including itself $watermaker =str_replace ($ext, ', $dstimg). ' _water '. Time (). $ext; Save name $saveFun ($DST, $watermaker); Save the picture, the first parameter is the picture to save, the second parameter is save path or save name Imagedestroy ($DST); Destruction of resources Imagedestroy ($sRC); Destruction of resources return $watermaker; }}?>
Articles you may be interested in:
- PHP Image Captcha Code
- PHP dynamic random Generation code for verification code
- PHP Verification Code Class code (the latest modification, fully customizable!)
- A nice PHP verification code class (share)
- A workaround for "The image cannot be displayed because of its own error" when PHP generates a verification code
- PHP Generate image Verification code, click to switch instances
- PHP Generate Captcha function
- thinkphp3.2 Click Refresh to generate the verification code
- PHP Implementation Generation Verification code instance sharing
http://www.bkjia.com/PHPjc/1119993.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119993.html techarticle PHP generated verification code, thumbnails and watermark class sharing, verification code watermark encapsulated a class, can generate verification code, thumbnails, and watermark map, share to everyone phpclass image{sessio ...