Example of a PHP method to generate a fuzzy image,

Source: Internet
Author: User

Example of a PHP method to generate a fuzzy image,

This example describes how to generate a fuzzy image using PHP. We will share this with you for your reference. The details are as follows:

<? Phpclass image_blur {/*** image Gaussian Blur (applicable to png/jpg/gif formats) * @ param $ srcImg original image * @ param $ savepath save path * @ param $ savename save name * @ param $ positon blur degree ** added based on the Martijn Frazer code, thanks to Martijn Frazer */public function gaussian_blur ($ srcImg, $ savepath = null, $ savename = null, $ blurFactor = 3) {$ gdImageResource = $ this-> image_create_from_ext ($ srcImg); $ srcImgObj = $ this-> blur ($ gdImageResource, $ blurFactor); $ temp = pathinfo ($ SrcImg); $ name = $ temp ['basename']; $ path = $ temp ['dirname']; $ exte = $ temp ['extension']; $ savename = $ savename? $ Savename: $ name; $ savepath = $ savepath? $ Savepath: $ path; $ savefile = $ savepath. '/'. $ savename; $ srcinfo = @ getimagesize ($ srcImg); switch ($ srcinfo [2]) {case 1: imagegif ($ srcImgObj, $ savefile); break; case 2: imagejpeg ($ srcImgObj, $ savefile); break; case 3: imagepng ($ srcImgObj, $ savefile); break; default: return 'failed to save '; // save Failed} return $ savefile; imagedestroy ($ srcImgObj);}/*** Strong Blur *** @ param $ gdImageResource image resource * @ param $ BlurFactor optional blur degree * optional blur degree 0 use 3 default over 5 extreme blur * @ return GD image resource type * @ author Martijn Frazer, idea based on http://stackoverflow.com/a/20264482 */private function blur ($ gdImageResource, $ blurFactor = 3) {// blurFactor has to be an integer $ blurFactor = round ($ blurFactor ); $ originalWidth = imagesx ($ gdImageResource); $ originalHeight = imagesy ($ gdImageResource); $ smallestWidth = ceil ($ origi NalWidth * pow (0.5, $ blurFactor); $ smallestHeight = ceil ($ originalHeight * pow (0.5, $ blurFactor); // for the first run, the previous image is the original input $ prevImage = $ gdImageResource; $ prevWidth = $ originalWidth; $ prevHeight = $ originalHeight; // scale down and gradually scale back up, blurring all the way for ($ I = 0; $ I <$ blurFactor; $ I + = 1) {// determine dimensions of next image $ NextWidth = $ smallestWidth * pow (2, $ I); $ nextHeight = $ smallestHeight * pow (2, $ I ); // resize previous image to next size $ nextImage = imagecreatetruecolor ($ nextWidth, $ nextHeight); imagecopyresized ($ nextImage, $ prevImage, 0, 0, 0, 0, $ nextWidth, $ nextHeight, $ prevWidth, $ prevHeight); // apply blur filter imagefilter ($ nextImage, IMG_FILTER_GAUSSIAN_BLUR); // now the new image becomes the previo Us image for the next step $ prevImage = $ nextImage; $ prevWidth = $ nextWidth; $ prevHeight = $ nextHeight ;} // scale back to original size and blur one more time imagecopyresized ($ gdImageResource, $ nextImage, 0, 0, 0, 0, $ originalWidth, $ originalHeight, $ nextWidth, $ nextHeight); imagefilter ($ gdImageResource, IMG_FILTER_GAUSSIAN_BLUR); // clean up imagedestroy ($ prevImage); // return result return $ gd ImageResource;} private function image_create_from_ext ($ imgfile) {$ info = getimagesize ($ imgfile); $ im = null; switch ($ info [2]) {case 1: $ im = imagecreatefromgif ($ imgfile); break; case 2: $ im = imagecreatefromjpeg ($ imgfile); break; case 3: $ im = imagecreatefrompng ($ imgfile); break ;} return $ im ;}$ image_blur = new image_blur (); $ image_blur-> gaussian_blur (". /1.jpg", null, null, 3);?>

Original image effect:

Effect of generating a blurred image:

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.