You could change line 12 of the code above to add another condition to the if
statement.
Change this:
if ($file->type() == 'image' and ($file->width() > $maxDimension or $file->height() > $maxDimension)) {
to this:
if ($file->filename() != 'dontResizeMe.jpg' and ($file->type() == 'image' and ($file->width() > $maxDimension or $file->height() > $maxDimension))) {
Note the extra set of brackets to keep the and
/ or
logic in line.
I’m sure there’s a more elegant way to do this, but you can use any of the $file->
methods here.
Edit: changed the or/and logic as per Lukas’ comment.
Maybe turn this into a plugin? Then it would separate logic from config.
Thank you @samnabi, this is very helpful.
You can also use
function shrinkImage($file, $maxDimension = c::get('images.maxWidth', 1000)) {
that way you can set the max. width for every environment.
1 Like
hm it seems that dontResizeMe.jpg
is still resized:
kirby()->hook('panel.file.upload', 'shrinkImage');
kirby()->hook('panel.file.replace', 'shrinkImage');
function shrinkImage($file, $maxDimension = 1000) {
try {
if ($file->filename() != 'dontResizeMe.jpg' or ($file->type() == 'image' and ($file->width() > $maxDimension or $file- >height() > $maxDimension))) {
$originalPath = $file->dir().'/'.$file->filename();
$resized = $file->resize($maxDimension,$maxDimension);
$resizedPath = $resized->dir().'/'.$resized->filename();
copy($resizedPath, $originalPath);
unlink($resizedPath);
}
} catch(Exception $e) {
return response::error($e->getMessage());
}
}
I believe the or
must be an and
:
$file->filename() != 'dontResizeMe.jpg' and
1 Like
@lukasbestle That’s it. 
I also tried with @Pascalmh exemple:
function shrinkImage($file, $maxDimension = c::get('images.maxWidth', 1000)) {
but get an error :
Parse error: syntax error, unexpected ‘(’, expecting ‘)’ in A:\UwAmp\www
The issue here is that you can’t call functions in default values. It has to be:
function shrinkImage($file, $maxDimension = null) {
if(!$maxDimension) $maxDimension = c::get('images.maxWidth', 1000);
...
2 Likes
Perfect, thank you all, for explications and solutions.
The resize feature is great. I just wanted to add that it’s also possible to limit file sizes in the blueprint. It will not resize, just prevent too large images from ever being uploaded.
files:
width: 150
height: 200
https://getkirby.com/docs/panel/blueprints/file-settings
1 Like
Hi! Just came across this – it’s a great addon and easy to use for noobs like me with no Php experience.
I was wondering if there was an easy way to adjust the compression level of the JPG while the resize is taking place? Most of the images I’m looking to upload are high res photos with a great level of detail, and while the max width works great, they do lose a lot of resolution in the process.
Could you guys help me with that?
Cheers
You can add a third argument to Line 18 of the gist.
$resized = $file->resize($maxDimension,$maxDimension,100);
The 100
is the image quality, anywhere from 0 to 100. Default is 80.
1 Like
Hello and thank you for share it.
I’ve got one Problem. The images will be scaled down, but when I upload an image in portrait format, it will be rotated by 90 degrees.
Do images in portrait format get uploaded correctly if you disable resizing?
Yes they do. I also activated ImageMagic. But that did not solve the problem either.
not working for me, tried with GD and IM…
at any circumstance the original file will not be resized…
Seems to be working still for me. Are you on Kirby 2.4.1?
Also, what user is your script running as? On an Apache server it’s typically www-data
. Make sure that user has permission to write, move, and delete files.
running 2.4.1, apache, not sure how to tell you “what user” and or if the user is www-data as well as permission.