sfGDAdapter.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2007 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfGDAdapter provides a mechanism for creating thumbnail images.
  11. * @see http://www.php.net/gd
  12. *
  13. * @package sfThumbnailPlugin
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @author Benjamin Meynell <bmeynell@colorado.edu>
  16. */
  17. class sfGDAdapter
  18. {
  19. protected
  20. $sourceWidth,
  21. $sourceHeight,
  22. $sourceMime,
  23. $maxWidth,
  24. $maxHeight,
  25. $scale,
  26. $inflate,
  27. $quality,
  28. $source,
  29. $thumb;
  30. /**
  31. * List of accepted image types based on MIME
  32. * descriptions that this adapter supports
  33. */
  34. protected $imgTypes = array(
  35. 'image/jpeg',
  36. 'image/pjpeg',
  37. 'image/png',
  38. 'image/gif',
  39. );
  40. /**
  41. * Stores function names for each image type
  42. */
  43. protected $imgLoaders = array(
  44. 'image/jpeg' => 'imagecreatefromjpeg',
  45. 'image/pjpeg' => 'imagecreatefromjpeg',
  46. 'image/png' => 'imagecreatefrompng',
  47. 'image/gif' => 'imagecreatefromgif',
  48. );
  49. /**
  50. * Stores function names for each image type
  51. */
  52. protected $imgCreators = array(
  53. 'image/jpeg' => 'imagejpeg',
  54. 'image/pjpeg' => 'imagejpeg',
  55. 'image/png' => 'imagepng',
  56. 'image/gif' => 'imagegif',
  57. );
  58. public function __construct($maxWidth, $maxHeight, $scale, $inflate, $quality, $options)
  59. {
  60. if (!extension_loaded('gd'))
  61. {
  62. throw new Exception ('GD not enabled. Check your php.ini file.');
  63. }
  64. $this->maxWidth = $maxWidth;
  65. $this->maxHeight = $maxHeight;
  66. $this->scale = $scale;
  67. $this->inflate = $inflate;
  68. $this->quality = $quality;
  69. $this->options = $options;
  70. }
  71. public function loadFile($thumbnail, $image)
  72. {
  73. $imgData = @GetImageSize($image);
  74. if (!$imgData)
  75. {
  76. throw new Exception(sprintf('Could not load image %s', $image));
  77. }
  78. if (in_array($imgData['mime'], $this->imgTypes))
  79. {
  80. $loader = $this->imgLoaders[$imgData['mime']];
  81. if(!function_exists($loader))
  82. {
  83. throw new Exception(sprintf('Function %s not available. Please enable the GD extension.', $loader));
  84. }
  85. $this->source = $loader($image);
  86. $this->sourceWidth = $imgData[0];
  87. $this->sourceHeight = $imgData[1];
  88. $this->sourceMime = $imgData['mime'];
  89. $thumbnail->initThumb($this->sourceWidth, $this->sourceHeight, $this->maxWidth, $this->maxHeight, $this->scale, $this->inflate);
  90. $this->thumb = imagecreatetruecolor($thumbnail->getThumbWidth(), $thumbnail->getThumbHeight());
  91. if ($imgData[0] == $this->maxWidth && $imgData[1] == $this->maxHeight)
  92. {
  93. $this->thumb = $this->source;
  94. }
  95. else
  96. {
  97. imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $thumbnail->getThumbWidth(), $thumbnail->getThumbHeight(), $imgData[0], $imgData[1]);
  98. }
  99. return true;
  100. }
  101. else
  102. {
  103. throw new Exception(sprintf('Image MIME type %s not supported', $imgData['mime']));
  104. }
  105. }
  106. public function loadData($thumbnail, $image, $mime)
  107. {
  108. if (in_array($mime,$this->imgTypes))
  109. {
  110. $this->source = imagecreatefromstring($image);
  111. $this->sourceWidth = imagesx($this->source);
  112. $this->sourceHeight = imagesy($this->source);
  113. $this->sourceMime = $mime;
  114. $thumbnail->initThumb($this->sourceWidth, $this->sourceHeight, $this->maxWidth, $this->maxHeight, $this->scale, $this->inflate);
  115. $this->thumb = imagecreatetruecolor($thumbnail->getThumbWidth(), $thumbnail->getThumbHeight());
  116. if ($this->sourceWidth == $this->maxWidth && $this->sourceHeight == $this->maxHeight)
  117. {
  118. $this->thumb = $this->source;
  119. }
  120. else
  121. {
  122. imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $thumbnail->getThumbWidth(), $thumbnail->getThumbHeight(), $this->sourceWidth, $this->sourceHeight);
  123. }
  124. return true;
  125. }
  126. else
  127. {
  128. throw new Exception(sprintf('Image MIME type %s not supported', $mime));
  129. }
  130. }
  131. public function save($thumbnail, $thumbDest, $targetMime = null)
  132. {
  133. if($targetMime !== null)
  134. {
  135. $creator = $this->imgCreators[$targetMime];
  136. }
  137. else
  138. {
  139. $creator = $this->imgCreators[$thumbnail->getMime()];
  140. }
  141. if ($creator == 'imagejpeg')
  142. {
  143. imagejpeg($this->thumb, $thumbDest, $this->quality);
  144. }
  145. else
  146. {
  147. $creator($this->thumb, $thumbDest);
  148. }
  149. }
  150. public function toString($thumbnail, $targetMime = null)
  151. {
  152. if ($targetMime !== null)
  153. {
  154. $creator = $this->imgCreators[$targetMime];
  155. }
  156. else
  157. {
  158. $creator = $this->imgCreators[$thumbnail->getMime()];
  159. }
  160. ob_start();
  161. $creator($this->thumb);
  162. return ob_get_clean();
  163. }
  164. public function toResource()
  165. {
  166. return $this->thumb;
  167. }
  168. public function freeSource()
  169. {
  170. if (is_resource($this->source))
  171. {
  172. imagedestroy($this->source);
  173. }
  174. }
  175. public function freeThumb()
  176. {
  177. if (is_resource($this->thumb))
  178. {
  179. imagedestroy($this->thumb);
  180. }
  181. }
  182. public function getSourceMime()
  183. {
  184. return $this->sourceMime;
  185. }
  186. }