Common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. * Read exif rotation from file and apply it.
  83. */
  84. public function fixOrientation()
  85. {
  86. if (!in_array(exif_imagetype($this->source->getInfos()), array(
  87. IMAGETYPE_JPEG,
  88. IMAGETYPE_TIFF_II,
  89. IMAGETYPE_TIFF_MM,
  90. ))) {
  91. return $this;
  92. }
  93. if (!extension_loaded('exif')) {
  94. throw new \RuntimeException('You need to EXIF PHP Extension to use this function');
  95. }
  96. $exif = @exif_read_data($this->source->getInfos());
  97. if ($exif === false || !array_key_exists('Orientation', $exif)) {
  98. return $this;
  99. }
  100. return $this->applyExifOrientation($exif['Orientation']);
  101. }
  102. /**
  103. * Apply orientation using Exif orientation value.
  104. */
  105. public function applyExifOrientation($exif_orienation)
  106. {
  107. switch ($exif_orienation) {
  108. case 1:
  109. break;
  110. case 2:
  111. $this->flip(false, true);
  112. break;
  113. case 3: // 180 rotate left
  114. $this->rotate(180);
  115. break;
  116. case 4: // vertical flip
  117. $this->flip(true, false);
  118. break;
  119. case 5: // vertical flip + 90 rotate right
  120. $this->flip(true, false);
  121. $this->rotate(-90);
  122. break;
  123. case 6: // 90 rotate right
  124. $this->rotate(-90);
  125. break;
  126. case 7: // horizontal flip + 90 rotate right
  127. $this->flip(false, true);
  128. $this->rotate(-90);
  129. break;
  130. case 8: // 90 rotate left
  131. $this->rotate(90);
  132. break;
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Opens the image.
  138. */
  139. abstract protected function openGif($file);
  140. abstract protected function openJpeg($file);
  141. abstract protected function openPng($file);
  142. abstract protected function openWebp($file);
  143. /**
  144. * Creates an image.
  145. */
  146. abstract protected function createImage($width, $height);
  147. /**
  148. * Creating an image using $data.
  149. */
  150. abstract protected function createImageFromData($data);
  151. /**
  152. * Loading image from $resource.
  153. */
  154. protected function loadResource($resource)
  155. {
  156. $this->resource = $resource;
  157. }
  158. protected function loadFile($file, $type)
  159. {
  160. if (!$this->supports($type)) {
  161. throw new \RuntimeException('Type '.$type.' is not supported by GD');
  162. }
  163. if ($type == 'jpeg') {
  164. $this->openJpeg($file);
  165. }
  166. if ($type == 'gif') {
  167. $this->openGif($file);
  168. }
  169. if ($type == 'png') {
  170. $this->openPng($file);
  171. }
  172. if ($type == 'webp') {
  173. $this->openWebp($file);
  174. }
  175. if (false === $this->resource) {
  176. throw new \UnexpectedValueException('Unable to open file ('.$file.')');
  177. } else {
  178. $this->convertToTrueColor();
  179. }
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function init()
  185. {
  186. $source = $this->source;
  187. if ($source instanceof \Gregwar\Image\Source\File) {
  188. $this->loadFile($source->getFile(), $source->guessType());
  189. } elseif ($source instanceof \Gregwar\Image\Source\Create) {
  190. $this->createImage($source->getWidth(), $source->getHeight());
  191. } elseif ($source instanceof \Gregwar\Image\Source\Data) {
  192. $this->createImageFromData($source->getData());
  193. } elseif ($source instanceof \Gregwar\Image\Source\Resource) {
  194. $this->loadResource($source->getResource());
  195. } else {
  196. throw new \Exception('Unsupported image source type '.get_class($source));
  197. }
  198. return $this;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function deinit()
  204. {
  205. $this->resource = null;
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function resize($width = null, $height = null, $background = 'transparent', $force = false, $rescale = false, $crop = false)
  211. {
  212. $current_width = $this->width();
  213. $current_height = $this->height();
  214. $new_width = 0;
  215. $new_height = 0;
  216. $scale = 1.0;
  217. if ($height === null && preg_match('#^(.+)%$#mUsi', $width, $matches)) {
  218. $width = round($current_width * ((float) $matches[1] / 100.0));
  219. $height = round($current_height * ((float) $matches[1] / 100.0));
  220. }
  221. if (!$rescale && (!$force || $crop)) {
  222. if ($width != null && $current_width > $width) {
  223. $scale = $current_width / $width;
  224. }
  225. if ($height != null && $current_height > $height) {
  226. if ($current_height / $height > $scale) {
  227. $scale = $current_height / $height;
  228. }
  229. }
  230. } else {
  231. if ($width != null) {
  232. $scale = $current_width / $width;
  233. $new_width = $width;
  234. }
  235. if ($height != null) {
  236. if ($width != null && $rescale) {
  237. $scale = max($scale, $current_height / $height);
  238. } else {
  239. $scale = $current_height / $height;
  240. }
  241. $new_height = $height;
  242. }
  243. }
  244. if (!$force || $width == null || $rescale) {
  245. $new_width = round($current_width / $scale);
  246. }
  247. if (!$force || $height == null || $rescale) {
  248. $new_height = round($current_height / $scale);
  249. }
  250. if ($width == null || $crop) {
  251. $width = $new_width;
  252. }
  253. if ($height == null || $crop) {
  254. $height = $new_height;
  255. }
  256. $this->doResize($background, $width, $height, $new_width, $new_height);
  257. }
  258. /**
  259. * Trim background color arround the image.
  260. *
  261. * @param int $bg the background
  262. */
  263. protected function _trimColor($background = 'transparent')
  264. {
  265. $width = $this->width();
  266. $height = $this->height();
  267. $b_top = 0;
  268. $b_lft = 0;
  269. $b_btm = $height - 1;
  270. $b_rt = $width - 1;
  271. //top
  272. for (; $b_top < $height; ++$b_top) {
  273. for ($x = 0; $x < $width; ++$x) {
  274. if ($this->getColor($x, $b_top) != $background) {
  275. break 2;
  276. }
  277. }
  278. }
  279. // bottom
  280. for (; $b_btm >= 0; --$b_btm) {
  281. for ($x = 0; $x < $width; ++$x) {
  282. if ($this->getColor($x, $b_btm) != $background) {
  283. break 2;
  284. }
  285. }
  286. }
  287. // left
  288. for (; $b_lft < $width; ++$b_lft) {
  289. for ($y = $b_top; $y <= $b_btm; ++$y) {
  290. if ($this->getColor($b_lft, $y) != $background) {
  291. break 2;
  292. }
  293. }
  294. }
  295. // right
  296. for (; $b_rt >= 0; --$b_rt) {
  297. for ($y = $b_top; $y <= $b_btm; ++$y) {
  298. if ($this->getColor($b_rt, $y) != $background) {
  299. break 2;
  300. }
  301. }
  302. }
  303. ++$b_btm;
  304. ++$b_rt;
  305. $this->crop($b_lft, $b_top, $b_rt - $b_lft, $b_btm - $b_top);
  306. }
  307. /**
  308. * Resizes the image to an image having size of $target_width, $target_height, using
  309. * $new_width and $new_height and padding with $bg color.
  310. */
  311. abstract protected function doResize($bg, $target_width, $target_height, $new_width, $new_height);
  312. /**
  313. * Gets the color of the $x, $y pixel.
  314. */
  315. abstract protected function getColor($x, $y);
  316. /**
  317. * {@inheritdoc}
  318. */
  319. public function enableProgressive()
  320. {
  321. throw new \Exception('The Adapter '.$this->getName().' does not support Progressive Image loading');
  322. }
  323. /**
  324. * This does nothing, but can be used to tag a ressource for instance (having a final image hash
  325. * for the cache different depending on the tag)
  326. */
  327. public function tag($tag)
  328. {
  329. }
  330. }