contao/image

Gmagick does not like tempnam without file extension

leofeyer opened this issue · 10 comments

The resizer uses a temporary file for atomic write operations:

image/src/Resizer.php

Lines 123 to 127 in 9cd4150

// Atomic write operation
$tmpPath = $this->filesystem->tempnam($dir, 'img');
$this->filesystem->chmod($tmpPath, 0666, umask());
$imagineImage->save($tmpPath, $imagineOptions);
$this->filesystem->rename($tmpPath, $path, true);

This temporary file, however, does not have a file extension, which leads to an No encode delegate for this image format (webp) error in Gmagick on Ubuntu 16.04. The error did not occur in my dev environment on macOS.

Appending the file extension to the temporary file fixed the issue:

// Atomic write operation
$tmpPath = $this->filesystem->tempnam($dir, 'img');
$this->filesystem->chmod($tmpPath, 0666, umask());

// Append the file extension to the temporary file
rename($tmpPath, $tmpPath.'.'.$imagineOptions['format']);
$tmpPath .= '.'.$imagineOptions['format'];

$imagineImage->save($tmpPath, $imagineOptions);
$this->filesystem->rename($tmpPath, $path, true);

Not sure if there is a better way to accomplish this though.

ausi commented

This should’ve already been fixed in 55a770a I think. We now always set the $imagineOptions['format'] to make sure that the file extension has no effect.

The No encode delegate message from GMagick seems strange to me in this case. 😕

55a770a does not fix the issue for me. If the temporary file does not have the .webp extension, the error occurs. 🤷‍♂

ausi commented

I think this is a bug in Imagine or GMagick, see php-imagine/Imagine#732

ausi commented

Until php-imagine/Imagine#733 gets merged, we could fix it with the following code I think:

try {
	$imagineImage->save($tmpPath, $imagineOptions);
} catch(\GmagickException $e) {
	if (stripos($e->getMessage(), 'no encode delegate') === false) {
		throw $e;
	}
	
	$gmagick = new \Gmagick();
	$gmagick->newimage(1, 1, (new \GmagickPixel('#FFFFFF'))->getcolor(false));
	$gmagick->setimageformat(strtoupper($imagineOptions['format']));
	$gmagick->getImageBlob();
	
	$imagineImage->save($tmpPath, $imagineOptions);
}
ausi commented

@leofeyer should I implement the bugfix from #67 (comment) ?

Yes please. 👍

ausi commented

See #70

I think we should keep this issue open though because we probably want to revert the fix once this issue is resolved in Imagine or Gmagick.

Definitely. 👍

ausi commented

Note to myself: Revert f246a59 once php-imagine/Imagine#733 got resolved.

ausi commented

Closed in favor of #76