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

Can't use Imagick

Hi,

i want to use Imagick, but not with phalcon adapter:

 public function uploadAction(){

    $croppedImage = $this->request->getPost('croppedImage', 'striptags');
    $field = $this->request->getPost('field', 'striptags');

    $data = explode( ',', $croppedImage);
    $imageBlob = base64_decode($data[1]);

    $imagick = new \Imagick();
    $imagick->readImageBlob($imageBlob);
    $imagick->setBackgroundColor(new ImagickPixel('transparent'));

    header("Content-Type: image/*");
    $imagick->writeImage('image.png');

    $this->view->setRenderLevel(
        View::LEVEL_ACTION_VIEW
    );
}

i get an 500 error, but with pure PHP it is working for me.

For my frontend i use cropit: https://scottcheng.github.io/cropit/

Thx for help :-) Stefan



85.5k
edited Sep '17

php extension is imagick enabled ?

btw a little bit of advice ( i have been using imagick for 5 years now ), dont use imagick adapter, its barely maintained, there is 1 signle hguy who maintain the extension, and he has close to 0 time to develop. he fixes 1-2 issues for ~ 6 months. So just use shell_exec or whatever exec you like.

//edit, imagick lacks some functionallity and has only bugs, so i just dont see a real reason using it ?

edited Sep '17

Well, have you checked logs, 500 HTTP status usually means you have fatal error somewhere. During development, you should enable error display, so it would make clear idea what's going on.

Could it be in your code not using root namespace here:

    $imagick->setBackgroundColor(new ImagickPixel('transparent'));

    //try 
    $imagick->setBackgroundColor(new \ImagickPixel('transparent'));

    //Also,  you should not be using raw PHP header() like this, instead use framework Response component
    //header("Content-Type: image/*");
    $this->response->setContentType('image/*');

Bottomline: I fully agree with @Izopi4a - deprecated software should not be used for new projects.



59.9k

Hello @Izopi4a yes it is enabled, i test it with raw PHP. Normally i also use shell_exec, will translate this later :-)

Hello @stamster ahhh in my code i capsuled it $imagick = new \Imagick();, but not in these lines, maybe it could be the problem.

Sometimes i am testing with raw PHP first and implement it later to Phalcon :-) So i will change this lines ;-)

Would it be better to use GD?

GD lib is supported, so yes, sure it's better choice.

Did you find the root cause of that fatal error?



59.9k
Accepted
answer

Hello @stamster,

yes i found it :-) First problem was the \ at new \ImagickPixel, the second problem was the

$this->view->setRenderLevel(
    View::LEVEL_ACTION_VIEW
);

part.

public function uploadAction(){

    $croppedImage = $this->request->getPost('croppedImage', 'striptags');
    $field = $this->request->getPost('field', 'striptags');

    $data = explode( ',', $croppedImage);
    $imageBlob = base64_decode($data[1]);

    $imagick = new \Imagick();
    $imagick->readImageBlob($imageBlob);
    $imagick->setBackgroundColor(new \ImagickPixel('transparent'));

    $this->response->setContentType('image/*');
    $imagick->writeImage('image.png');

}

Now it works for me, but i will take a look at GD in future :-)

Rgds Stefan