-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement color limit for PNG & GIF encoders
- Loading branch information
1 parent
dc24ed9
commit b8d6023
Showing
14 changed files
with
208 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Intervention\Image\Drivers\Gd\Traits; | ||
|
||
use GdImage; | ||
|
||
trait CanReduceColors | ||
{ | ||
/** | ||
* Reduce colors in a given GdImage to the given limit. Reduction is only | ||
* applied when the given limit is under the given threshold | ||
* | ||
* @param GdImage $gd | ||
* @param int $limit | ||
* @param int $threshold | ||
* @return GdImage | ||
*/ | ||
private function maybeReduceColors(GdImage $gd, int $limit, int $threshold = 256): GdImage | ||
{ | ||
// no color limit: no reduction | ||
if ($limit === 0) { | ||
return $gd; | ||
} | ||
|
||
// limit is over threshold: no reduction | ||
if ($limit > $threshold) { | ||
return $gd; | ||
} | ||
|
||
// image size | ||
$width = imagesx($gd); | ||
$height = imagesy($gd); | ||
|
||
// create empty gd | ||
$reduced = imagecreatetruecolor($width, $height); | ||
|
||
// create matte | ||
$matte = imagecolorallocatealpha($reduced, 255, 255, 255, 127); | ||
|
||
// fill with matte | ||
imagefill($reduced, 0, 0, $matte); | ||
|
||
imagealphablending($reduced, false); | ||
|
||
// set transparency and get transparency index | ||
imagecolortransparent($reduced, $matte); | ||
|
||
// copy original image | ||
imagecopy($reduced, $gd, 0, 0, 0, 0, $width, $height); | ||
|
||
// reduce limit by one to include possible transparency in palette | ||
$limit = imagecolortransparent($gd) === -1 ? $limit : $limit - 1; | ||
|
||
// decrease colors | ||
imagetruecolortopalette($reduced, true, $limit); | ||
|
||
return $reduced; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Intervention\Image\Drivers\Imagick\Traits; | ||
|
||
use Imagick; | ||
|
||
trait CanReduceColors | ||
{ | ||
/** | ||
* Returns a Imagick from a given image with reduced colors to a given limit. | ||
* Reduction is only applied when the given limit is under the given threshold | ||
* | ||
* @param Imagick $imagick | ||
* @param int $limit | ||
* @param int $threshold | ||
* @return Imagick | ||
*/ | ||
private function maybeReduceColors(Imagick $imagick, int $limit, int $threshold = 256): Imagick | ||
{ | ||
if ($limit === 0) { | ||
return $imagick; | ||
} | ||
|
||
if ($limit > $threshold) { | ||
return $imagick; | ||
} | ||
|
||
$imagick->quantizeImage( | ||
$limit, | ||
$imagick->getImageColorspace(), | ||
0, | ||
false, | ||
false | ||
); | ||
|
||
return $imagick; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.