Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 8 additions & 60 deletions src/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public static function getGravityTypes(): array
}

/**
* @return Image
*
* @throws \Throwable
*/
public function crop(int $width, int $height, string $gravity = Image::GRAVITY_CENTER): self
Expand Down Expand Up @@ -369,65 +367,15 @@ public function save(?string $path = null, string $type = '', int $quality = 75)

case 'avif':
case 'heic':
$signature = $this->image->getImageSignature();

$temp = tempnam(sys_get_temp_dir(), 'temp-'.$signature);
if ($temp === false) {
throw new Exception('Failed to create temporary file');
}

$output = tempnam(sys_get_temp_dir(), 'output-'.$signature);
if ($output === false) {
\unlink($temp);
throw new Exception('Failed to create output file');
}

$temp .= '.'.\strtolower($this->image->getImageFormat());
$output .= '.'.$type;

try {
// save temp
$this->image->writeImages($temp, true);

// convert temp
$command = ['magick convert', \escapeshellarg($temp)];

$quality = (int) $quality;
if ($quality >= 0) {
$command = [...$command, '-quality', $quality];
}

$command = [
...$command, \escapeshellarg($output), '2>&1', // 2>&1 redirect stderr to stdout
];

\exec(implode(' ', $command), $outputArray, $returnCode);

if ($returnCode !== 0) {
throw new Exception("Image conversion failed with status {$returnCode}: ".implode("\n", $outputArray));
}

$data = \file_get_contents($output);

// save to path
if (! empty($path)) {
\file_put_contents($path, $data, LOCK_EX);

return;
}

return $data;
} finally {
if (file_exists($temp)) {
\unlink($temp);
}
if (file_exists($output)) {
\unlink($output);
}

$this->image->clear();
$this->image->destroy();
$this->image->setImageFormat($type);
if ($quality >= 0) {
// setImageCompressionQuality() is silently ignored by the libheif coder —
// setCompressionQuality() (object-level, not image-level) must be called
// after setImageFormat() for quality to take effect on AVIF/HEIC output.
// See: https://github.com/Imagick/imagick/issues/711
$this->image->setCompressionQuality($quality);
}
break;

case 'webp':
Comment thread
greptile-apps[bot] marked this conversation as resolved.
$temp = null;
Expand Down
2 changes: 1 addition & 1 deletion tests/Image/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function test_crop100x100_heic(): void

$this->assertEquals(\is_readable($target), true);
$this->assertGreaterThan(500, \filesize($target));
$this->assertEquals(8426, \filesize($target));
$this->assertEquals(8490, \filesize($target));
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertFileExists($target);
$this->assertNotEmpty(\file_get_contents($target));
Expand Down
Loading