vendor/intervention/image/src/Intervention/Image/Gd/Driver.php line 19

Open in your IDE?
  1. <?php
  2. namespace Intervention\Image\Gd;
  3. use Intervention\Image\Exception\NotSupportedException;
  4. use Intervention\Image\Image;
  5. class Driver extends \Intervention\Image\AbstractDriver
  6. {
  7. /**
  8. * Creates new instance of driver
  9. *
  10. * @param Decoder $decoder
  11. * @param Encoder $encoder
  12. */
  13. public function __construct(Decoder $decoder = null, Encoder $encoder = null)
  14. {
  15. if ( ! $this->coreAvailable()) {
  16. throw new NotSupportedException(
  17. "GD Library extension not available with this PHP installation."
  18. );
  19. }
  20. $this->decoder = $decoder ? $decoder : new Decoder;
  21. $this->encoder = $encoder ? $encoder : new Encoder;
  22. }
  23. /**
  24. * Creates new image instance
  25. *
  26. * @param int $width
  27. * @param int $height
  28. * @param mixed $background
  29. * @return \Intervention\Image\Image
  30. */
  31. public function newImage($width, $height, $background = null)
  32. {
  33. // create empty resource
  34. $core = imagecreatetruecolor($width, $height);
  35. $image = new Image(new static, $core);
  36. // set background color
  37. $background = new Color($background);
  38. imagefill($image->getCore(), 0, 0, $background->getInt());
  39. return $image;
  40. }
  41. /**
  42. * Reads given string into color object
  43. *
  44. * @param string $value
  45. * @return AbstractColor
  46. */
  47. public function parseColor($value)
  48. {
  49. return new Color($value);
  50. }
  51. /**
  52. * Checks if core module installation is available
  53. *
  54. * @return boolean
  55. */
  56. protected function coreAvailable()
  57. {
  58. return (extension_loaded('gd') && function_exists('gd_info'));
  59. }
  60. /**
  61. * Returns clone of given core
  62. *
  63. * @return mixed
  64. */
  65. public function cloneCore($core)
  66. {
  67. $width = imagesx($core);
  68. $height = imagesy($core);
  69. $clone = imagecreatetruecolor($width, $height);
  70. imagealphablending($clone, false);
  71. imagesavealpha($clone, true);
  72. $transparency = imagecolorallocatealpha($clone, 0, 0, 0, 127);
  73. imagefill($clone, 0, 0, $transparency);
  74. imagecopy($clone, $core, 0, 0, 0, 0, $width, $height);
  75. return $clone;
  76. }
  77. }