Decoder.php 4.3 KB

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