You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EffectsBusiness.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Http\Business;
  3. use App\Http\Business\Filters\AbstractFilter;
  4. use App\Http\DBO\EffectApplyDbo;
  5. use Exception;
  6. use ImagickPixel;
  7. use Imagine\Image\Point;
  8. use Luticate\Utils\LuBusiness;
  9. use App\Http\DataAccess\EffectsDataAccess;
  10. class EffectsBusiness extends LuBusiness {
  11. protected static function getDataAccess()
  12. {
  13. return new EffectsDataAccess();
  14. }
  15. public static function apply(EffectApplyDbo $data)
  16. {
  17. if (!preg_match('/^[a-zA-Z]+$/', $data->getEffect())) {
  18. self::badInput("Invalid effect");
  19. }
  20. $className = 'App\Http\Business\Filters\\' . $data->getEffect() . 'Filter';
  21. if (!class_exists($className)) {
  22. self::notFound("Effect does not exist");
  23. }
  24. /**
  25. * @var $filter AbstractFilter
  26. */
  27. $filter = new $className();
  28. $filter->apply($data);
  29. return $data;
  30. }
  31. public static function effectBlackAndWhite(EffectApplyDbo $data) {
  32. $image = $data->getImage();
  33. $iterator = $image->getPixelIterator();
  34. foreach ($iterator as $row => $pixels) {
  35. /**
  36. * @var $pixel ImagickPixel
  37. */
  38. foreach ( $pixels as $col => $pixel ) {
  39. $color = $pixel->getColor();
  40. $g = 0.299 * $color["r"] + 0.587 * $color["g"] + 0.114 * $color["b"];
  41. $g = ($g < 127 ? 0 : 255);
  42. $pixel->setColor("rgb($g, $g, $g)");
  43. }
  44. $iterator->syncIterator();
  45. }
  46. }
  47. public static function effectGrayScale(EffectApplyDbo $data) {
  48. $image = $data->getImage();
  49. $iterator = $image->getPixelIterator();
  50. foreach ($iterator as $row => $pixels) {
  51. /**
  52. * @var $pixel ImagickPixel
  53. */
  54. foreach ( $pixels as $col => $pixel ) {
  55. $color = $pixel->getColor();
  56. $g = 0.299 * $color["r"] + 0.587 * $color["g"] + 0.114 * $color["b"];
  57. $pixel->setColor("rgb($g, $g, $g)");
  58. }
  59. $iterator->syncIterator();
  60. }
  61. }
  62. }