6

I need to do some actions on jpeg images - Heroku's PHP GD does not allow that. I've read that it is possible with Imagick, so i rewritten the code, pushed it to heroku and...

PHP Fatal error: Class 'Imagick' not found in [...]

So am I doing something wrong(code works locally)?

$tlo = new Imagick();
$tlo->newImage(640, 480, new ImagickPixel('white'));
$tlo->setImageFormat('jpg');

Is there any way of working with jpg on heroku?

anonim1133
  • 95
  • 1
  • 7
  • 1
    As for doing wrong - yes, you are trying to run code on different environment, without fully realizing how it works. `Imagick` as opposed to `GD` is not part of php, requires external library (ImageMagick) and must be installed additionally. – dev-null-dweller Jul 03 '12 at 22:58
  • Here's my compiled `imagick.so` on [github](https://github.com/alkhoo/heroku-cedar-php-extension) with instructions. Takes 5 mins to set it up – Alvin K. Apr 30 '13 at 07:39

2 Answers2

14

A simpler approach is to install ImageMagick using composer.json, as explained here: https://devcenter.heroku.com/articles/php-support#using-optional-extensions

You just need to include imagick in the require section and update composer:

{
    ...
    "require": {
         "ext-imagick": "*",
         ...
    }
}
eillarra
  • 5,027
  • 1
  • 26
  • 32
  • 1
    Note that, after adding this line to your composer.json, you will also need to run "composer update" to update your composer.lock file. Then you can check both files into Git and push to Heroku, and it will install Imagemagick. – Derrick Miller Dec 22 '20 at 01:22
1

ImageMagick, a command-line utility and programming library, must be installed on the system for Imagick to work.

If it's not working for you, then presumably Heroku's PHP web dynos do not have this installed by default. You have two options: you can find some convoluted way to package ImageMagick with your application itself, for instance by adding compiled binaries to your git source tree. Or, you can modify the Heroku PHP buildpack, which is the set of rules that sets up the web dyno before your application is deployed, to install ImageMagick along with Apache and PHP itself. The latter approach is more likely to work.

Once you've modified the buildpack, change your application to point to your buildpack fork with the command-line Heroku tools (the --buildpack option) and redeploy.

Community
  • 1
  • 1
Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73