Working on a slightly modified version of the standard (image: )
tag so that it uses the width and height attributes to resize or crop the image using the giving width and height attributes, rather than just setting the width and heights on the element.
I think that means i just need to tweak the src line to something like it:
$tag->src = $tag->file->resize($tag->width, $tag->height)->url();
Unfournatly it’s not actually resizing the image, it is just creating a file in the media folder with the right name, but physically it’s the original sized image.
spring-120x75.jpg
What can i do?
What is the original size of the image? Hm, for me it works. Keep in mind that resize doesn’t crop the image.
Sure, i’m setting it so i can either focus crop it or resize it. Im just starting with resize and building it up.
The image i am using is 1920 x 1080. I just noticed that it isnt actually moving it to the content folder, but the url it is generating is pointing to a file in the media folder.
/media/pages/spotlight/artist/514118754-1564925842/spring-400x200.jpg
However it seems to be returning the file from the content folder from that URL.
Here is my whole tag…
'boximage' => [
'attr' => [
'alt',
'caption',
'class',
'height',
'imgclass',
'link',
'linkclass',
'rel',
'target',
'text',
'title',
'width'
],
'html' => function ($tag) {
if ($tag->file = $tag->file($tag->value)) {
$resized = $tag->file->resize($tag->width, $tag->height)->url();
$tag->src = $resized;
$tag->alt = $tag->alt ?? $tag->file->alt()->or(' ')->value();
$tag->title = $tag->title ?? $tag->file->title()->value();
$tag->caption = $tag->caption ?? $tag->file->caption()->value();
} else {
$tag->src = Url::to($tag->value);
}
$link = function ($img) use ($tag) {
if (empty($tag->link) === true) {
return $img;
}
if ($link = $tag->file($tag->link)) {
$link = $link->url();
} else {
$link = $tag->link === 'self' ? $tag->src : $tag->link;
}
return Html::a($link, [$img], [
'rel' => $tag->rel,
'class' => $tag->linkclass,
'target' => $tag->target
]);
};
$image = Html::img($tag->src, [
'class' => $tag->imgclass,
'title' => $tag->title,
'alt' => $tag->alt ?? ' '
]);
if ($tag->kirby()->option('kirbytext.image.figure', true) === false) {
return $link($image);
}
// render KirbyText in caption
if ($tag->caption) {
$tag->caption = [$tag->kirby()->kirbytext($tag->caption, [], true)];
}
return Html::figure([ $link($image) ], $tag->caption, [
'class' => $tag->class
]);
}
],
Hm, have you emptied the media folder?
Your code works for me, image is copied to the media folder and resized as expected. Are you using any other plugins that might interfere? File::url component or something like that?
Yes, that is when i noticed it wasnt being copied, but the URL is pointing the media folder. All ive got in there is a bunch of empty hashstamped folders.
I dont think so - only the focus plugin and a hook that downsizes images larger then 2000 pixels, but that wouldnt have got triggered in this instance. None of the other plugins have anything to do with images.
Does normal image resizing in a template work? Are the thumbs actually produced?
That doesnt work either…
<?= $page->image('spring.jpg')->resize('400', '200')->url() ?>
However, i just found the cause… i forgot that i was using ImageMagick as the image driver. If i switch back to GD, it works as expected.
Perhaps you can verify it’s not just me. GD is fine i guess but i would rather use ImageMagick since it gives higher quality results.
Is ImageMagic installed on your server?
Im working locally at the moment, but yes its installed and its also on my VPS.
Hm, has it ever worked? You might have to set the path to the convert binary in your config:
I dont think im using resize anywhere but i’m sure GD works since i have focuscrop working fine on most of the images on the site.
Doesn’t focus crop come with its own drivers? Which one do you use?
It looks like it extends the built in drivers to add new features.
Kirby\Image\Darkroom::$types['gd'] = 'Flokosiol\Focus\GdLib';
Kirby\Image\Darkroom::$types['im'] = 'Flokosiol\Focus\ImageMagick';
So which ever one kirby is set to, it loads the extended driver from the plugin.
Yes, right. So the question is, if you set the driver in your config to im
, focuscropping works but kirbycropping doesn’t? That would be weird.
Then as a next step I’d check if kirbycropping/resizing works without the plugin.
I hadn’t finished yet, see edit above…
Resizing / cropping still does not work. I’ve even reinstalled imagemagick and explictly set the bin path - no luck there either. it only seems to work locally, as expected, using GD. I guess ill just use GD locally, and set IM in the domain config for my VPS.
Hm, that’s a bit strange, but I don’t know how I can possibly reproduce this. Which Kirby version are you using? 3.2.2 or the latest 3.2.3-rc1?