$width)$maxwidth = $width; if($maxheight>$height)$maxheight = $height; } if ($height > $width) { $ratio = $maxheight / $height; $newheight = $maxheight; $newwidth = $width * $ratio; } else { $ratio = $maxwidth / $width; $newwidth = $maxwidth; $newheight = $height * $ratio; } $newimg = imagecreatetruecolor($newwidth,$newheight); $palsize = ImageColorsTotal($img); for ($i = 0; $i < $palsize; $i++) { $colors = ImageColorsForIndex($img, $i); ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']); } imagefill($newimg, 0, 0, IMG_COLOR_TRANSPARENT); imagesavealpha($newimg,true); imagealphablending($newimg, true); imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); $img = $newimg; } /** * 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) { if(!$blurFactor) $blurFactor = 3; if($blurFactor>6) $blurFactor = 6; else if($blurFactor<0) $blurFactor = 0; // 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; } function filter(&$im,$vars) { foreach($vars as $var) { if(strpos($var,'_')) { $a = explode('_',$var); $var = $a[0]; $val = $a[1]; } switch($var) { case 'negative': imagefilter($im,IMG_FILTER_NEGATE); break; case 'grayscale': imagefilter($im,IMG_FILTER_GRAYSCALE); break; case 'brightness': imagefilter($im,IMG_FILTER_BRIGHTNESS,$val); break; case 'edgedetect': imagefilter($im,IMG_FILTER_EDGEDETECT); break; case 'smooth': imagefilter($im,IMG_FILTER_SMOOTH,$val); break; case 'contrast': imagefilter($im,IMG_FILTER_CONTRAST,$val); break; case 'pixelate': imagefilter($im,IMG_FILTER_PIXELATE,$val); break; case 'blur': $this->blur($im,$val); break; } } } }