Common.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace Gregwar\Image\Adapter;
  3. abstract class Common extends Adapter
  4. {
  5. /**
  6. * @inheritdoc
  7. */
  8. public function zoomCrop($width, $height, $background = 'transparent', $xPosLetter = 'center', $yPosLetter = 'center')
  9. {
  10. // Calculate the different ratios
  11. $originalRatio = $this->width() / $this->height();
  12. $newRatio = $width / $height;
  13. // Compare ratios
  14. if ($originalRatio > $newRatio) {
  15. // Original image is wider
  16. $newHeight = $height;
  17. $newWidth = (int) $height * $originalRatio;
  18. } else {
  19. // Equal width or smaller
  20. $newHeight = (int) $width / $originalRatio;
  21. $newWidth = $width;
  22. }
  23. // Perform resize
  24. $this->resize($newWidth, $newHeight, $background, true);
  25. // Define x position
  26. switch($xPosLetter) {
  27. case 'L':
  28. case 'left':
  29. $xPos = 0;
  30. break;
  31. case 'R':
  32. case 'right':
  33. $xPos = (int) $newWidth - $width;
  34. break;
  35. default:
  36. $xPos = (int) ($newWidth - $width) / 2;
  37. }
  38. // Define y position
  39. switch($yPosLetter) {
  40. case 'T':
  41. case 'top':
  42. $yPos = 0;
  43. break;
  44. case 'B':
  45. case 'bottom':
  46. $yPos = (int) $newHeight - $height;
  47. break;
  48. default:
  49. $yPos = (int) ($newHeight - $height) / 2;
  50. }
  51. // Crop image to reach desired size
  52. $this->crop($xPos, $yPos, $width, $height);
  53. return $this;
  54. }
  55. /**
  56. * Resizes the image forcing the destination to have exactly the
  57. * given width and the height
  58. *
  59. * @param int $w the width
  60. * @param int $h the height
  61. * @param int $bg the background
  62. */
  63. public function forceResize($width = null, $height = null, $background = 'transparent')
  64. {
  65. return $this->resize($width, $height, $background, true);
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function scaleResize($width = null, $height = null, $background='transparent', $crop = false)
  71. {
  72. return $this->resize($width, $height, $background, false, true, $crop);
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function cropResize($width = null, $height = null, $background='transparent')
  78. {
  79. return $this->resize($width, $height, $background, false, false, true);
  80. }
  81. /**
  82. * Fix orientation using Exif informations
  83. */
  84. public function fixOrientation()
  85. {
  86. if (!extension_loaded('exif')) {
  87. throw new \RuntimeException('You need to EXIF PHP Extension to use this function');
  88. }
  89. $exif = exif_read_data($this->source->getInfos());
  90. if(!array_key_exists('Orientation', $exif)) {
  91. return $this;
  92. }
  93. switch($exif['Orientation']) {
  94. case 1:
  95. break;
  96. case 2:
  97. $this->flip(false, true);
  98. break;
  99. case 3: // 180 rotate left
  100. $this->rotate(180);
  101. break;
  102. case 4: // vertical flip
  103. $this->flip(true, false);
  104. break;
  105. case 5: // vertical flip + 90 rotate right
  106. $this->flip(true, false);
  107. $this->rotate(-90);
  108. break;
  109. case 6: // 90 rotate right
  110. $this->rotate(-90);
  111. break;
  112. case 7: // horizontal flip + 90 rotate right
  113. $this->flip(false, true);
  114. $this->rotate(-90);
  115. break;
  116. case 8: // 90 rotate left
  117. $this->rotate(90);
  118. break;
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Opens the image
  124. */
  125. abstract protected function openGif($file);
  126. abstract protected function openJpeg($file);
  127. abstract protected function openPng($file);
  128. /**
  129. * Creates an image
  130. */
  131. abstract protected function createImage($width, $height);
  132. /**
  133. * Creating an image using $data
  134. */
  135. abstract protected function createImageFromData($data);
  136. /**
  137. * Loading image from $resource
  138. */
  139. protected function loadResource($resource)
  140. {
  141. $this->resource = $resource;
  142. }
  143. protected function loadFile($file, $type)
  144. {
  145. if (!$this->supports($type)) {
  146. throw new \RuntimeException('Type '.$type.' is not supported by GD');
  147. }
  148. if ($type == 'jpeg') {
  149. $this->openJpeg($file);
  150. }
  151. if ($type == 'gif') {
  152. $this->openGif($file);
  153. }
  154. if ($type == 'png') {
  155. $this->openPng($file);
  156. }
  157. if (false === $this->resource) {
  158. throw new \UnexpectedValueException('Unable to open file ('.$file.')');
  159. } else {
  160. $this->convertToTrueColor();
  161. }
  162. }
  163. /**
  164. * @inheritdoc
  165. */
  166. public function init()
  167. {
  168. $source = $this->source;
  169. if ($source instanceof \Gregwar\Image\Source\File) {
  170. $this->loadFile($source->getFile(), $source->guessType());
  171. } else if ($source instanceof \Gregwar\Image\Source\Create) {
  172. $this->createImage($source->getWidth(), $source->getHeight());
  173. } else if ($source instanceof \Gregwar\Image\Source\Data) {
  174. $this->createImageFromData($source->getData());
  175. } else if ($source instanceof \Gregwar\Image\Source\Resource) {
  176. $this->loadResource($source->getResource());
  177. } else {
  178. throw new \Exception('Unsupported image source type '.get_class($source));
  179. }
  180. return $this;
  181. }
  182. /**
  183. * @inheritdoc
  184. */
  185. public function resize($width = null, $height = null, $background = 'transparent', $force = false, $rescale = false, $crop = false)
  186. {
  187. $current_width = $this->width();
  188. $current_height = $this->height();
  189. $new_width = 0;
  190. $new_height = 0;
  191. $scale = 1.0;
  192. if ($height === null && preg_match('#^(.+)%$#mUsi', $width, $matches)) {
  193. $width = round($current_width * ((float)$matches[1]/100.0));
  194. $height = round($current_height * ((float)$matches[1]/100.0));
  195. }
  196. if (!$rescale && (!$force || $crop)) {
  197. if ($width!=null && $current_width>$width) {
  198. $scale = $current_width/$width;
  199. }
  200. if ($height!=null && $current_height>$height) {
  201. if ($current_height/$height > $scale)
  202. $scale = $current_height/$height;
  203. }
  204. } else {
  205. if ($width!=null) {
  206. $scale = $current_width/$width;
  207. $new_width = $width;
  208. }
  209. if ($height!=null) {
  210. if ($width!=null && $rescale) {
  211. $scale = max($scale,$current_height/$height);
  212. } else {
  213. $scale = $current_height/$height;
  214. }
  215. $new_height = $height;
  216. }
  217. }
  218. if (!$force || $width==null || $rescale) {
  219. $new_width = round($current_width/$scale);
  220. }
  221. if (!$force || $height==null || $rescale) {
  222. $new_height = round($current_height/$scale);
  223. }
  224. if ($width == null || $crop) {
  225. $width = $new_width;
  226. }
  227. if ($height == null || $crop) {
  228. $height = $new_height;
  229. }
  230. $this->doResize($background, $width, $height, $new_width, $new_height);
  231. }
  232. /**
  233. * Trim background color arround the image
  234. *
  235. * @param int $bg the background
  236. */
  237. protected function _trimColor($background='transparent')
  238. {
  239. $width = $this->width();
  240. $height = $this->height();
  241. $b_top = 0;
  242. $b_lft = 0;
  243. $b_btm = $height - 1;
  244. $b_rt = $width - 1;
  245. //top
  246. for(; $b_top < $height; ++$b_top) {
  247. for($x = 0; $x < $width; ++$x) {
  248. if ($this->getColor($x, $b_top) != $background) {
  249. break 2;
  250. }
  251. }
  252. }
  253. // bottom
  254. for(; $b_btm >= 0; --$b_btm) {
  255. for($x = 0; $x < $width; ++$x) {
  256. if ($this->getColor($x, $b_btm) != $background) {
  257. break 2;
  258. }
  259. }
  260. }
  261. // left
  262. for(; $b_lft < $width; ++$b_lft) {
  263. for($y = $b_top; $y <= $b_btm; ++$y) {
  264. if ($this->getColor($b_lft, $y) != $background) {
  265. break 2;
  266. }
  267. }
  268. }
  269. // right
  270. for(; $b_rt >= 0; --$b_rt) {
  271. for($y = $b_top; $y <= $b_btm; ++$y) {
  272. if ($this->getColor($b_rt, $y) != $background) {
  273. break 2;
  274. }
  275. }
  276. }
  277. $b_btm++;
  278. $b_rt++;
  279. $this->crop($b_lft, $b_top, $b_rt - $b_lft, $b_btm - $b_top);
  280. }
  281. /**
  282. * Resizes the image to an image having size of $target_width, $target_height, using
  283. * $new_width and $new_height and padding with $bg color
  284. */
  285. abstract protected function doResize($bg, $target_width, $target_height, $new_width, $new_height);
  286. /**
  287. * Gets the color of the $x, $y pixel
  288. */
  289. abstract protected function getColor($x, $y);
  290. /**
  291. * @inheritdoc
  292. */
  293. public function enableProgressive(){
  294. throw new \Exception('The Adapter '.$this->getName().' does not support Progressive Image loading');
  295. }
  296. }