diff --git a/src/Intervention/Image/Gd/Source.php b/src/Intervention/Image/Gd/Source.php index 8a9d6a7f..dee22615 100644 --- a/src/Intervention/Image/Gd/Source.php +++ b/src/Intervention/Image/Gd/Source.php @@ -21,7 +21,8 @@ class Source extends \Intervention\Image\AbstractSource switch ($info[2]) { case IMAGETYPE_PNG: $core = imagecreatefrompng($path); - imagepalettetotruecolor($core); + // imagepalettetotruecolor($core); + $this->gdResourceToTruecolor($core); break; case IMAGETYPE_JPEG: @@ -30,7 +31,8 @@ class Source extends \Intervention\Image\AbstractSource case IMAGETYPE_GIF: $core = imagecreatefromgif($path); - imagepalettetotruecolor($core); + // imagepalettetotruecolor($core); + $this->gdResourceToTruecolor($core); break; default: @@ -75,4 +77,37 @@ class Source extends \Intervention\Image\AbstractSource return $image; } + + /** + * Transform GD resource into Truecolor version + * + * @param resource $resource + * @return bool + */ + public function gdResourceToTruecolor(&$resource) + { + if (imageistruecolor($resource)) { + return true; + } + + $width = imagesx($resource); + $height = imagesy($resource); + + // new canvas + $canvas = imagecreatetruecolor($width, $height); + + // fill with transparent color + imagealphablending($canvas, false); + $transparent = imagecolorallocatealpha($canvas, 0, 0, 0, 127); + imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent); + imagealphablending($canvas, true); + + // copy original + imagecopy($canvas, $resource, 0, 0, 0, 0, $width, $height); + imagedestroy($resource); + + $resource = $canvas; + + return true; + } } diff --git a/tests/GdSystemTest.php b/tests/GdSystemTest.php index 657cec5d..f36ea035 100644 --- a/tests/GdSystemTest.php +++ b/tests/GdSystemTest.php @@ -1122,7 +1122,7 @@ class GdSystemTest extends PHPUnit_Framework_TestCase public function testPickColorFromPalette() { $img = $this->manager()->make('tests/images/tile.png'); - imagetruecolortopalette($img->getCore(), true, 200); + $img->limitColors(200); $c = $img->pickColor(0, 0); $this->assertEquals(180, $c[0]);