Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the $format parameter to the save method #951

Merged
merged 2 commits into from Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Intervention/Image/Image.php
Expand Up @@ -126,9 +126,10 @@ public function encode($format = null, $quality = 90)
*
* @param string $path
* @param int $quality
* @param string $format
* @return \Intervention\Image\Image
*/
public function save($path = null, $quality = null)
public function save($path = null, $quality = null, $format = null)
{
$path = is_null($path) ? $this->basePath() : $path;

Expand All @@ -138,7 +139,11 @@ public function save($path = null, $quality = null)
);
}

$data = $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality);
if ($format === null) {
$format = pathinfo($path, PATHINFO_EXTENSION);
}

$data = $this->encode($format, $quality);
$saved = @file_put_contents($path, $data);

if ($saved === false) {
Expand Down
20 changes: 20 additions & 0 deletions tests/ImageTest.php
@@ -1,6 +1,7 @@
<?php

use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use PHPUnit\Framework\TestCase;

class ImageTest extends TestCase
Expand Down Expand Up @@ -44,6 +45,25 @@ public function testSave()
@unlink($save_as);
}

public function testFormatSave()
{
$save_as = __DIR__.'/tmp/test';

$config = ['driver' => new Intervention\Image\Imagick\Driver()];
$manager = new ImageManager($config);

$image = $manager->make('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
$this->assertInstanceOf('Intervention\Image\Image', $image);
$this->assertInstanceOf('Imagick', $image->getCore());

$gifCore = $image->getCore();
$this->assertEquals($gifCore->getImageMimeType(), 'image/gif');
$image->save($save_as, null, 'jpg');

$this->assertEquals(\mime_content_type($save_as), 'image/jpeg');
@unlink($save_as);
}

public function testIsEncoded()
{
$image = $this->getTestImage();
Expand Down