Common.php 10 KB

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