PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Centurion/Image/Adapter/GD.php

http://github.com/centurion-project/Centurion
PHP | 255 lines | 167 code | 43 blank | 45 comment | 25 complexity | 676ca6a217f17046b3fe566007fbfc1a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Centurion
  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. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@centurion-project.org so we can send you a copy immediately.
  12. *
  13. * @category Centurion
  14. * @package Centurion_Image
  15. * @subpackage Adapter
  16. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  17. * @license http://centurion-project.org/license/new-bsd New BSD License
  18. * @version $Id$
  19. */
  20. /**
  21. * @category Centurion
  22. * @package Centurion_Image
  23. * @subpackage Adapter
  24. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  25. * @license http://centurion-project.org/license/new-bsd New BSD License
  26. * @author Florent Messa <florent.messa@gmail.com>
  27. * @author Laurent Chenay <lc@centurion-project.org>
  28. */
  29. class Centurion_Image_Adapter_GD extends Centurion_Image_Adapter_Abstract
  30. {
  31. /**
  32. * Stores function names for each image type
  33. */
  34. protected $_imgLoaders = array(
  35. 'image/jpeg' => 'imagecreatefromjpeg',
  36. 'image/jpg' => 'imagecreatefromjpeg',
  37. 'image/pjpeg' => 'imagecreatefromjpeg',
  38. 'image/png' => 'imagecreatefrompng',
  39. 'image/x-png' => 'imagecreatefrompng',
  40. 'image/gif' => 'imagecreatefromgif',
  41. );
  42. /**
  43. * Stores function names for each image type
  44. */
  45. protected $_imgCreators = array(
  46. 'image/jpeg' => 'imagejpeg',
  47. 'image/jpg' => 'imagejpeg',
  48. 'image/pjpeg' => 'imagejpeg',
  49. 'image/png' => 'imagepng',
  50. 'image/x-png' => 'imagepng',
  51. 'image/gif' => 'imagegif',
  52. );
  53. /**
  54. * List of accepted image types based on MIME
  55. * descriptions that this adapter supports
  56. */
  57. protected $_imgTypes = array(
  58. 'image/jpeg',
  59. 'image/jpg',
  60. 'image/pjpeg',
  61. 'image/png',
  62. 'image/gif',
  63. 'image/x-png'
  64. );
  65. public function __construct($options = array(), $scale = true, $inflate = true, $quality = self::DEFAULT_QUALITY)
  66. {
  67. if (!extension_loaded('gd')) {
  68. throw new Centurion_Exception ('GD not enabled. Check your php.ini file.');
  69. }
  70. parent::__construct($options, $scale, $inflate, $quality);
  71. }
  72. /**
  73. * @param $image string
  74. * @return Centurion_Image_Adapter_GD
  75. */
  76. public function open($image)
  77. {
  78. $information = @getimagesize($image);
  79. if (!$information) {
  80. throw new Centurion_Exception(sprintf('Could not load image %s', $image));
  81. }
  82. if (!in_array($information['mime'], $this->_imgTypes)) {
  83. throw new Centurion_Exception(sprintf('Image MIME type %s not supported', $information['mime']));
  84. }
  85. $loader = $this->_imgLoaders[$information['mime']];
  86. if (!function_exists($loader)) {
  87. throw new Centurion_Exception(sprintf('Function %s not available. Please enable the GD extension.', $loader));
  88. }
  89. $this->_source = $loader($image);
  90. if (null === $this->_source) {
  91. throw new Centurion_Exception(sprintf('Could not load the file %s', $image));
  92. }
  93. if (function_exists('imageantialias'))
  94. imageantialias($this->_source, true);
  95. $this->_sourceWidth = $information[0];
  96. $this->_sourceHeight = $information[1];
  97. $this->_sourceMime = $information['mime'];
  98. $this->_sourceSrc = $image;
  99. return $this;
  100. }
  101. public function load($image, $mime)
  102. {
  103. if (!in_array($mime, $this->_imgTypes)) {
  104. throw new Centurion_Exception(sprintf('Image MIME type %s not supported', $mime));
  105. }
  106. $this->_source = imagecreatefromstring($image);
  107. if (function_exists('imageantialias'))
  108. imageantialias($this->_source, true);
  109. $this->_reloadSize();
  110. $this->_sourceMime = $mime;
  111. $this->_generateThumb();
  112. return $this;
  113. }
  114. public function save($dest, $targetMime = null)
  115. {
  116. $dirname = dirname($dest);
  117. if (!file_exists($dirname)) {
  118. throw new Centurion_Exception(sprintf('Directory %s does not exist', $dirname));
  119. }
  120. if (!is_writable($dirname)) {
  121. throw new Centurion_Exception(sprintf('File %s is not writable', $dirname));
  122. }
  123. if (!$this->_hasBeenModified && $this->_sourceSrc !== null) {
  124. return copy($this->_sourceSrc, $dest);
  125. }
  126. if (null !== $targetMime) {
  127. $creator = $this->_imgCreators[$targetMime];
  128. } else {
  129. $creator = $this->_imgCreators[$this->getMime()];
  130. }
  131. if (!function_exists($creator)) {
  132. throw new Centurion_Exception(sprintf('TargetMime %s is not valid', $targetMime));
  133. }
  134. if ('imagejpeg' === $creator) {
  135. return imagejpeg($this->_thumb, $dest, $this->_quality);
  136. }
  137. if ('imagepng' === $creator) {
  138. $quality = (int) round(($this->_quality / 100) * 9);
  139. return imagepng($this->_thumb, $dest, $quality);
  140. }
  141. return $creator($this->_thumb, $dest);
  142. }
  143. public function effect()
  144. {
  145. $args = func_get_args();
  146. $args[0] = constant($args[0]);
  147. array_unshift($args, $this->_source);
  148. call_user_func_array('imagefilter', $args);
  149. $this->_thumb = $this->_source;
  150. return $this;
  151. }
  152. public function __toString()
  153. {
  154. $creator = $this->_imgCreators[$this->getMime()];
  155. ob_start();
  156. $creator($this->_thumb);
  157. return ob_get_clean();
  158. }
  159. protected function _newThumb($width, $height)
  160. {
  161. if (function_exists('imagecreatetruecolor')) {
  162. $this->_thumb = imagecreatetruecolor($width, $height);
  163. } else {
  164. $this->_thumb = imagecreate($width, $height);
  165. }
  166. return $this->_preserveAlpha();
  167. }
  168. protected function _copyToThumb($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
  169. {
  170. imagecopyresampled($this->_thumb,
  171. $this->_source,
  172. $dst_x, $dst_y,
  173. $src_x,
  174. $src_y,
  175. $dst_w,
  176. $dst_h,
  177. $src_w,
  178. $src_h);
  179. return $this;
  180. }
  181. /**
  182. * @return Centurion_Image_Adapter_GD
  183. */
  184. protected function _reloadSize()
  185. {
  186. $this->_sourceWidth = imagesx($this->_source);
  187. $this->_sourceHeight = imagesy($this->_source);
  188. return $this;
  189. }
  190. protected function _preserveAlpha()
  191. {
  192. if ($this->_sourceMime == 'image/png' && $this->_options['preserveAlpha'] === true) {
  193. $result = imagealphablending($this->_thumb, false);
  194. $colorTransparent = imagecolorallocatealpha($this->_thumb,
  195. $this->_options['alphaMaskColor'][0],
  196. $this->_options['alphaMaskColor'][1],
  197. $this->_options['alphaMaskColor'][2], 0);
  198. imagefill($this->_thumb, 0, 0, $colorTransparent);
  199. imagesavealpha($this->_thumb, true);
  200. }
  201. // preserve transparency in GIFs... this is usually pretty rough tho
  202. if ($this->_sourceMime == 'image/gif' && $this->_options['preserveTransparency'] === true) {
  203. $colorTransparent = imagecolorallocate($this->_thumb,
  204. $this->_options['transparencyMaskColor'][0],
  205. $this->_options['transparencyMaskColor'][1],
  206. $this->_options['transparencyMaskColor'][2]);
  207. imagecolortransparent($this->_thumb, $colorTransparent);
  208. imagetruecolortopalette($this->_thumb, true, 256);
  209. }
  210. return $this;
  211. }
  212. }