Skip to content

Commit

Permalink
update to rosell-dk/webp-convert:^2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickael GOETZ committed Oct 30, 2019
1 parent 7fdc9ef commit 763e6b5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
},
"require": {
"php": ">=7.1",
"ext-gd": "*",
"ext-exif": "*",
"rosell-dk/webp-convert": "^1.1",
"ext-gd": "*",
"rosell-dk/webp-convert": "^2.3",
"symfony/config": "^3.3||^4.1",
"symfony/dependency-injection": "^3.3||^4.1",
"symfony/http-kernel": "^3.3||^4.1"
Expand Down
37 changes: 34 additions & 3 deletions src/Service/ImagingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mesavolt\ImagingBundle\Service;


use WebPConvert\Exceptions\InvalidFileExtensionException;
use WebPConvert\Convert\Exceptions\ConversionFailedException;
use WebPConvert\WebPConvert;

class ImagingService
Expand Down Expand Up @@ -60,11 +60,42 @@ public function shrink(string $source, string $destination, ?int $width = null,
*/
public function generateWebp(string $source, string $destination, int $quality = self::DEFAULT_WEBP_QUALITY): bool
{
$ext = pathinfo($destination, PATHINFO_EXTENSION);
$realDestination = $destination;
if ($ext !== 'webp') {
$tmpFile = tempnam(sys_get_temp_dir(), 'imagingservice-temp-file');
$tmpFileWithExtension = "$tmpFile.webp";

if(!rename($tmpFile, $tmpFileWithExtension)) {
return false;
}

$realDestination = $tmpFileWithExtension;
}


try {
return WebPConvert::convert($source, $destination, [
WebPConvert::convert($source, $realDestination, [
'quality' => $quality,
'converter' => 'cwebp',
'converter-options' => [
'cwebp' => [
'cwebp-try-cwebp' => false,
'try-common-system-paths' => false,
'try-discovering-cwebp' => false,
'cwebp-try-supplied-binary-for-os' => true,
],
],
]);
} catch (InvalidFileExtensionException $ex) {

if ($realDestination !== $destination) {
if (!rename($realDestination, $destination)) {
return false;
}
}

return true;
} catch (ConversionFailedException $ex) {
// Throws when trying to convert gifs
return false;
}
Expand Down

0 comments on commit 763e6b5

Please sign in to comment.