From 4e1af839c3c826f1112fbd8ba17e5f59380a3e25 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Fri, 19 Jul 2013 19:19:18 +0200 Subject: [PATCH 1/3] Fix #208 : ImageInterface::save $path argument is now optional --- lib/Imagine/Filter/Basic/Save.php | 2 +- lib/Imagine/Filter/Transformation.php | 2 +- lib/Imagine/Gd/Image.php | 20 +++++++++++-- lib/Imagine/Gd/Imagine.php | 19 ++++++------ lib/Imagine/Gmagick/Image.php | 10 ++++++- lib/Imagine/Image/ManipulatorInterface.php | 2 +- lib/Imagine/Imagick/Image.php | 9 +++++- lib/Imagine/Imagick/Imagine.php | 1 + .../Imagine/Test/Image/AbstractImageTest.php | 30 +++++++++++++++++++ 9 files changed, 78 insertions(+), 17 deletions(-) diff --git a/lib/Imagine/Filter/Basic/Save.php b/lib/Imagine/Filter/Basic/Save.php index dc87148d1..0f76625a1 100644 --- a/lib/Imagine/Filter/Basic/Save.php +++ b/lib/Imagine/Filter/Basic/Save.php @@ -35,7 +35,7 @@ class Save implements FilterInterface * @param string $path * @param array $options */ - public function __construct($path, array $options = array()) + public function __construct($path = null, array $options = array()) { $this->path = $path; $this->options = $options; diff --git a/lib/Imagine/Filter/Transformation.php b/lib/Imagine/Filter/Transformation.php index 5590bd1a3..dff0a746d 100644 --- a/lib/Imagine/Filter/Transformation.php +++ b/lib/Imagine/Filter/Transformation.php @@ -180,7 +180,7 @@ public function rotate($angle, ColorInterface $background = null) /** * {@inheritdoc} */ - public function save($path, array $options = array()) + public function save($path = null, array $options = array()) { return $this->add(new Save($path, $options)); } diff --git a/lib/Imagine/Gd/Image.php b/lib/Imagine/Gd/Image.php index 1256ea1d4..a5e2cc602 100644 --- a/lib/Imagine/Gd/Image.php +++ b/lib/Imagine/Gd/Image.php @@ -43,6 +43,13 @@ final class Image implements ImageInterface */ private $palette; + /** + * Path to original source file + * + * @var null|string + */ + private $path; + /** * Constructs a new Image instance using the result of * imagecreatetruecolor() @@ -50,10 +57,11 @@ final class Image implements ImageInterface * @param resource $resource * @param PaletteInterface $palette */ - public function __construct($resource, PaletteInterface $palette) + public function __construct($resource, PaletteInterface $palette, $path = null) { $this->palette = $palette; $this->resource = $resource; + $this->path = $path; } /** @@ -213,8 +221,16 @@ final public function rotate($angle, ColorInterface $background = null) /** * {@inheritdoc} */ - final public function save($path, array $options = array()) + final public function save($path = null, array $options = array()) { + $path = null === $path ? $this->path : $path; + + if (null === $path) { + throw new RuntimeException( + 'You can ommit save path only if image has been open from a file' + ); + } + $format = isset($options['format']) ? $options['format'] : pathinfo($path, \PATHINFO_EXTENSION); diff --git a/lib/Imagine/Gd/Imagine.php b/lib/Imagine/Gd/Imagine.php index 7e33e36a1..583ad1baf 100644 --- a/lib/Imagine/Gd/Imagine.php +++ b/lib/Imagine/Gd/Imagine.php @@ -103,22 +103,21 @@ public function create(BoxInterface $size, ColorInterface $color = null) */ public function open($path) { - $handle = @fopen($path, 'r'); + $data = @file_get_contents($path); - if (false === $handle) { + if (false === $data) { throw new InvalidArgumentException(sprintf( 'File %s doesn\'t exist', $path )); } - try { - $image = $this->read($handle); - } catch (\Exception $e) { - fclose($handle); - throw new RuntimeException(sprintf('Unable to open image %s', $path), $e->getCode(), $e); + $resource = @imagecreatefromstring($data); + + if (!is_resource($resource)) { + throw new InvalidArgumentException(sprintf('Unable to open image %s', $path)); } - return $image; + return $this->wrap($resource, new RGB(), $path); } /** @@ -165,7 +164,7 @@ public function font($file, $size, ColorInterface $color) return new Font($file, $size, $color); } - private function wrap($resource, PaletteInterface $palette) + private function wrap($resource, PaletteInterface $palette, $path = null) { if (!imageistruecolor($resource)) { list($width, $height) = array(imagesx($resource), imagesy($resource)); @@ -194,6 +193,6 @@ private function wrap($resource, PaletteInterface $palette) imageantialias($resource, true); } - return new Image($resource, $palette); + return new Image($resource, $palette, $path); } } diff --git a/lib/Imagine/Gmagick/Image.php b/lib/Imagine/Gmagick/Image.php index f1a880b61..d39c68292 100644 --- a/lib/Imagine/Gmagick/Image.php +++ b/lib/Imagine/Gmagick/Image.php @@ -300,8 +300,16 @@ private function applyImageOptions(\Gmagick $image, array $options) /** * {@inheritdoc} */ - public function save($path, array $options = array()) + public function save($path = null, array $options = array()) { + $path = null === $path ? $this->gmagick->getImageFilename() : $path; + + if ('' === trim($path)) { + throw new RuntimeException( + 'You can omit save path only if image has been open from a file' + ); + } + try { $this->prepareOutput($options); $allFrames = !isset($options['animated']) || false === $options['animated']; diff --git a/lib/Imagine/Image/ManipulatorInterface.php b/lib/Imagine/Image/ManipulatorInterface.php index b1dbf3fb0..a86a403dd 100644 --- a/lib/Imagine/Image/ManipulatorInterface.php +++ b/lib/Imagine/Image/ManipulatorInterface.php @@ -106,7 +106,7 @@ public function paste(ImageInterface $image, PointInterface $start); * * @return ManipulatorInterface */ - public function save($path, array $options = array()); + public function save($path = null, array $options = array()); /** * Outputs the image content diff --git a/lib/Imagine/Imagick/Image.php b/lib/Imagine/Imagick/Image.php index ac61cee71..4fc4fc3c7 100644 --- a/lib/Imagine/Imagick/Image.php +++ b/lib/Imagine/Imagick/Image.php @@ -289,8 +289,15 @@ public function rotate($angle, ColorInterface $background = null) /** * {@inheritdoc} */ - public function save($path, array $options = array()) + public function save($path = null, array $options = array()) { + $path = null === $path ? $this->imagick->getImageFilename() : $path; + if (null === $path) { + throw new RuntimeException( + 'You can omit save path only if image has been open from a file' + ); + } + try { $this->prepareOutput($options); $this->imagick->writeImages($path, true); diff --git a/lib/Imagine/Imagick/Imagine.php b/lib/Imagine/Imagick/Imagine.php index 1c59c7e32..bcb39007c 100644 --- a/lib/Imagine/Imagick/Imagine.php +++ b/lib/Imagine/Imagick/Imagine.php @@ -59,6 +59,7 @@ public function open($path) try { $image = $this->read($handle); + $image->getImagick()->setImageFilename($path); } catch (\Exception $e) { fclose($handle); throw new RuntimeException(sprintf('Unable to open image %s', $path), $e->getCode(), $e); diff --git a/tests/Imagine/Test/Image/AbstractImageTest.php b/tests/Imagine/Test/Image/AbstractImageTest.php index db74c2fb2..1e1958b3d 100644 --- a/tests/Imagine/Test/Image/AbstractImageTest.php +++ b/tests/Imagine/Test/Image/AbstractImageTest.php @@ -93,6 +93,36 @@ public function testUsePalette($from, $to, $color) unlink(__DIR__ . '/tmp.jpg'); } + public function testSaveWithoutPathFileFromImageLoadShouldBeOkay() + { + $source = __DIR__ . '/../../Fixtures/google.png'; + $tmpFile = __DIR__ . '/../../Fixtures/google.tmp.png'; + + if (file_exists($tmpFile)) { + unlink($tmpFile); + } + + copy($source, $tmpFile); + + $this->assertEquals(md5_file($source), md5_file($tmpFile)); + + $this + ->getImagine() + ->open($tmpFile) + ->resize(new Box(20, 20)) + ->save(); + + $this->assertNotEquals(md5_file($source), md5_file($tmpFile)); + unlink($tmpFile); + } + + public function testSaveWithoutPathFileFromImageCreationShouldFail() + { + $image = $this->getImagine()->create(new Box(20, 20)); + $this->setExpectedException('Imagine\Exception\RuntimeException'); + $image->save(); + } + public function provideFromAndToPalettes() { return array( From 770b475993ed6edad498b58d0fae3a0924e1f6fd Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Fri, 19 Jul 2013 19:39:50 +0200 Subject: [PATCH 2/3] Fix #232 : ImageInterface::save format option should be optional if original file type is known --- lib/Imagine/Gd/Image.php | 12 +++++++++--- tests/Imagine/Test/Image/AbstractImageTest.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/Imagine/Gd/Image.php b/lib/Imagine/Gd/Image.php index a5e2cc602..b8e94f7b7 100644 --- a/lib/Imagine/Gd/Image.php +++ b/lib/Imagine/Gd/Image.php @@ -231,9 +231,15 @@ final public function save($path = null, array $options = array()) ); } - $format = isset($options['format']) - ? $options['format'] - : pathinfo($path, \PATHINFO_EXTENSION); + if (isset($options['format'])) { + $format = $options['format']; + } else { + if ('' !== $extension = pathinfo($path, \PATHINFO_EXTENSION)) { + $format = $extension; + } else { + $format = pathinfo($this->path, \PATHINFO_EXTENSION); + } + } $this->saveOrOutput($format, $options, $path); diff --git a/tests/Imagine/Test/Image/AbstractImageTest.php b/tests/Imagine/Test/Image/AbstractImageTest.php index 1e1958b3d..a1d6f7976 100644 --- a/tests/Imagine/Test/Image/AbstractImageTest.php +++ b/tests/Imagine/Test/Image/AbstractImageTest.php @@ -93,6 +93,20 @@ public function testUsePalette($from, $to, $color) unlink(__DIR__ . '/tmp.jpg'); } + public function testSaveWithoutFormatShouldSaveInOriginalFormat() + { + $tmpFile = __DIR__ . '/tmpfile'; + + $this + ->getImagine() + ->open(__DIR__ . '/../../Fixtures/large.jpg') + ->save($tmpFile); + + $data = exif_read_data($tmpFile); + $this->assertEquals('image/jpeg', $data['MimeType']); + unlink($tmpFile); + } + public function testSaveWithoutPathFileFromImageLoadShouldBeOkay() { $source = __DIR__ . '/../../Fixtures/google.png'; From 9cba3ebeda6316b5557a29881654ed153b26517a Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Thu, 15 Aug 2013 15:36:27 +0200 Subject: [PATCH 3/3] Address stof comments --- lib/Imagine/Gd/Image.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/Imagine/Gd/Image.php b/lib/Imagine/Gd/Image.php index b8e94f7b7..c538a2845 100644 --- a/lib/Imagine/Gd/Image.php +++ b/lib/Imagine/Gd/Image.php @@ -227,18 +227,16 @@ final public function save($path = null, array $options = array()) if (null === $path) { throw new RuntimeException( - 'You can ommit save path only if image has been open from a file' + 'You can omit save path only if image has been open from a file' ); } if (isset($options['format'])) { $format = $options['format']; + } elseif ('' !== $extension = pathinfo($path, \PATHINFO_EXTENSION)) { + $format = $extension; } else { - if ('' !== $extension = pathinfo($path, \PATHINFO_EXTENSION)) { - $format = $extension; - } else { - $format = pathinfo($this->path, \PATHINFO_EXTENSION); - } + $format = pathinfo($this->path, \PATHINFO_EXTENSION); } $this->saveOrOutput($format, $options, $path);