From 93b880a523373eec80e284fa02d3b43c36ab4905 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Sat, 13 Jun 2015 01:08:35 +0200 Subject: [PATCH] Fixed transparency on cropped PNG. Fixes #19 --- core/libs/gd/slirgdimage.class.php | 42 +++++++++++++++++++++++++----- core/slir.class.php | 10 +++++-- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/core/libs/gd/slirgdimage.class.php b/core/libs/gd/slirgdimage.class.php index 0bd7ab7..6098aaf 100644 --- a/core/libs/gd/slirgdimage.class.php +++ b/core/libs/gd/slirgdimage.class.php @@ -51,6 +51,8 @@ class SLIRGDImage extends SLIRImage implements SLIRImageLibrary */ private $data; + private $transparencyEnabled = false; + /** * @param string $path * @return void @@ -330,6 +332,9 @@ public function enableTransparency() { imagealphablending($this->getImage(), false); imagesavealpha($this->getImage(), true); + + $this->transparencyEnabled = true; + return $this; } @@ -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); @@ -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( diff --git a/core/slir.class.php b/core/slir.class.php index 6d0915f..d5406b8 100755 --- a/core/slir.class.php +++ b/core/slir.class.php @@ -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 @@ -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;