Remember one thing, forget three things: How to use FuelPHP's Image class

table of contents
Hello.
I'm Mandai, in charge of Wild on the development team.
When dealing with images in PHP, you are usually forced to choose between GD and ImageMagick.Whether
you use ImageMagick for the accuracy of the converted image or GD for its ease of installation depends on your preference and the 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, FuelPHP provides a way to convert using the convert command through the exec function, even if you do not have IMagick installed, as long as you have ImageMagick installed.
The version of FuelPHP used is 1.8.
Install the library
Installation is basically easy, as it can be done via a package manager.
I have summarized the installation procedures for GD and ImageMagick.
Installing GD
In the case of CentOS, GD installation can be completed with the yum command.
This time, GD was added to an environment where remi's PHP5.6 was installed.
sudo yum install --enablerepo=remi-php56 php-gd
All that's left to do is restart the web server and you're done.
Check the phpinfo() result and if there's anything related to GD, then you've succeeded.
Installing ImageMagick
Next, we will show you how to install ImageMagick.
By the way, if you want to edit images with PHP, you only need to have either GD or ImageMagick installed.
You don't need both.
First, install ImageMagick using the yum command
sudo yum install ImageMagick
Depending on the version of Linux you are using, it may already be installed.
If so, proceed to the next step.
Next, install IMagick, an extension library for using ImageMagick from PHP.
Installing IMagick
Next, use PECL to install the PHP library, IMagick.
Without thinking, 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 will get scolded for this.
Run 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 with an error.
In such cases, I have found that installing the development package [package name]-devel often works fine, but I don't know if this is the case or not, so I'll try typing the following command.
sudo yum install ImageMagick-devel
It seems to be installed successfully, so let's install it.
There are many dependent packages, so it may take a while.
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
It looks like the compilation and installation were successful.
At the end, there is a message telling you to add "extension=imagick.so" to php.ini, so do that.
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, prepare a config file that handles the Image class settings.
The default setting is fuel/core/config/image.php, so copy this to fuel/app/config.
If you are using 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, the highest quality is set to 100%, which means that the converted file size will be several times larger if you use JPEG.
The default value for GD is 75, so it is recommended to adjust it to around 75-95.
The rest is up to your preference
By default, the image will be reloaded once you execute the save or output method.
If you want to use the same image again for some reason (for example, to save the output image),
'persistence' => true,
This seems to be good as it will prevent reloading
Let's try editing
Images are loaded from the Fuel\Core\Image class,
which selects the driver for GD, IMagick, or ImageMagick according to the config.
$image = Fuel\Core\Image::forge(null, '/path/to/image');
This completes the image loading.
When using GD, you had to manually select and use the imagecreatefrom* functions, but this class distinguishes between them and defines them for you.
However, since it only distinguishes between them based on the file extension, an error will occur when loading a file 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 extension.
If you want to edit a file without an extension, you have no choice but to check it in binary, so you'll need to use a little ingenuity.
The second argument is only valid for GD, and I wasn't sure how to use it.
In this case, you cannot add or overwrite the config, so you need to create a proper configuration file in advance
For various editing, please refer to
the Image - Class - FuelPHP documentation The methods here are common to all drivers, so you only need to understand this.
If you want to crop an image, the crop method is useful.
If you want to crop from the center, the crop_resize method is handy.
If you want to resize an image, the resize method is useful.
The rotate method is useful
for rotating an image When using GD, if the background is visible at an angle of 30 or 45 degrees, it seems that you cannot set a transparent color.
The flip class is useful when you want to flip an image (turn it upside down or left to right as if it were a mirror image)
If you want to add a watermark to an image, the watermark method is useful.
If you want to add a border to an image, the border method is useful.
If you want to mask an image onto the loaded image, the mask method is useful:
The rounded method is useful
for converting to rounded corners However, when using this method in combination with ImageMagick and a transparent image, there seems to be a problem where the transparent corners are not transparent.
GD calculates the color information on PHP.
If you want to know the image size, the sizes method is useful.
The size in this case is 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.
The grayscale method is useful
for converting an image to grayscale 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.
The output method is useful if you want to send HTTP headers and display an image in the browser
If you're just doing some simple editing, the above should be enough.
Resizing is especially easy, so it's very useful.
That's it.
0