/lib/Imagine/Gd/Imagine.php

https://github.com/ndusan/bginfobox · PHP · 225 lines · 150 code · 40 blank · 35 comment · 21 complexity · 6195b9cf7b1c0f990f6c6c2b9810c8c0 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Imagine package.
  4. *
  5. * (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Imagine\Gd;
  11. use Imagine\Image\Color;
  12. use Imagine\Image\BoxInterface;
  13. use Imagine\Image\ImagineInterface;
  14. use Imagine\Exception\InvalidArgumentException;
  15. use Imagine\Exception\RuntimeException;
  16. final class Imagine implements ImagineInterface
  17. {
  18. /**
  19. * @var array
  20. */
  21. private $types = array(
  22. IMAGETYPE_GIF => 'gif',
  23. IMAGETYPE_JPEG => 'jpeg',
  24. IMAGETYPE_JPEG2000 => 'jpeg',
  25. IMAGETYPE_PNG => 'png',
  26. IMAGETYPE_UNKNOWN => 'unknown',
  27. IMAGETYPE_WBMP => 'wbmp',
  28. IMAGETYPE_XBM => 'xbm'
  29. );
  30. /**
  31. * @var array
  32. */
  33. private $info;
  34. /**
  35. * @throws Imagine\Exception\RuntimeException
  36. */
  37. public function __construct()
  38. {
  39. $this->loadGdInfo();
  40. $this->requireGdVersion('2.0.1');
  41. }
  42. private function loadGdInfo()
  43. {
  44. if (!function_exists('gd_info')) {
  45. throw new RuntimeException('Gd not installed');
  46. }
  47. $this->info = gd_info();
  48. }
  49. private function requireGdVersion($version)
  50. {
  51. if (version_compare(GD_VERSION, $version, '<')) {
  52. throw new RuntimeException('GD2 version 2.0.1 or higher is required');
  53. }
  54. }
  55. /**
  56. * (non-PHPdoc)
  57. * @see Imagine\Image\ImagineInterface::create()
  58. */
  59. public function create(BoxInterface $size, Color $color = null)
  60. {
  61. $width = $size->getWidth();
  62. $height = $size->getHeight();
  63. $resource = imagecreatetruecolor($width, $height);
  64. if (false === $resource) {
  65. throw new RuntimeException('Create operation failed');
  66. }
  67. $color = $color ? $color : new Color('fff');
  68. if (false === imagealphablending($resource, false) ||
  69. false === imagesavealpha($resource, true)) {
  70. throw new RuntimeException(
  71. 'Could not set alphablending, savealpha and antialias values'
  72. );
  73. }
  74. if (function_exists('imageantialias')) {
  75. imageantialias($resource, true);
  76. }
  77. $color = imagecolorallocatealpha(
  78. $resource, $color->getRed(), $color->getGreen(), $color->getBlue(),
  79. round(127 * $color->getAlpha() / 100)
  80. );
  81. if (false === $color) {
  82. throw new RuntimeException('Unable to allocate color');
  83. }
  84. if (false === imagefilledrectangle(
  85. $resource, 0, 0, $width, $height, $color
  86. )) {
  87. throw new RuntimeException('Could not set background color fill');
  88. }
  89. return new Image($resource, $this);
  90. }
  91. /**
  92. * (non-PHPdoc)
  93. * @see Imagine\Image\ImagineInterface::open()
  94. */
  95. public function open($path)
  96. {
  97. if (!is_file($path)) {
  98. throw new InvalidArgumentException(sprintf(
  99. 'File %s doesn\'t exist', $path
  100. ));
  101. }
  102. $info = getimagesize($path);
  103. if (false === $info) {
  104. throw new RuntimeException('Could not collect image metadata');
  105. }
  106. list($width, $height, $type) = $info;
  107. $format = $this->types[$type];
  108. $supported = array(
  109. 'gif' => 'GIF Read Support',
  110. 'jpeg' => 'JPEG Support',
  111. 'png' => 'PNG Support',
  112. 'wbmp' => 'WBMP Support',
  113. 'xbm' => 'XBM Support'
  114. );
  115. if (!$this->info[$supported[$format]]) {
  116. throw new RuntimeException(sprintf(
  117. 'Installed version of GD doesn\'t support "%s" image format',
  118. $format
  119. ));
  120. }
  121. if (!function_exists('imagecreatefrom'.$format)) {
  122. throw new InvalidArgumentException(
  123. 'Invalid image format specified, only "gif", "jpeg", "png", '.
  124. '"wbmp", "xbm" images are supported'
  125. );
  126. }
  127. $resource = call_user_func('imagecreatefrom'.$format, $path);
  128. //Active in php development version 5.3, surelly true in 5.4
  129. //Inactivate for compatibility
  130. if( "gif" === $format and false){
  131. $index = imagecolortransparent($resource);
  132. if($index != (-1)){
  133. $color = ImageColorsForIndex($resource, $index);
  134. imagecolorset( $resource, $index, $color['red'], $color['green'], $color['blue'], 127 );
  135. }
  136. }
  137. if (!is_resource($resource)) {
  138. throw new RuntimeException(sprintf(
  139. 'File "%s" could not be opened', $path
  140. ));
  141. }
  142. if (false === imagealphablending($resource, false) ||
  143. false === imagesavealpha($resource, true)) {
  144. throw new RuntimeException(
  145. 'Could not set alphablending, savealpha and antialias values'
  146. );
  147. }
  148. if (function_exists('imageantialias')) {
  149. imageantialias($resource, true);
  150. }
  151. return new Image($resource);
  152. }
  153. /**
  154. * (non-PHPdoc)
  155. * @see Imagine\Image\ImagineInterface::load()
  156. */
  157. public function load($string)
  158. {
  159. $resource = imagecreatefromstring($string);
  160. if (!is_resource($resource)) {
  161. throw new InvalidArgumentException('An image could not be created from the given input');
  162. }
  163. if (false === imagealphablending($resource, false) ||
  164. false === imagesavealpha($resource, true)) {
  165. throw new RuntimeException(
  166. 'Could not set alphablending, savealpha and antialias values'
  167. );
  168. }
  169. if (function_exists('imageantialias')) {
  170. imageantialias($resource, true);
  171. }
  172. return new Image($resource);
  173. }
  174. /**
  175. * (non-PHPdoc)
  176. * @see Imagine\Image\ImagineInterface::font()
  177. */
  178. public function font($file, $size, Color $color)
  179. {
  180. if (!$this->info['FreeType Support']) {
  181. throw new RuntimeException('GD is not compiled with FreeType support');
  182. }
  183. return new Font($file, $size, $color);
  184. }
  185. }