Imagick on heroku - is it possible? - php

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?

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": "*",
...
}
}

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.

Related

PHP Serverless Slim can't execute imagettftext

I intend to use a serverless php function to generate an image with TrueType fonts. In order to do that, I chose bref with Serverless framework and composer.
To process the image and include the text, I'm using imagettftext function from gd library. I ran php -S localhost:8000 index.php for local testing purposes. It worked fine, I got the output image as I expected it.
Thus I ran composer install --optimize-autoloader --no-dev and then serverless deploy to create a AWS Lamda function. However, when I called the deployed function's endpoint, it showed a Slim Application Error - without any detail:
A website error has occurred. Sorry for the temporary inconvenience.
So I took a look at AWS Cloudwatch logs and found this error message:
Message: Call to undefined function imagettftext()
Searching about this error, I found out it's often related to gd library missing. Then I compared phpinfo() from local test to the one from deployed function. Both have gd installed, the difference is that the local one has FreeType Support enabled.
Could you help me to find a way to enable this FreeType Support also for the serverless function? Is it possible to require this support through composer.json?
There's an issue open on GitHub at the moment regarding compiling PHP with freetype support, which explains the result you're seeing: https://github.com/brefphp/bref/issues/497.
GD needs to know about freetype as it's compiled into PHP, so I doubt you would be able to include it with composer

I've used composer to install drupal - why do I still have unresolved php dependencies?

I'm trying to build a drupal webserver using composer, which I am told is the right way to do it.
The command I've used to install drupal is this:
composer create-project drupal-composer/drupal-project:8.x-dev --stability dev mysite --no-interaction
However, when I start my apache server I get a bunch of errors about missing libraries, functions, etc. I can resolve these by googling each one and installing the required php library - but isn't composer supposed to do this for me? According to the composer docs;
Composer is a tool for dependency management in PHP. It allows you to
declare the libraries your project depends on and it will manage
(install/update) them for you.
When I navigate to the mysite/vendor directory I see a bunch of drupal-related packages, but as far as I can tell these are only drupal dependencies, not php dependencies. Am I missing something here?
I think the confusion here comes from the word 'libraries'.
Composer is a tool for dependency management written in PHP.
PHP itself relies on a scripting engine (originally Zend, but there are alternatives like Facebook's HHVM, etc.), it works as a compiler and runtime engine and it needs 'libraries' - think of it as modules or extensions. These modules are mostly written in C, some in C++.
Having 'missing libraries' errors when Apache starts up does not mean Composer has missed any Drupal dependencies, it means PHP needs to be configured or rebuilt properly with the missing modules.
There are 3 types of modules (PHP's extensions membership):
Core modules that cannot be left out of a PHP binary with compilation options.
Core extension modules that are bundled with PHP core but not necessarily enabled.
External modules that are not bundled with PHP core. These modules are available from a repository called PECL (PHP Extension Community Library).
For example, if you were to speedup Drupal or any PHP applications by caching both PHP code and user variables, you would need some opcode like APC. That said, adding APC settings to php.ini would not work as is without the corresponding module being compiled with PHP.

How to install imagick in xampp/wampp in Windows

Today I have tried to install imagick in my xampp a hundred times. And I did it. If you have troubles, please try following these steps:
Since xampp is x86, check if your Image Magick (the program) is x86. If not, download the latest version here: imagemagick.org/script/binary-releases.php#windows
Please remember that u MUST download the Win32 dynamic at 16 bits-per-pixel.
Install Image Magick at C:\imagemagick.
Download the latest stable version of imagick here: . You MUST download the 5.x Thread Safe (TS) x86. The x in 5.x is the version of your PHP.
Put the php_imagick.dll in php/ext folder of your xampp installation.
Add this in your php.ini: extension=php_imagick.dll
Here is when I have a lot of trouble. If you try to start the server (or writing php in the console (start->cmd)), you will get an error. If that error says something like this: Unable to find the entry point for the procedure FlattenImages in the DLL CORE_RL_magick.dll it means that you're well underway.
Now, you must go into the installation folder of Image Magick and overwrite the DLL you downloaded with the php_imagick.dll. All of them start with the prefix CORE_RL_.
Once you overwrote all of them, you can try writing php in the windows console. I don't say something means that the imagick is functional (you can check in localhost/xampp/ > phpinfo() link. Personally, I had to install a new DLL: vcomp110.dll.
If someone has the same error as me (the system can't found the vcomp11.dd) download it here
Now, if you try executing php on console, or just initializing the apache module of xampp, you should not have errors.
Goodbye and hope I can help someone.
EDIT: when tried to convert some-img.bmp some-result.jpg in the console, I had this error:
convert.exe: unable to open module file 'C:\Users\my-acc\.magick\IM_MOD_RL_BMP_.dll': No such file or directory # ...
This happened when there is no folder .magick in my-acc. The thing you need to do is this:
Create a folder named .magick in the my-acc folder. Since the name starts with a stop, you need to create it through the console. Here are the commands:
1.1. c:
1.2. cd Users\my-acc
1.3. mkdir .magick
Go to C:\imagemagick\modules\coders and copy the entire files into the latest folder created.
Now, you will be able to use Image Magick through the commands or the PHP Classes.
On background, if you use Codeigniter, i have no idea how to use the image_lib with imagick. So, it's better with the own imagick classes. Here a simple example:
$image = new Imagick($upload_data['full_path']);
$image->thumbnailImage(250,0);
if($image->writeImage($upload_data['file_path'].$upload_data['raw_name'].'_thumb'.$upload_data['file_ext'])){
something here;
}
$image->clear();
This answer might be extremely naive, but, I've found that getting things done in Windows is generally too painful. Many people agree with that and have found solace in a tool known as "chocolatey".
First, install chocolatey on your Windows machine (super easy install instructions):
https://chocolatey.org/
Then, use chocolatey to install everything else that you need (imagemagick in this case):
https://chocolatey.org/packages/imagemagick
I suspect you might have to do some minor tweaking to configuration files after that, plus handle all of the "Windows permissions" needed to get things to work smoothly. Permissions are can be as simple (and not necessarily safe) as "full control" to some user, plus activating inheritance and overwriting all children's settings to use inheritance.
In short, install, configure, permissions, go.
PS. This answer might be incomplete/prototypical/experimental, since I don't run codeigniter, php, imagemagick on Windows.
This how-to of Winderfind is wonderful, but for me it was not yet complete to troubleshoot all problems. First of all, have a look at this tutorial:
http://hrt0kmt.hatenablog.com/entry/2015/05/27/170608
Secondly I discovered that not all versions of ImageMagick will work with all versions of php_imagick.dll. Check out my and others answers on this post for further troubleshooting:
ImageMagick supported formats no value
Great tutorial,
If you are running windows 11, and installing xampp with php 8.1, you need to install 64bit versions:
Win32 dynamic at 16 bits-per-pixel
Becomes
Win64 dynamic at 16 bits-per-pixel
And
5.x Thread Safe (TS) x86
Becomes
8.1 Thread Safe (TS) x64
If you've installed the 32bit versions (as I did), you can just download the 64bit versions and overwrite all the files. Then restart apache and it should start working.

Point imagick to location other than /usr/bin

I'm trying to compile the imagick extension for Heroku, but I need it to look in the /app/bin folder for ImageMagick rather than the /usr/bin/ folder. I've used as much google-fu as I have, but I can't figure out how to build the imagick.so extension and have it point to a different folder.
From PHP, if I do shell_exec("which convert") it echoes the /app/bin version, but when I do Imagick::getVersion() it points to the /usr/bin/ version. This makes me think it's an issue in the extension.
I haven't ever had to make my own extension from source, so that could definitely be part of the reason. Anything you could do to point me in the right direction would be helpful.
Try with a php.ini file in the webroot, with the following contents:
extension_dir="/app/bin"
extension=imagick.so
You may have the wrong end of the stick. You don't compile the Imagick extension against the Image Magick executables but against the Image Magick libraries. i.e. if you're installing Image Magick through a package manager, you should be installing "ImageMagick-devel" not "ImageMagick"
You can then set the directory that Imagick should look for the Image Magick libraries for with the setting:
./configure --libdir=/usr/lib64 --with-php-config=/usr/local/bin/php-config
Or similar depending on where the libraries are installed, and then call make.
However if you're already compiling Imagick you might as well as also compile Image Magick from source.
Edit
If I was standing behind you, this would probably be solvable in 5 minutes. I suggest:
1) Find where the Imagick libraries actually are on your system - in particular find the libMagickWand-6.Q16.so file.
2) Make sure you're copying the Image Magick libraries that you want to use to the live server as well as the Imagick library, unless you're statically compiling it (which you probably aren't).
3) Rename the lib that you don't want it to be compiled against. See if compiling Imagick picks the right version up then.
4) Debug the ./configure script for Imagick - you can print stuff out like AC_MSG_RESULT(libs before $DEBUG_LIBS) as I had to do for one issue: https://github.com/mkoppanen/imagick/issues/8 and ensure it's picking the right version up when compiling.
5) Raise an issue at https://github.com/mkoppanen/imagick/ and get the Imagick guy to have a look.

What are the odds of GD or ImageMagick already being part of a client's PHP install?

I'm working on a package which includes rescaling of images in PHP. For image rescaling, PHP has the GD and ImageMagick libraries. Are those likely to be part of a given client's PHP install already?
Is there a "default" install of PHP, for that matter? By default, are either GD or ImageMagick included?
If neither is installed, should I have some sort of horrible fallback position of reading, rescaling, and saving GIFs, JPGs, and PNGs, or is it simple to add GD or ImageMagick to an existing install?
i can't remember when i've last seen a lamp hosting provider without GD. the imagemagick extension is not that widespread. if they run their server themself, they really should be able to activate one of it or both. in your place i'd build the full functionality with GD and a (probably reduced) imagemagick fallback. if they got neither, show them the basic concepts of the wheel and/or fire, they should be grateful.
PS: i encountered providers that deactivated certain GD functions (computationally too intensive on a shared hosting environment), like imagerotate and imagefilter.
GD is the easiet library to include in php.. it is "only" an extension, mainly included in php packages... the only operation needed is to activate the extension.
Regarding ImageMagik, it is more complecated, since the php imageMagik library is only an interface for the image magik software.
Using imageMagik require both software and php library installed
It is reasonable to expect the client to have the capability or an understanding of how to add the library. Are you selling software or servers maintenance?
If you're going for the whole "ease of use" angle, you'll want to package php with your app (and GD installed already), of which I'm not sure the legality. (but there's probably some way to get it done)
90% GD - 10% ImageMagick
Windows - Yes
Linux/MAC - No (but fairly easy to install)

Categories