note 114750 added to function.imagefilter

From: Date: Tue, 01 Apr 2014 17:03:38 +0000
Subject: note 114750 added to function.imagefilter
Groups: php.notes 
Request: Send a blank email to php-notes+get-199095@lists.php.net to get a copy of this message
I needed an especially strong blur effect today and had a hard time achieving adequate results with the built-in IMG_FILTER_GAUSSIAN_BLUR filter. In order to achieve the strength of the blur I required I had to repeat the filter up to 100 times, which took way too long to be acceptable. After a bit of searching, I found this answer to be quite a good solution to this problem: http://stackoverflow.com/a/20264482 Based on that technique, I wrote the following generic function to achieve a very strong blur in a reasonable amount of processing: <?php /** * Strong Blur * * @param resource $gdImageResource * @param int $blurFactor optional * This is the strength of the blur * 0 = no blur, 3 = default, anything over 5 is extremely blurred * @return GD image resource * @author Martijn Frazer, idea based on http://stackoverflow.com/a/20264482 */ function blur($gdImageResource, $blurFactor = 3) { // blurFactor has to be an integer $blurFactor = round($blurFactor); $originalWidth = imagesx($gdImageResource); $originalHeight = imagesy($gdImageResource); $smallestWidth = ceil($originalWidth * 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 way 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 previous 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 $gdImageResource; } ?> ---- Server IP: 80.64.47.13 Probable Submitter: 83.37.239.77 ---- Manual Page -- http://php.net/manual/en/function.imagefilter.php Edit -- https://master.php.net/note/edit/114750 Del: integrated -- https://master.php.net/note/delete/114750/integrated Del: useless -- https://master.php.net/note/delete/114750/useless Del: bad code -- https://master.php.net/note/delete/114750/bad+code Del: spam -- https://master.php.net/note/delete/114750/spam Del: non-english -- https://master.php.net/note/delete/114750/non-english Del: in docs -- https://master.php.net/note/delete/114750/in+docs Del: other reasons-- https://master.php.net/note/delete/114750 Reject -- https://master.php.net/note/reject/114750 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199095) next »