diff --git a/Thumbnailer.php b/Thumbnailer.php index fd94b33..362609b 100755 --- a/Thumbnailer.php +++ b/Thumbnailer.php @@ -20,6 +20,8 @@ use yii\helpers\Url; use yii\imagine\Image as Imagine; use yii\caching\CacheInterface; +use yii\helpers\Inflector; +use Mimey\MimeTypes; /** * Image thumbnailer for Yii2. @@ -247,8 +249,27 @@ protected function generateThumbnail( $ignore_certificate = false ) { - $filename = basename($url); - $thumbnailPath = Yii::getAlias("$this->thumbnailsPath/{$width}x{$height}/{$filename}"); + $urlparts = parse_url($url); + $hash = ($urlparts['query'] ?? null) ? ('-'.md5($urlparts['query'])) : ''; + if($urlparts['path'] ?? null) + { + $path = $urlparts['path']; + $filename = pathinfo($path)['filename'] ?? null; + $filename = $filename ? Inflector::slug($filename) : null; + $extension = pathinfo($path)['extension'] ?? null; + } + if(!$filename) + { + $filename = uniqid('thumbnail-'); + } + if(! $extension) + { + $mimes = new MimeTypes; + $contentType = @FileHelper::getMimeType($url) ?: ((array_change_key_case(get_headers($url,true))['content-type']) ?? null); + $extension = $contentType ? ($mimes->getExtension($contentType) ?? 'jpg') : 'jpg'; + } + Yii::warning(['filename' => $filename, 'extension' => $extension, 'hash' => $hash, 'urlparts' => $urlparts],"Generating Thumbnail based on $url"); + $thumbnailPath = Yii::getAlias("$this->thumbnailsPath/{$width}x{$height}/{$filename}{$hash}.{$extension}"); try { if ($ignore_certificate){ diff --git a/composer.json b/composer.json index 64f36b0..c63d66f 100755 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ ], "require": { "yiisoft/yii2": "*", - "yiisoft/yii2-imagine": "~2.2.0" + "yiisoft/yii2-imagine": "~2.2.0", + "ralouphie/mimey": "*" }, "autoload": { "psr-4": {