PHP generated verification code, thumbnails and Watermark diagram class examples of detailed

Source: Internet
Author: User
Tags border color set background
This article mainly for you to share is the PHP generation verification code, thumbnails, and watermark diagram of the class, very practical, the need for small partners can refer to the next

<?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; }}?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

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.