[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

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

Hello.
I'm Mandai, in charge of Wild on the development team.

When handling images with PHP, you are usually forced to choose between using GD and ImageMagick.
I think it depends on your preference and situation, whether to use ImageMagick for its accuracy after conversion or GD, which is easy to install.

However, the one troublesome thing is that you need to learn two different image processing methods, as the usage is completely different.

This time, we will introduce FuelPHP's Image class, which wraps the basic functions of these two libraries and can be called using a common method.

Furthermore, FuelPHP also provides a way to convert using the convert command through the exec function if you have ImageMagick installed, even if you do not have IMagick installed.
FuelPHP version 1.8 is used.

Install the library

Installation is basically easy as it can be done via a package manager.
I have summarized the installation steps for GD and ImageMagick.

 

Installing GD

For CentOS, GD installation is completed using the yum command.
This time, I added GD in an environment where remi's PHP5.6 was installed.

sudo yum install --enablerepo=remi-php56 php-gd

Then restart the web server and you're done.
Look at the result of phpinfo() and if there is a description about GD, it is a success.

 

Installing ImageMagick

Next are the steps 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 Linux you run, it may already be installed.
At that point, move on.
Next, install IMagick, an extension library for handling ImageMagick from PHP.

 

Installing IMagick

Next, use PECL to install the PHP side library, IMagick.
If you run the command below without thinking...

pecl install imagick Cannot install, php_dir for channel "pecl.php.net" is not writeable by the current user

That's what makes me angry.
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 whether checking the C compiler works... yes checking whether we are cross compiling... no checking for suffix of executables... checking for suffix of object files ... o checking whether we are using the GNU C compiler... yes checking whether cc accepts -g... yes checking for cc option to accept ISO C89... none needed checking how to run the C preprocessor... cc -E checking for icc... no checking for suncc... no whether checking 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 such cases, it is my experience that installing a development package called [package name]-devel often works, and I don't know if it exists or not, but for now, I type the following command. I'll try it.

sudo yum install ImageMagick-devel

It seems to be working fine, so I will install it.
There are many dependent packages, so it may take some time.

If 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 appears that the compilation was completed successfully and the installation was successful.
At the end, there is a message asking you to add "extension=imagick.so" to php.ini, so let's add it.

If you want to do it smartly, you can create an ini file with an appropriate name under /etc/php.d/ and put "extension=imagick.so" in it.

Just like GD, all you have to do is restart the web server.

 

Preparation to handle 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 it under 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 location is the library setting to be used on line 26.

    'driver' => 'gd',

By default, GD is selected, so if you want to use ImageMagick, change it to either 'imagemagick' or 'imagick'.

Next is the image quality settings.

    'quality' => 94,

By default, the image quality is 100%, which is the highest quality, and if you use JPEG, the file size after conversion will be several times larger.
The default value of GD is 75, so it is better to adjust it between 75 and 95.

After that, it's up to you.

In the initial state, once you execute the save method or output method, the image will be reloaded.
If for some reason you want to use the same image again (such as saving the output image),

	'persistence' => true,

If you set it as , it will not reload and seems to be good.

 

Let's try editing it

Images are loaded from the Fuel\Core\Image class.
This class selects drivers for GD, IMagick, and ImageMagick according to the contents of the config.

$image = Fuel\Core\Image::forge(null, '/path/to/image');

The image is now loaded.
When using GD, you had to select and use imagecreatefrom* functions yourself, but you can identify and define them within the class.
However, since it only differentiates based on extension, an error will occur when loading a file that does not have an extension.

In this case, use the load method instead of forging.

$image = Fuel\Core\Image::load('/path/to/image', false, 'jpg');

You can specify the extension with the third argument.
If you want to edit a file that does not have an extension, the only way to check it is in binary format, so you will need to be a little creative.
The second argument is valid only for GD, and I didn't really understand how to use it.

In this case, you cannot add or overwrite the config, so you need to create a configuration file in advance.

For various edits, the Image - Class - FuelPHP documentation is useful.
The methods here are common to all drivers, so you only need to understand them here.

The crop method is useful for cropping images

If you want to crop from the center, the crop_resize method is convenient.

The resize method is useful for resizing images

If you want to rotate an image, the rotate method is useful.
When using GD, if the background is visible at 30 degrees or 45 degrees, it seems that the transparent color cannot be set.

The flip class is useful when you want to flip an image (change the orientation vertically or horizontally as if it were reflected in a mirror)

The watermark method is useful if you want to add a watermark to an image

The border method is useful when you want to frame an image

The mask method is useful if you want to mask an image to the loaded image

The rounded method is useful
when converting to an image with rounded However, when using the method in combination with ImageMagick and a transparent image, there seems to be a problem that the transparent parts at the four corners are not transparent.
GD calculates color information using PHP.

If you want to know the image size, the sizes method is useful.
In this case, the size is the number of horizontal and vertical pixels of the image, not the file size.
To obtain the file size, use the standard function filesize.

If you want to convert an image to grayscale, the grayscale method is useful.
Imagick and ImageMagick use dedicated functions provided by ImageMagick, and GD extracts all pixels in a loop and calculates the color information of each pixel on PHP.

The output method is useful if you want to display an image in the browser by sending HTTP headers

If it's just a simple edit, I think the above will be sufficient.
It's especially useful because it's very easy to change the size.

That's it.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
2,240
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.