CakePhP3: Creating Thumbnails for images after upload

Hi All

I am successfully using Jose Gonzalez’s Upload plugin in my CRUD forms to upload photos of garments.

But I need to create at least one 150x150 pixel thumbnail. I have added Florian Krämer’s Imagine plugin to my composer.json file.

I have added the following to the PhotosTable.php to the initialize method.

    $this->addBehavior('Burzum/Imagine.Imagine');

And have the following code in the afterSave method.

    $imageOperations = [
    'thumbnail' => [
    'height' => 600,
    'width' => 200
    ],
    ];
    
    $this->imagineObject->processImage('/files/photos/filename/DC00021.jpg',
                               '/files/photos/filename/thumb/DC00021.jpg',
                               [],
                               $imageOperations
                               );

But I get

Table “App\Model\Table\PhotosTable” is not associated with “imagineObject”

Any ideas? I’m assuming afterSave is the best place to do this?

Andy

I think, as the book says you should call like this
$this->Imagine->processImage(...);

Thanks for the suggestion Raul - bu tI already tried that…

Not an answer to your question I’m afraid, but I highly recommend using Glide by the phpleague: http://glide.thephpleague.com/

It allows you to manipulate your images on the fly, so if you need to create a particular thumbnail, you simply create a link to the image and add some parameters. Glide then automatically creates and displays the desired image file (well, a bit of configuration is necessary, but only once).

It can be a big pain to create thumbnails manually especially after the upload, but with Glide you just don’t have to worry about any of that anymore. There’s also a Glide plugin especially for Cakephp and it’s fairly easy to get it to work (but the cake plugin is not absolutely necessary).

So yeah, maybe this helps someone since I feel like not many people have even heard about this option.

Turns out I needed to use $this->processImage(…);

Andy

1 Like