This component allows developers to manipulate image files. We can perform multiple operation on a single image file.
Adapters
Adapter are used to encapsulate specific image manipulator programs. The following image manipulator programs are supported:
Class | Description |
---|---|
Phalcon\Image\Adapter\Gd | Requires the GD PHP extension |
Phalcon\Image\Adapter\Imagick | Requires the ImageMagick PHP extension |
Implementation
<?php
use Phalcon\Image\Factory;
$options = [
'width' => 200,
'height' => 200,
'file' => 'upload/javatpoint.jpg',
'adapter' => 'imagick',
];
$image = Factory::load($options);
?>
Resizing Image
We can resize image with proper ratio maintenance by using different methods.
\Phalcon\Image::WIDTH
It is used to change the width of the image but keeps the ratio maintains. If we specify height then it is ignored.
<?php
$image = new \Phalcon\Image\Adapter\Gd('image.jpg');
$image->resize( 300, null, \Phalcon\Image::WIDTH
);
$image->save('resized-image.jpg');
?>
\Phalcon\Image::HEIGHT
It is used to change the height of the image but keeps the ratio maintains. If we specify width then it is ignored.
<?php
$image = new \Phalcon\Image\Adapter\Gd('image.jpg');
$image->resize(
null,
300,
\Phalcon\Image::HEIGHT
);
$image->save('resized-image.jpg');
?>
Cropping Image
It is used to crop the image by 200px*200px.
<?php
$image = new \Phalcon\Image\Adapter\Gd('image.jpg');
$width =200;
$height = 200;
$offsetX = (($image->getWidth() - $width) / 2);
$offsetY = (($image->getHeight() - $height) / 2);
$image->crop($width, $height, $offsetX, $offsetY);
$image->save('cropped-image.jpg');
?>
Rotating Image
It is used to rotate throughout 360 degree as per requirement.
<?php
$image = new \Phalcon\Image\Adapter\Gd('image.jpg');
// Rotate an image by 90 degrees clockwise
$image->rotate(90);
$image->save('rotated-image.jpg');
?>
Sharpening Image
The sharpen() method takes integer value between 0 (no effect) to 100 (very sharp).
<?php
$image = new \Phalcon\Image\Adapter\Gd('image.jpg');
$image->sharpen(50);
$image->save('sharpened-image.jpg');
?>
Blurring Image
<?php
$image = new \Phalcon\Image\Adapter\Gd('image.jpg');
$image->blur(50);
$image->save('blurred-image.jpg');
?>
Leave a Reply