(Mis)using the thumb class to orient & overwrite images

Hey all,

maybe some of you remember my former topic about auto-orient and thumbnails. Well I solved that.

Now my question is, can I “misuse” the thumb class to orient all images I upload via FTP correctly and overwrite the original file? So what I need to do is:

  1. I need to prevent the thumb class from duplicating my images into the thumb folder. So I need to modify the file destination, right?
  2. The original files need to be overwritten so I don’t get duplicates in the source folder, too.

Can I achieve this by just using params? Because I still use the thumb class to generate thumbnails of the very same set of images i want to auto-orient. Confusing…

Hope this makes sense…

From looking at the source code, this should be possible (not tested though). The options are all there …

Okay I tried this:

<?php echo thumb($pic, array('destination' => true, 'root' => /test, 'url' => /test, 'autoOrient' => true, 'overwrite' => true))->url() ;?>

but as long as I have one of the params destination, root or url in it, it gives me server error 500. Seems I’m not able to write to a different directory than /thumbs. Unfortunately there’s little documentation on the thumbs params. Anyone knows how to handle this?

You need to wrap the string values in quotes, otherwise PHP will divide nothing by a non-existing constant :smiley:. Also you should use an absolute path for the root.

<?php echo thumb($pic, array('destination' => true, 'root' => kirby()->roots()->index() . DS . 'test', 'url' => '/test', 'autoOrient' => true, 'overwrite' => true))->url(); ?>
1 Like

Thanks so much lukas for pointing me into the right direction. I definitely need to dig deeper into php, hehe

So I got a lot of things to work:

Images are being created inside the right folder now!
Overwriting the original image works. I just added

'filename' => '{name}.{extension}'

This is important so kirby does not sanitize the filenames and therefor duplicates the images.

Only problem that remains is the url bit. No matter what I put in there it either way results in a URL way deeper than from my kirby directory or it puts in a “/thumbs” somewhere (some kind of default, isn’t it) so I never have the right url in my output.

how can I put a relative path to /test ?

'url' => kirby() . DS . '/test'

won’t work because of the afore mentioned /thumbs it sticks into my URL.

That would be kirby()->urls()->index() . '/test'

Works lika a charm! Thanks a lot Lukas.

Is there a section in the docs where I can read up about paths? I’d like to get a deeper understanding of how that works. For example I have no clue what

. DS .

means :smile:

DS is a Kirby-specific PHP constant that is a short form of the PHP DIRECTORY_SEPARATOR constant. This constant is either / or \, depending on your server’s operating system (backslash for Windows, slash for basically everything else).

Kirby uses this constant to be compatible with all operating systems and it is very clean if you use it as well. But it should only be used for file system paths, because URLs always use slashes, no matter what the OS is.

I hope you could follow me. If there are any questions left, don’t hesitate to ask. :wink:

Thanks for the feedback, I have added it to the list for new documentation.