Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /**
  2. * Applies an image effects blur, scale and crop.
  3. *
  4. * The result is am image with 2 layers:
  5. * - background: a cropped picture with a blur effect.
  6. * - foreground: overlay with the same image and scale effect.
  7. *
  8. * @param int $width
  9. * Result image width.
  10. * @param int $height
  11. * Result image height.
  12. * @param int $radius
  13. * Result image blur radius.
  14. */
  15. public function blurScaleAndCrop($width, $height, $radius) {
  16. $image = $this->getImageFactory()->get($this->path);
  17. if (!$image->isValid()) {
  18. throw new InvalidArgumentException("Image {$this->path} is not valid");
  19. }
  20.  
  21. // Create the background image.
  22. $image->scaleAndCrop($width, $height);
  23. /** @var DrupalsystemPluginImageToolkitGDToolkit $toolkit */
  24. $toolkit = $image->getToolkit();
  25. $resource = $toolkit->getResource();
  26. // Apply blur.
  27. if ($radius) {
  28. for ($i = 0; $i < $radius; $i++) {
  29. imagefilter($resource, IMG_FILTER_GAUSSIAN_BLUR);
  30. }
  31. }
  32. $image->apply('gaussian-blur', ['radius' => $radius]);
  33.  
  34. // Create the foreground image.
  35. $foreground = $this->getImageFactory()->get($this->path);
  36. if (!$foreground->isValid()) {
  37. throw new InvalidArgumentException("Image {$this->path} is not valid");
  38. }
  39. $foreground->scale($width, $height);
  40.  
  41. // Merge the 2 image layers.
  42. $dst_im = $image->getToolkit()->getResource();
  43. $src_im = $foreground->getToolkit()->getResource();
  44. $f_width = $foreground->getWidth();
  45. $f_height = $foreground->getHeight();
  46. $x = ($width - $f_width) / 2;
  47. $y = ($height - $f_height) / 2;
  48. imagecopy($dst_im, $src_im, $x, $y, 0, 0, $f_width, $f_height);
  49. $image->save();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement