Decoder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Intervention\Image\Gd;
  3. use \Intervention\Image\Image;
  4. use \Intervention\Image\Size;
  5. class Decoder extends \Intervention\Image\AbstractDecoder
  6. {
  7. /**
  8. * Initiates new image from path in filesystem
  9. *
  10. * @param string $path
  11. * @return \Intervention\Image\Image
  12. */
  13. public function initFromPath($path)
  14. {
  15. $info = @getimagesize($path);
  16. if ($info === false) {
  17. throw new \Intervention\Image\Exception\NotReadableException(
  18. "Unable to read image from file ({$path})."
  19. );
  20. }
  21. // define core
  22. switch ($info[2]) {
  23. case IMAGETYPE_PNG:
  24. $core = imagecreatefrompng($path);
  25. $this->gdResourceToTruecolor($core);
  26. break;
  27. case IMAGETYPE_JPEG:
  28. $core = imagecreatefromjpeg($path);
  29. $this->gdResourceToTruecolor($core);
  30. break;
  31. case IMAGETYPE_GIF:
  32. $core = imagecreatefromgif($path);
  33. $this->gdResourceToTruecolor($core);
  34. break;
  35. default:
  36. throw new \Intervention\Image\Exception\NotReadableException(
  37. "Unable to read image type. GD driver is only able to decode JPG, PNG or GIF files."
  38. );
  39. }
  40. // build image
  41. $image = $this->initFromGdResource($core);
  42. $image->mime = $info['mime'];
  43. $image->setFileInfoFromPath($path);
  44. return $image;
  45. }
  46. /**
  47. * Initiates new image from GD resource
  48. *
  49. * @param Resource $resource
  50. * @return \Intervention\Image\Image
  51. */
  52. public function initFromGdResource($resource)
  53. {
  54. return new Image(new Driver, $resource);
  55. }
  56. /**
  57. * Initiates new image from Imagick object
  58. *
  59. * @param Imagick $object
  60. * @return \Intervention\Image\Image
  61. */
  62. public function initFromImagick(\Imagick $object)
  63. {
  64. throw new \Intervention\Image\Exception\NotSupportedException(
  65. "Gd driver is unable to init from Imagick object."
  66. );
  67. }
  68. /**
  69. * Initiates new image from binary data
  70. *
  71. * @param string $data
  72. * @return \Intervention\Image\Image
  73. */
  74. public function initFromBinary($binary)
  75. {
  76. $resource = @imagecreatefromstring($binary);
  77. if ($resource === false) {
  78. throw new \Intervention\Image\Exception\NotReadableException(
  79. "Unable to init from given binary data."
  80. );
  81. }
  82. $image = $this->initFromGdResource($resource);
  83. $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary);
  84. return $image;
  85. }
  86. /**
  87. * Transform GD resource into Truecolor version
  88. *
  89. * @param resource $resource
  90. * @return bool
  91. */
  92. public function gdResourceToTruecolor(&$resource)
  93. {
  94. $width = imagesx($resource);
  95. $height = imagesy($resource);
  96. // new canvas
  97. $canvas = imagecreatetruecolor($width, $height);
  98. // fill with transparent color
  99. imagealphablending($canvas, false);
  100. $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
  101. imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent);
  102. imagecolortransparent($canvas, $transparent);
  103. imagealphablending($canvas, true);
  104. // copy original
  105. imagecopy($canvas, $resource, 0, 0, 0, 0, $width, $height);
  106. imagedestroy($resource);
  107. $resource = $canvas;
  108. return true;
  109. }
  110. }