/pimcore/lib/Pimcore/Image/Adapter/GD.php

https://github.com/timglabisch/pimcore · PHP · 270 lines · 136 code · 63 blank · 71 comment · 17 complexity · 6399f23eba5355ecc553b016efa9bc4e MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.pimcore.org/license
  11. *
  12. * @copyright Copyright (c) 2009-2013 pimcore GmbH (http://www.pimcore.org)
  13. * @license http://www.pimcore.org/license New BSD License
  14. */
  15. class Pimcore_Image_Adapter_GD extends Pimcore_Image_Adapter {
  16. /**
  17. * @var string
  18. */
  19. protected $path;
  20. /**
  21. * contains imageresource
  22. * @var mixed
  23. */
  24. protected $resource;
  25. /**
  26. * @param $imagePath
  27. * @return bool|Pimcore_Image_Adapter_GD
  28. */
  29. public function load ($imagePath) {
  30. $this->path = $imagePath;
  31. if(!$this->resource = @imagecreatefromstring(file_get_contents($this->path))) {
  32. return false;
  33. }
  34. // set dimensions
  35. list($width, $height) = getimagesize($this->path);
  36. $this->setWidth($width);
  37. $this->setHeight($height);
  38. return $this;
  39. }
  40. /**
  41. * @param $path
  42. * @return void
  43. */
  44. public function save ($path, $format = null, $quality = null) {
  45. $format = strtolower($format);
  46. if(!$format) {
  47. $format = "png";
  48. }
  49. // progressive jpeg
  50. if($format == "pjpeg") {
  51. imageinterlace($this->resource, true);
  52. $format = "jpeg";
  53. }
  54. if($format == "jpg") {
  55. $format = "jpeg";
  56. }
  57. $functionName = 'image' . $format;
  58. if(!function_exists($functionName)) {
  59. $functionName = "imagepng";
  60. }
  61. // always create a PNG24
  62. if($format == "png") {
  63. imagesavealpha($this->resource, true);
  64. }
  65. switch ($format) {
  66. case 'jpeg':
  67. $functionName($this->resource, $path, $quality);
  68. break;
  69. default:
  70. $functionName($this->resource, $path);
  71. }
  72. return $this;
  73. }
  74. /**
  75. * @return void
  76. */
  77. protected function destroy() {
  78. imagedestroy($this->resource);
  79. }
  80. /**
  81. * @param $width
  82. * @param $height
  83. * @return resource
  84. */
  85. protected function createImage ($width, $height) {
  86. $newImg = imagecreatetruecolor($width, $height);
  87. imagesavealpha($newImg, true);
  88. imagealphablending($newImg, false);
  89. $trans_colour = imagecolorallocatealpha($newImg, 255, 0, 0, 127);
  90. imagefill($newImg, 0, 0, $trans_colour);
  91. return $newImg;
  92. }
  93. /**
  94. * @param $width
  95. * @param $height
  96. * @return Pimcore_Image_Adapter
  97. */
  98. public function resize ($width, $height) {
  99. $newImg = $this->createImage($width, $height);
  100. ImageCopyResampled($newImg, $this->resource, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  101. $this->resource = $newImg;
  102. $this->setWidth($width);
  103. $this->setHeight($height);
  104. $this->reinitializeImage();
  105. return $this;
  106. }
  107. /**
  108. * @param $x
  109. * @param $y
  110. * @param $width
  111. * @param $height
  112. * @return Pimcore_Image_Adapter_GD
  113. */
  114. public function crop($x, $y, $width, $height) {
  115. $x = min($this->getWidth(), max(0, $x));
  116. $y = min($this->getHeight(), max(0, $y));
  117. $width = min($width, $this->getWidth() - $x);
  118. $height = min($height, $this->getHeight() - $y);
  119. $new_img = $this->createImage($width, $height);
  120. imagecopy($new_img, $this->resource, 0, 0, $x, $y, $width, $height);
  121. $this->resource = $new_img;
  122. $this->setWidth($width);
  123. $this->setHeight($height);
  124. $this->reinitializeImage();
  125. return $this;
  126. }
  127. /**
  128. * @param $width
  129. * @param $height
  130. * @param string $color
  131. * @param string $orientation
  132. * @return Pimcore_Image_Adapter_GD
  133. */
  134. public function frame ($width, $height) {
  135. $this->contain($width, $height);
  136. $x = ($width - $this->getWidth()) / 2;
  137. $y = ($height - $this->getHeight()) / 2;
  138. $newImage = $this->createImage($width, $height);
  139. imagecopy($newImage, $this->resource,$x, $y, 0, 0, $this->getWidth(), $this->getHeight());
  140. $this->resource = $newImage;
  141. $this->setWidth($width);
  142. $this->setHeight($height);
  143. $this->reinitializeImage();
  144. return $this;
  145. }
  146. /**
  147. * @param $color
  148. * @return Pimcore_Image_Adapter
  149. */
  150. public function setBackgroundColor ($color) {
  151. list($r,$g,$b) = $this->colorhex2colorarray($color);
  152. // just imagefill() on the existing image doesn't work, so we have to create a new image, fill it and then merge
  153. // the source image with the background-image together
  154. $newImg = imagecreatetruecolor($this->getWidth(), $this->getHeight());
  155. $color = imagecolorallocate($newImg, $r, $g, $b);
  156. imagefill($newImg, 0, 0, $color);
  157. imagecopy($newImg, $this->resource,0, 0, 0, 0, $this->getWidth(), $this->getHeight());
  158. $this->resource = $newImg;
  159. $this->reinitializeImage();
  160. return $this;
  161. }
  162. /**
  163. * @return Pimcore_Image_Adapter_GD
  164. */
  165. public function grayscale () {
  166. imagefilter($this->resource, IMG_FILTER_GRAYSCALE);
  167. $this->reinitializeImage();
  168. return $this;
  169. }
  170. /**
  171. * @return Pimcore_Image_Adapter_GD
  172. */
  173. public function sepia () {
  174. imagefilter($this->resource, IMG_FILTER_GRAYSCALE);
  175. imagefilter($this->resource, IMG_FILTER_COLORIZE, 100, 50, 0);
  176. $this->reinitializeImage();
  177. return $this;
  178. }
  179. public function addOverlay ($image, $x = 0, $y = 0, $alpha = 100, $composite = "COMPOSITE_DEFAULT", $origin = 'top-left') {
  180. $image = ltrim($image,"/");
  181. $image = PIMCORE_DOCUMENT_ROOT . "/" . $image;
  182. // 100 alpha is default
  183. if(empty($alpha)) {
  184. $alpha = 100;
  185. }
  186. $alpha = round($alpha / 100, 1);
  187. if(is_file($image)) {
  188. list($oWidth, $oHeight) = getimagesize($image);
  189. if($origin == 'top-right') {
  190. $x = $this->getWidth() - $oWidth - $x;
  191. } elseif($origin == 'bottom-left') {
  192. $y = $this->getHeight() - $oHeight - $y;
  193. } elseif($origin == 'bottom-right') {
  194. $x = $this->getWidth() - $oWidth - $x;
  195. $y = $this->getHeight() - $oHeight - $y;
  196. } elseif($origin == 'center') {
  197. $x = round($this->getWidth() / 2) - round($oWidth / 2) + $x;
  198. $y = round($this->getHeight() / 2) -round($oHeight / 2) + $y;
  199. }
  200. $overlay = imagecreatefromstring(file_get_contents($image));
  201. imagealphablending($this->resource, true);
  202. imagecopyresampled($this->resource, $overlay, $x, $y, 0, 0, $oWidth, $oHeight, $oWidth, $oHeight);
  203. }
  204. $this->reinitializeImage();
  205. return $this;
  206. }
  207. }