Image Helper

Permalink 1 user found helpful
Where is the image helper now? How do we load in in views and controllers?

pvernaglia
View Replies:
Shotster replied on at Permalink Reply
Shotster
The legacy image helper is in /concrete/src/Legacy/ImageHelper.php, and it can be loaded like...

Core::make('helper/image');

However, it's marked deprecated. I guess the new approach would be to use the facade like...

Image::create($size);
Image::open($path);
Image::load($string);

It appears to prefer Imagick and fall back to Gd. See the class definitions in the vendor directory.

-Steve
pvernaglia replied on at Permalink Reply
pvernaglia
I had already tried Core::make('helper/image'), that doesn't work. I'll take a look in vendor directory. I hope we get some documentation soon!
Shotster replied on at Permalink Reply
Shotster
> I had already tried Core::make('helper/image'), that doesn't work.

What doesn't work about it? It returns an ImageHelper object for me...

$ih = Core::make('helper/image');

What are you trying to do exactly?

-Steve
pvernaglia replied on at Permalink Reply
pvernaglia
In view.php If you do something like

[code]
$img = Core::make('html/image', array($fl));
print $img;
[code]

That displays the image full size. With image helper we could getThumbnail, outputThumbnail, size images, crop them, how do we do all that stuff now?
Shotster replied on at Permalink Reply
Shotster
$file = File::getByID($fID);                
$imageHelper = Core::make('helper/image');
$imageHelper->outputThumbnail($file, $maxWidth, $maxHeight);

But, as I said, the legacy image helper is deprecated. I'm not exactly sure how you'd do the same thing with the new approach using the image facade. Maybe someone else knows.

-Steve
andrew replied on at Permalink Reply
andrew
The new approach is just using the Imagine image library, which gets delivered to you when you access Image::load.

https://imagine.readthedocs.org/en/latest/...

It's a nice object-oriented way of manipulating images.
Shotster replied on at Permalink Reply
Shotster
> The new approach is just using the Imagine image library, which
> gets delivered to you when you access Image::load.

Yeah, but unless I'm missing something, there no longer appears to be a simple one-line call to output a scaled or cropped version as an HTML element. The legacy image helper would automatically create a file in the cache directory to use for generating thumbnail elements. Should Concrete\Core\Html\Image.php do something similar?

-Steve
pvernaglia replied on at Permalink Reply
pvernaglia
I played a little with it in a controller today

<code>
use Imagine\Image\ImageInterface;

$img = File::getByID(24);
if ($img instanceof File) {
$fr = $img->getFileResource();
$image = \Image::load($fr->read());

$data = $image->getSize();
}
</code>

I don't know if this is the way to do it, but $data gives me the image size. I didn't get much further than that, I was hoping for something similar to the old image helper

<code>
$image->outputThumbnail()
$image->getThumbnail()
etc.
</code>

But there is more to it than that, if someone has some examples please share them.
thanks
mnakalay replied on at Permalink Reply
mnakalay
To add to what Shoster is asking:
It seems we have to save the thumbnails ourselves. Does it means we have to define our own folder structure and naming convention for those files as long as we save them in the application/files/thumbnails folder?
Shotster replied on at Permalink Reply
Shotster
Just to clarify the issue a bit...

The "Image" facade, which uses the "Imagine" image library, is great for manipulating images - i.e. working with image data, such as applying filters and effects, scaling, cropping, etc. However, it provides no facilities for rendering image elements - i.e. generating mark-up such as <img> or <picture> tags. To do that, you can use the HTML "Image" class at...

/concrete/src/Html/Image.php

...and as @pvernaglia noted in a previous post, it's loaded by calling...

Core::make('html/image', array($file));

The "problem" is that it provides no way to generate a thumbnail element "on the fly". I'm suggesting that it be modified to do so. Is there any reason this would not be a good idea?

-Steve
andrew replied on at Permalink Reply
andrew
I think this makes sense. I've opened up another place to discuss it so it can be more easily tracked (along with code):

https://github.com/concrete5/concrete5-5.7.0/issues/1152...
mnakalay replied on at Permalink Reply
mnakalay
I have the feeling this is a work in progress (image helper replacement I mean).

I say that because if you look at core blocks that should implement that functionality well... they simply don't.

Example: the image block lets you set a max width and max weight and how to resize (exact match or not) but it doesn't resize the image at all, it just shows the original image.

So your best bet for the time being is probably to use the legacy helper which, by the way, implements some of the new functionality.
emirii replied on at Permalink Reply
emirii
To install and use preset thumbnails:

In your controller.php (or create custom thumbnail preset in "Settings" -> "Files" -> "Thumbnails"
use \Concrete\Core\File\Image\Thumbnail\Type\Type;
       //ADD THUMBNAIL TYPES
       $type = new Type();
       $type->setHeight(100);
       $type->setWidth(100);
       $type->setName('GalleryES Manager');
       $type->setHandle('gallery_e_s_manager');
       $type->save();


In your view.php:
use \Concrete\Core\File\Image\Thumbnail\Type\Type as Type;
use \Concrete\Core\File\Image\Thumbnail\Thumbnail as Thumbnail;
                  $fv = $f->getApprovedVersion();
                  //GET GALLERYES MANAGER TYPE
                  $type = new Type();
                  $type = $type->getByHandle('gallery_e_s_manager');
                  $type = $type->getBaseVersion();
                  $thumbnail = new Thumbnail($type, $f->getThumbnailURL('gallery_e_s_manager'));
                  $thumbTypes = $f->getThumbnails();
                  $needsThumb = TRUE;
                        foreach ($thumbTypes as $tType) {
                            if ($thumbnail == $tType) {
                                $needsThumb = FALSE;
                            }
                        }


It is a bit spaghetti code in the view, only because right now Concrete5 5.7 does not automatically generate thumbnails for existing images when creating new thumbnail types. When that image is loaded with the thumbnail handle it is not automatically resized, and the fullsize image appears, which is not good! So we need to check to make sure we don't have to generate it first before displaying.
core77 replied on at Permalink Reply
Since there are no docs at the moment... Do you mean the block controller or the package controller?

How would we install thumbnail sets with package controller?
emirii replied on at Permalink Reply
emirii
You would put this in your package controller on install:
use \Concrete\Core\File\Image\Thumbnail\Type\Type;
       //ADD THUMBNAIL TYPES
       $type = new Type();
       $type->setHeight(100);
       $type->setWidth(100);
       $type->setName('GalleryES Manager');
       $type->setHandle('gallery_e_s_manager');
       $type->save();


This will install your new thumbnail type, like single pages or blocks etc.

Then, you will use the 2nd example above to call your thumbnail. For some reason thumbnails of your new type are not automatically generated so you have to use that code block to check if the thumbnail exists first (or C5 will display the high resolution image instead) and create it if it doesnt... I don't know why it's like this but possibly will be clearer when the docs are released.

Put the use lines at the top of your code for readability also :P

EDIT: Here's the code again just in case i changed something, along with image tag for example...

use \Concrete\Core\File\Image\Thumbnail\Type\Type as Type;
use \Concrete\Core\File\Image\Thumbnail\Thumbnail as Thumbnail;
                         $f = File::getByID($previewImage);
                         //GET GALLERYES MANAGER TYPE AND COMPARE TO SEE IF WE NEED TO REGENERATE THUMBNAILS
                         //THIS IS REALLY UGLY BUT THE ONLY WAY THAT WILL GENERATE NON-EXISTENT THUMBNAILS!!!!!!
                         //WHYYY???
                         $type = new Type();
                         $type = $type->getByHandle('gallery_e_s_manager');
                         $type = $type->getBaseVersion();
                         $thumbnail = new Thumbnail($type, $f->getThumbnailURL('gallery_e_s_manager'));
                         $thumbTypes = $f->getThumbnails();
                         $needsThumb = TRUE;
                         foreach ($thumbTypes as $tType) {
                             if ($thumbnail == $tType) {
                                 $needsThumb = FALSE;
core77 replied on at Permalink Reply
Nice. Got it working.
A3020 replied on at Permalink Reply
A3020
I think you need to use the FileVersion object here:
$fv = $f->getApprovedVersion();
$fs->getThumbnailURL('gallery_e_s_manager'));

Instead of $f->getThumbnailURL(..)
A3020 replied on at Permalink Reply
A3020
Here's my approach to get Magnific Gallery working in C57:
http://www.adrikodde.nl/blog/2014/concrete-57-thumbnails/...