We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use Imagick with images upload?

Hello. How can I use Imagick with images upload?

At the moment I upload images like this.

//...
if ($this->request->hasFiles()) 
    $photo = $this->request->getUploadedFiles()[0];

    if (!$photo->getError()) {
        $photo->moveTo('images/image.webp');
    }
}
//...

Where it's more correct to use Imagick in this code?



10.1k

if you mean the Imagepack. This isn't part of the phalcon framework. The package is written in javascript so it would be hard to embed inside a php framework like Phalcon. Not sure but don't think there are any php implementations at all of this technique.



10.1k
Accepted
answer

Ah ok. No problem. Make sure you have the imagick php extension installed and you can do just something like this.

<?php

if ($this->request->hasFiles()) 
    $photo = $this->request->getUploadedFiles()[0];

    if (!$photo->getError()) {
        $photo->moveTo('images/image.webp');
        $image = new \Phalcon\Image\Adapter\Imagick("images/image.webp");
        $image->resize(200, 200)->rotate(90)->crop(100, 100);
        if ($image->save()) {
            echo "success";
        }
    }
}

Not sure what formats Imagick supports. You should test if webp works.

Oh, thanks! And thanks for information of extension. I thought the latest version of Imagick was already included in the out-of-box Phalcon. Сan I give the photo to Imagick only after uploading it to the final directory? Up to this point, I thought that I could do it in the file upload process.



10.1k
edited Jan '19

Imagick is not a extension Phalcon maintains. It's something generic. Like pdo_mysql You could move it to a temp dir first and handle the conversion there before moving it to the final dir.

I don't think you can do it purely in memory (correct me of I'm wrong).

But I think php already created files in the php tmp before executing the scripts. You could try to the mutation there but not sure if that's aloud and wise to do.

My flow normally is:

Check request for files -> test types -> move to project tmp dir -> handle conversions -> move to final dir

Imagick is not a extension Phalcon maintains. It's something generic. Like pdo_mysql You could move it to a temp dir first and handle the conversion there before moving it to the final dir.

I don't think you can do it purely in memory (correct me of I'm wrong).

But I think php already created files in the php tmp before executing the scripts. You could try to the mutation there but not sure if that's aloud and wise to do.

My flow normally is:

Check request for files -> test types -> move to project tmp dir -> handle conversions -> move to final dir

正解!