/filters/filter.crop.php

https://github.com/symphonycms/jit_image_manipulation · PHP · 168 lines · 133 code · 35 blank · 0 comment · 9 complexity · 21a9c2fac3b017069308a4b15639a3fb MD5 · raw file

  1. <?php
  2. class FilterCrop extends JIT\ImageFilter
  3. {
  4. public $mode = 3;
  5. const TOP_LEFT = 1;
  6. const TOP_MIDDLE = 2;
  7. const TOP_RIGHT = 3;
  8. const MIDDLE_LEFT = 4;
  9. const CENTER = 5;
  10. const MIDDLE_RIGHT = 6;
  11. const BOTTOM_LEFT = 7;
  12. const BOTTOM_MIDDLE = 8;
  13. const BOTTOM_RIGHT = 9;
  14. public static function about()
  15. {
  16. return array(
  17. 'name' => 'JIT Filter: Crop'
  18. );
  19. }
  20. public static function parseParameters($parameter_string)
  21. {
  22. $param = array();
  23. if (preg_match_all('/^(2|3)\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-F0-9]{3,6}\/)?(?:(0|1)\/)?(.+)$/i', $parameter_string, $matches, PREG_SET_ORDER)) {
  24. $param['mode'] = (int)$matches[0][1];
  25. $param['settings']['width'] = (int)$matches[0][2];
  26. $param['settings']['height'] = (int)$matches[0][3];
  27. $param['settings']['position'] = (int)$matches[0][4];
  28. $param['settings']['background'] = trim($matches[0][5], '/');
  29. $param['settings']['external'] = (bool)$matches[0][6];
  30. $param['image_path'] = $matches[0][7];
  31. }
  32. return !empty($param) ? $param : false;
  33. }
  34. public static function run(\Image $res, $settings)
  35. {
  36. $resource = $res->Resource();
  37. $dst_w = Image::width($resource);
  38. $dst_h = Image::height($resource);
  39. $width = $settings['settings']['width'];
  40. $height = $settings['settings']['height'];
  41. if (!empty($width) && !empty($height)) {
  42. $dst_w = $width;
  43. $dst_h = $height;
  44. } elseif (empty($height)) {
  45. $ratio = ($dst_h / $dst_w);
  46. $dst_w = $width;
  47. $dst_h = round($dst_w * $ratio);
  48. } elseif (empty($width)) {
  49. $ratio = ($dst_w / $dst_h);
  50. $dst_h = $height;
  51. $dst_w = round($dst_h * $ratio);
  52. }
  53. $image_width = Image::width($resource);
  54. $image_height = Image::height($resource);
  55. $tmp = imagecreatetruecolor($dst_w, $dst_h);
  56. self::__fill($resource, $tmp, $settings['settings']['background']);
  57. list($src_x, $src_y, $dst_x, $dst_y) = self::__calculateDestSrcXY($dst_w, $dst_h, $image_width, $image_height, $image_width, $image_height, $anchor);
  58. imagecopyresampled($tmp, $resource, $src_x, $src_y, $dst_x, $dst_y, $image_width, $image_height, $image_width, $image_height);
  59. if (is_resource($resource)) {
  60. imagedestroy($resource);
  61. }
  62. $res->setResource($tmp);
  63. return $res;
  64. }
  65. protected static function __calculateDestSrcXY($width, $height, $src_w, $src_h, $dst_w, $dst_h, $position = self::TOP_LEFT)
  66. {
  67. $dst_x = $dst_y = 0;
  68. $src_x = $src_y = 0;
  69. if ($width < $src_w) {
  70. $mx = array(
  71. 0,
  72. ceil(($src_w * 0.5) - ($width * 0.5)),
  73. $src_x = $src_w - $width
  74. );
  75. } else {
  76. $mx = array(
  77. 0,
  78. ceil(($width * 0.5) - ($src_w * 0.5)),
  79. $src_x = $width - $src_w
  80. );
  81. }
  82. if ($height < $src_h) {
  83. $my = array(
  84. 0,
  85. ceil(($src_h * 0.5) - ($height * 0.5)),
  86. $src_y = $src_h - $height
  87. );
  88. } else {
  89. $my = array(
  90. 0,
  91. ceil(($height * 0.5) - ($src_h * 0.5)),
  92. $src_y = $height - $src_h
  93. );
  94. }
  95. switch ($position) {
  96. case 1:
  97. break;
  98. case 2:
  99. $src_x = 1;
  100. break;
  101. case 3:
  102. $src_x = 2;
  103. break;
  104. case 4:
  105. $src_y = 1;
  106. break;
  107. case 5:
  108. $src_x = 1;
  109. $src_y = 1;
  110. break;
  111. case 6:
  112. $src_x = 2;
  113. $src_y = 1;
  114. break;
  115. case 7:
  116. $src_y = 2;
  117. break;
  118. case 8:
  119. $src_x = 1;
  120. $src_y = 2;
  121. break;
  122. case 9:
  123. $src_x = 2;
  124. $src_y = 2;
  125. break;
  126. }
  127. $a = ($width >= $dst_w ? $mx[$src_x] : 0);
  128. $b = ($height >= $dst_h ? $my[$src_y] : 0);
  129. $c = ($width < $dst_w ? $mx[$src_x] : 0);
  130. $d = ($height < $dst_h ? $my[$src_y] : 0);
  131. return array($a, $b, $c, $d);
  132. }
  133. }