Skip to content

Instantly share code, notes, and snippets.

@AlexR1712
Created October 23, 2017 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexR1712/73cb183bb316a0b6c27267f3f5d3c131 to your computer and use it in GitHub Desktop.
Save AlexR1712/73cb183bb316a0b6c27267f3f5d3c131 to your computer and use it in GitHub Desktop.
gaussian blur in one sector of the image PHP
<?php
$loadFile = 'https://c1.staticflickr.com/5/4094/4809804115_40f52c8f25_b.jpg';
$image = imagecreatefromstring(file_get_contents($loadFile));
if ($image !== false) {
$face = imagecrop($image, ['x' => 152, 'y' => 568, 'width' => 199, 'height' => 199]);
/* Get original image size */
$w = 199;
$h = 199;
/* Create array with width and height of down sized images */
$size = ['sm' => ['w' => intval($w / 4), 'h' => intval($h / 4)],
'md' => ['w' => intval($w / 2), 'h' => intval($h / 2)],
];
/* Scale by 25% and apply Gaussian blur */
$sm = imagecreatetruecolor($size['sm']['w'], $size['sm']['h']);
imagecopyresampled($sm, $face, 0, 0, 0, 0, $size['sm']['w'], $size['sm']['h'], $w, $h);
for ($x = 1; $x <= 2; $x++) { //Should be higher than 1
imagefilter($sm, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($sm, IMG_FILTER_SMOOTH, 99);
imagefilter($sm, IMG_FILTER_BRIGHTNESS, 10);
/* Scale result by 200% and blur again */
$md = imagecreatetruecolor($size['md']['w'], $size['md']['h']);
imagecopyresampled($md, $sm, 0, 0, 0, 0, $size['md']['w'], $size['md']['h'], $size['sm']['w'], $size['sm']['h']);
imagedestroy($sm);
for ($x = 1; $x <= 1; $x++) { //Should be higher than 1
imagefilter($md, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($md, IMG_FILTER_SMOOTH, 99);
imagefilter($md, IMG_FILTER_BRIGHTNESS, 10);
/* Scale result back to original size */
imagecopyresampled($face, $md, 0, 0, 0, 0, $w, $h, $size['md']['w'], $size['md']['h']);
imagecopymerge($image, $face, 152, 568, 0, 0, 199, 199, 100);
imagepng($face, 'face.png');
imagepng($image, 'image.png');
imagedestroy($face);
imagedestroy($image);
} else {
echo 'An error occurred.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment