Skip to content
Open
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
25 changes: 23 additions & 2 deletions Thumbnailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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){
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"require": {
"yiisoft/yii2": "*",
"yiisoft/yii2-imagine": "~2.2.0"
"yiisoft/yii2-imagine": "~2.2.0",
"ralouphie/mimey": "*"
},
"autoload": {
"psr-4": {
Expand Down