Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Fixed transparency on cropped PNG. Fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 12, 2015
1 parent e5155ff commit 93b880a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
42 changes: 35 additions & 7 deletions core/libs/gd/slirgdimage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class SLIRGDImage extends SLIRImage implements SLIRImageLibrary
*/
private $data;

private $transparencyEnabled = false;

/**
* @param string $path
* @return void
Expand Down Expand Up @@ -330,6 +332,9 @@ public function enableTransparency()
{
imagealphablending($this->getImage(), false);
imagesavealpha($this->getImage(), true);

$this->transparencyEnabled = true;

return $this;
}

Expand All @@ -340,14 +345,33 @@ public function enableTransparency()
*/
public function fill()
{
$color = $this->getBackground();
$color = $this->getBackground();

if ($color === null) {
$color = "ffffff";
}

$background = null;

$background = imagecolorallocate(
if ($this->transparencyEnabled === true) {
$background = imagecolorallocatealpha(
$this->getImage(),
hexdec($color[0].$color[1]),
hexdec($color[2].$color[3]),
hexdec($color[4].$color[5])
);
hexdec($color[4].$color[5]),
127
);
}
else {

$background = imagecolorallocate(
$this->getImage(),
hexdec($color[0].$color[1]),
hexdec($color[2].$color[3]),
hexdec($color[4].$color[5])
);

}

imagefilledrectangle($this->getImage(), 0, 0, $this->getWidth(), $this->getHeight(), $background);

Expand Down Expand Up @@ -417,9 +441,13 @@ private function cropImage($leftOffset, $topOffset)
$class = __CLASS__;
$cropped = new $class();

$cropped->setWidth($this->getCropWidth())
->setHeight($this->getCropHeight())
->setBackground($this->getBackground());
$cropped->setMimeType($this->getMimeType()) // To enable again transparency on PNGs !
->setWidth($this->getCropWidth())
->setHeight($this->getCropHeight())
->setBackground($this->getBackground());


$cropped->background();

// Copy rendered image to cropped image
imagecopy(
Expand Down
10 changes: 8 additions & 2 deletions core/slir.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,9 @@ private function copySourceToRendered()
$this->getRendered()->background();

// Resample the original image into the resized canvas we set up earlier
if ($this->getSource()->getWidth() !== $this->getRendered()->getWidth() || $this->getSource()->getHeight() != $this->getRendered()->getHeight()) {
if ($this->getSource()->getWidth() !== $this->getRendered()->getWidth() ||
$this->getSource()->getHeight() != $this->getRendered()->getHeight()) {

$this->getSource()->resample($this->getRendered());
} else {
// No resizing is needed, so make a clean copy
Expand Down Expand Up @@ -706,7 +708,11 @@ private function makeIPTCTag($rec, $data, $value)
*/
private function isSourceImageDesired()
{
if ($this->isWidthDifferent() || $this->isHeightDifferent() || $this->isBackgroundFillOn() || $this->isQualityOn() || $this->isCroppingNeeded()) {
if ($this->isWidthDifferent() ||
$this->isHeightDifferent() ||
$this->isBackgroundFillOn() ||
$this->isQualityOn() ||
$this->isCroppingNeeded()) {
return false;
} else {
return true;
Expand Down

0 comments on commit 93b880a

Please sign in to comment.