5.7 Scaling and cropping image to specific size within composer

Permalink
What is the best way for an image to be scaled and cropped to a specific size within composer?

I just want my client to select an image within the composer form that will then be scaled and cropped to a size of my choosing.

Do i use thumbnails, attributes or an image block template? I have searched and haven't found a solution. It would of been easier if these options were included within the image block in composer.

Regards
Luke

View Replies: View Best Answer
goodnightfirefly replied on at Permalink Best Answer Reply
goodnightfirefly
The thumbnail system does this automatically when images are uploaded, so your client can just select an image in the composer form, and then you can output your special thumbnail for that image.

This will by default scale and center your image, but you can later adjust the scaling and position of the crop by visiting the File Manager and editing the properties of your image. There will be a 'Thumbnails' option there.

<?php $f = File::getByID($yourFileID); ?>
<img src="<?php echo $f->getThumbnailURL('your_thumbnail_handle'); ?>" />


Alternatively, if you just want to spit out an image that is x by y, you can do so:

<?php $ih = Core::make('helper/image');
$f = File::getByID($yourFileID);
$image = $ih->getThumbnail($f, 400, 300, true); // set false if you don't want it cropped
?>
<img src="<?php echo $image->src; ?>" />
lwduk replied on at Permalink Reply
Thank you for clarifying this. What is the best way to include the coding on a composer page type?
I could just create a page template with the code included then use this within a page type but is there a smarter way without having to create an additional page template which seems excessive?

There may be a simple answer to this which I am just missing?
goodnightfirefly replied on at Permalink Reply
goodnightfirefly
If hardcoding it into a page template is overkill as you mentioned, you can instead create a 'Custom Template' for the image block and use the code I mentioned.

Some docs about using Custom Templates: https://www.concrete5.org/documentation/developers/5.7/working-with-...
lwduk replied on at Permalink Reply
Thanks, it confirms what I thought maybe the answer.