Is there a way to rewrite urls to the media folder that works with thumbnail generation on the fly?
I managed to get it working to rewrite the urls with ‘file::version’ in the Kirby plugin component array, but when I try to hit the rewritten url I get a 500 error, because Kirby doesn’t know the url to solve the jobs to create the thumbnails. I somehow think it is bounded to the path /media/pages/
I want to achieve something like this:
https://kirbyurl.de/media/pages/blog/artice/1234-56789/image.jpg
to
https://assets.contenturl.de/blog/artice/1234-56789/image.jpg
The url https://assets.contenturl.de
points to https://kirbyurl.de/media/pages/
Anyone has a hint for me to get this working right?
Thanks in advance!
Could you please post your code?
Sure @texnixe. Sorry!
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\Filename;
use Kirby\Cms\FileVersion;
use Kirby\Cms\Template;
use Kirby\Data\Data;
use Kirby\Http\Server;
use Kirby\Image\Darkroom;
use Kirby\Text\Markdown;
use Kirby\Text\SmartyPants;
use Kirby\Toolkit\A;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Tpl as Snippet;
Kirby::plugin('my/urls', [
'components' => [
'file::version' => function (App $kirby, File $file, array $options = []) {
$mediaRoot = dirname($file->mediaRoot());
$dst = $mediaRoot . '/{{ name }}{{ attributes }}.{{ extension }}';
$thumbRoot = (new Filename($file->root(), $dst, $options))->toString();
$thumbName = basename($thumbRoot);
$job = $mediaRoot . '/.jobs/' . $thumbName . '.json';
if (file_exists($thumbRoot) === false) {
try {
Data::write($job, array_merge($options, [
'filename' => $file->filename()
]));
} catch (Throwable $e) {
return $file;
}
}
return new FileVersion([
'modifications' => $options,
'original' => $file,
'root' => $file->root(),
'url' => 'https://assets.contenturl.de/' . $file->parent()->id() .'/'. $file->mediaHash() .'/'. $thumbName
]);
},
'file::url' => function ($kirby, $file): string {
return 'https://assets.contenturl.de/' . $file->parent()->id() .'/'. $file->mediaHash() .'/'. $file->filename();
}
]
]);
well @texnixe I could use https://github.com/bnomei/kirby3-janitor but I think it would be great to know how I can “teach” Kirby to listen to different URLs for solving the thumbnail creation job.
I did now a helper function. Never knew $file->resize(1500)->save()
existed.
function createNewImageUrl($image) {
if ($image != '') {
if (Str::contains($image->url(), 'http')) {
$file = $image;
} else {
$file = $image->toFile();
}
if ($file) {
if($file->isResizable()) {
if (!$file->resize(1500)->exists()) {
$file->resize(1500)->save();
}
$imageUrl = $file->resize(1500)->url();
} else {
$imageUrl = $file->url();
}
return Str::replace($imageUrl, 'kirbyurl.de/media/pages/', 'assets.contenturl.de/');
}
}
return '';
}