Learn one, forget three: How to use FuelPHP's Image class

table of contents
Hello.
I'm Mandai, the Wild team member in charge of development.
When working with images in PHP, you're usually faced with a choice between using GD or ImageMagick.
Whether you choose ImageMagick for its conversion accuracy or GD for its easier installation depends on your preference and the specific situation.
However, one annoying thing is that the usage is completely different, so you have to learn two different ways of processing images
This time, we will introduce FuelPHP's Image class, which wraps the basic functions of these two libraries and allows them to be called using common methods
Furthermore, even if you don't have ImageMagick installed, FuelPHP also provides a way to convert images using the `convert` command via the `exec` function, provided you have ImageMagick installed. I
am using FuelPHP version 1.8.
Install the library
Installation is basically easy as it can be done via the package manager.
I've summarized the installation procedures for both GD and ImageMagick.
Installing GD
In the case of CentOS, GD installation can be completed using the yum command.
This time, we added GD in an environment where PHP 5.6 from remi was installed.
sudo yum install --enablerepo=remi-php56 php-gd
Finally, restart the web server, and you're done.
Check the output of phpinfo(); if there is a description related to GD, it was successful.
Installing ImageMagick
Next, here are the steps for installing ImageMagick.
Incidentally, if you want to edit images using PHP, having either GD or ImageMagick installed is sufficient.
Both are not necessary.
First, install ImageMagick using the yum command
sudo yum install ImageMagick
Depending on the Linux distribution you're running, it might already be installed.
If so, proceed to the next step.
Next, we'll install IMagick, an extension library for using ImageMagick from PHP.
Installing IMagick
Next, we'll use PECL to install the PHP library, IMagick.
Without thinking too much, if you just type the following command...
pecl install imagick Cannot install, php_dir for channel "pecl.php.net" is not writeable by the current user
You'll get an error message like this.
Try running it with `sudo` as shown below.
sudo pecl install imagick downloading imagick-3.4.3RC1.tgz ... Starting to download imagick-3.4.3RC1.tgz (245,140 bytes) ................................done: 245,140 bytes 19 source files, building running: phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 Please provide the prefix of Imagemagick installation [autodetect] : building in /var/tmp/pear-build-rootELyyWP/imagick-3.4.3RC1 running: /var/tmp/imagick/configure --with-php-config=/usr/bin/php-config --with-imagick checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking for cc... cc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether checking we are cross compiling... no checking for suffix of executables... checking for suffix of object files... o whether checking we are using the GNU C compiler... yes checking whether cc accepts -g... yes for cc option to accept ISO C89... none needed how to run the C preprocessor... cc -E checking for icc... no checking for suncc... no checking whether cc understands -c and -o together... yes checking for system library directory... lib checking if compiler supports -R... no checking if compiler supports -Wl,-rpath,... yes checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu checking for PHP prefix... /usr checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib checking for PHP extension directory... /usr/lib64/php/modules checking for PHP installed headers prefix... /usr/include/php checking if debug is enabled... no checking if zts is enabled... no checking for re2c... no configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. checking for gawk... gawk checking whether to enable the imagick extension... yes, shared checking for pkg-config... /usr/bin/pkg-config checking ImageMagick MagickWand API configuration program... checking Testing /usr/local/bin/MagickWand-config... Doesn't exist checking Testing /usr/bin/MagickWand-config... Doesn't exist checking Testing /usr/sbin/bin/MagickWand-config... Doesn't exist checking Testing /opt/bin/MagickWand-config... Doesn't exist checking Testing /opt/local/bin/MagickWand-config... Doesn't exist configure: error: not found. Please provide a path to MagickWand-config or Wand-config program. ERROR: `/var/tmp/imagick/configure --with-php-config=/usr/bin/php-config --with-imagick' failed
It stopped due to an error.
In cases like this, installing a development package called [package name]-devel often solves the problem, but I'm not sure if it even exists. Anyway, I'll try running the following command.
sudo yum install ImageMagick-devel
It seems to have been found successfully, so I will proceed with the installation.
There are many dependent packages, so it may take some time.
Once the installation is successful, try installing IMagick from PECL again
sudo pecl install imagick downloading imagick-3.4.3RC1.tgz ... Starting to download imagick-3.4.3RC1.tgz (245,140 bytes) ................................done: 245,140 bytes 19 source files, building -- Omitted -- Build process completed successfully Installing '/usr/lib64/php/modules/imagick.so' Installing '/usr/include/php/ext/imagick/php_imagick_shared.h' install ok: channel://pecl.php.net/imagick-3.4.3RC1 configuration option "php_ini" is not set to php.ini location You should add "extension=imagick.so" to php.ini
The compilation seems to have finished successfully, and the installation was completed.
Towards the end, there's a message asking you to add "extension=imagick.so" to php.ini, so please add it.
If you want to do it smartly, just create an ini file with an appropriate name under /etc/php.d/ and add "extension=imagick.so" to it
After that, just restart the web server, just like with GD
Preparing to use various image libraries from FuelPHP
First, we need to prepare a config file that handles the settings for the Image class.
The default setting is fuel/core/config/image.php, so copy this to fuel/app/config.
From the FuelPHP root directory, you can copy it with the following command.
cp -a fuel/core/config/image.php fuel/app/config/image.php
After copying, edit fuel/app/config/image.php
The setting is on line 26, which is the setting for the library to be used
'driver' => 'gd',
By default, GD is selected, so if you want to use ImageMagick, change it to either 'imagemagick' or 'imagick'
Next, set the image quality
'quality' => 94,
By default, it's set to the highest quality at 100%, which will increase the file size of converted JPEGs by several times.
GD's default value is 75, so it's better to adjust it to around 75-95.
The rest is up to your preference
By default, the image is reloaded after executing the save or output method.
If for some reason you want to use the same image again (for example, saving an outputted image),
'persistence' => true,
This seems to be good as it will prevent reloading
Let's try editing
Image loading is handled by the Fuel\Core\Image class.
This class selects the appropriate driver for GD, IMagick, or ImageMagick based on the configuration settings.
$image = Fuel\Core\Image::forge(null, '/path/to/image');
Image loading is now complete.
When using GD, you had to manually select and use the `imagecreatefrom*` functions, but this class identifies and defines them for you.
However, since it only identifies files by extension, an error will occur when loading files without an extension.
In this case, do not forge, but use the load method
$image = Fuel\Core\Image::load('/path/to/image', false, 'jpg');
The third argument allows you to specify the file extension.
If you're editing a file without an extension, you'll have to check it in binary format, which requires some ingenuity.
The second argument is only valid for GD, and I couldn't figure out its purpose.
In this case, you cannot add or overwrite the config, so you need to create a proper configuration file in advance
For various editing techniques,the Image - Class - FuelPHP documentationis very helpful.
The methods described there are common to all drivers, so understanding them is sufficient.
For cropping images,the `crop` methodis very useful.
If you want to crop from the center,the `crop_resize` methodis a convenient option.
For resizing images,the `resize` methodis very convenient.
for rotating imagesThe `rotate` methodis useful
When using GD, it seems that you cannot set a transparent color when the background is visible at rotations of 30 degrees or 45 degrees.
flipping an image (changing its orientation vertically or horizontally, as if reflected in a mirror)The `flip` classis useful for
for adding watermarks to imagesThe `watermark` methodis useful
for adding a border to an imageThe `border` methodis useful
If you want to mask an image that has been loaded,the `mask` methodis useful.
for converting to rounded-corner imagesThe `rounded` methodis convenient
However, when using this method in combination with ImageMagick and transparent images, there seems to be a bug where the transparent parts of the four corners are not made transparent.
GD calculates color information in PHP.
If you want to know the size of an image,the `sizes` methodis useful.
In this case, the size refers to the number of pixels in the width and height of the image, not the file size.
To get the file size, use the standard function `filesize`.
When converting an image to grayscale,the `grayscale` methodis useful.
Imagick and ImageMagick use dedicated functions provided by ImageMagick, while GD extracts all pixels in a loop and calculates the color information of each pixel in PHP.
If you want to send an image via HTTP headers and display it in the browser,the `output` methodis useful.
For simple editing, the above should suffice.
Resizing, in particular, is very easy and convenient.
That's all
0
