Grafika is a PHP image processing library, which is based on Imagick and GD. It can be used to change the image size, clip, compare, add watermarking and so on. There are also perceptual hashing, advanced image filtering, drawing Bessel curve and other functions, which can be described as very powerful.

Because there are too many functions, it is divided into several articles to write.

“1. Basic Image Processing”
2. Image Special Effect Processing Module
3. Image Attribute Processing
4. Graphic Drawing

Advantage:

  • Thumbnails are very fast and of very high quality.
  • Support intelligent tailoring
  • Good support for GIF pictures
  • Five thumbnail patterns
  • Image Contrast Function
  • Advanced Image Filtering Function
  • Image Mixing
  • APIs supported by other image processing libraries are basically supported

install

download

1. Direct download:

Grafika’s official website, Github address

2、composer

    composer require kosinix/grafika:dev-master --prefer-dist

Environmental needs

  • PHP >= 5.3, of course, php7 is officially recommended
  • GD Library >= Version 2.0
  • ImagickBest (not required) >= 3.3.0,ImageMagick >= 6.5.3

deploy

DownloadedGrafikaThe basic structure of the directory is as follows:

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

HowevercomposerDownload a little more, you just need to usekosinix/grafikaJust the things in the catalogue.

We aregrafikaCreate a directoryindex.phpAll the subsequent operations are here.

grafikaIt provides us with a very useful one.autoloader.phpBe locatedsrcUnder the catalogue.

stayindex.phpIntroduce it.To illustrate, the following examples need to introduce thisautoloader.phpFile, we omit by defaultThe following can be developed directly.

require_once 'src/autoloader.php';

Create Editors

1、createEditor

grafikaThrough static methodcreateEditorTo create aeditor. It contains all the image processing methods.

Because,grafikaBased onImagickandGDLibrary, so usecreateEditorThe method will automatically select the required image processing library according to the current situation. (Recommended)

use Grafika\Grafika; // Import package
$editor = Grafika::createEditor(); // Create the best available editor

2、Imagick Editor

Of course, you can also use it directly.ImagickClass library.

use Grafika\Imagick\Editor; // Import package
$editor = new Editor(); // Imagick editor

Note: Some situations may not support this kind of library, you need to use the following statement to check it. (However, you’d better use method 1 directly, there are no such things.)

use Grafika\Imagick\Editor; // Import package
$editor = new Editor(); // Imagick editor
if( $editor->isAvailable() ) { // Safety check

    // Your code here

}

3、GD Editor

You can also use it directly.GDLibraries, there are some situations that may not support, remember to check

use Grafika\Gd\Editor; // Import package
$editor = new Editor(); // Gd editor
if( $editor->isAvailable() ) { // Safety check

    // Your code here

}

Create images

grafikaAllow you to create an image to be processed in four ways

1. Open the image directly

Establisheditor + openMethod

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'path/to/image.jpg');

2. Opening Pictures Statically

Use Direct Opening to Create Pictures

use Grafika\Grafika;
$image = Grafika::createImage('path/to/image.jpg');

// Here, $editor = Grafika:: createEditor () is omitted.

3. Create a blank canvas

Create a new canvas as a new image

use Grafika\Grafika;
$image = Grafika::createBlankImage(100,100);

4. Copy one from an existing picture

Copy an image for image processing

$copy = clone $image;

In this way, you have to make sure that you have a picture before.

After these methods, the operation is much the same. We only choose the first common method as an illustrative example.

Picture thumbnails

Let’s prepare an original picture first.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

Next, let’s assume that the thumbnail we want to create is 200 PX long and 200 PX wide.

1、Resize Fit

Equal scaling type. Then make sure that the longer side of the picture does not exceed 200 px, equal to the scale.No background filling after zooming

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor - > open ($image1,'yanying. jpg'); // open yanying. JPG and store it in $image1
$editor->resizeFit($image1 , 200 , 200);
$editor->save($image1 , 'yanying1.jpg');

$editor - > open ($image2,'yanying-h.jpg'); //open yanying.jpg and store it in $image2
$editor->resizeFit($image2 , 200 , 200);
$editor->save($image2 , 'yanying2.jpg');

Of course, don’t forget the first line.require

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic ProcessingPHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

2、Resize Exact

Fixed size zoom type. That is, no matter the aspect ratio of the picture, it will be reduced to 200 px, which may cause the picture to be deformed.

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor - > open ($image1,'yanying. jpg'); // open yanying. JPG and store it in $image1
$editor->resizeExact($image1 , 200 , 200);
$editor->save($image1 , 'yanying1.jpg');

$editor - > open ($image2,'yanying-h.jpg'); //open yanying.jpg and store it in $image2
$editor->resizeExact($image2 , 200 , 200);
$editor->save($image2 , 'yanying2.jpg');

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic ProcessingPHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

3、Resize Fill

Cut in the middle. It’s to zoom the shorter ones to 200px, and then cut the long ones larger than 200px in the middle, so that the picture won’t be deformed.

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor - > open ($image1,'yanying. jpg'); // open yanying. JPG and store it in $image1
$editor->resizeFill($image1 , 200,200);
$editor->save($image1 , 'yanying1.jpg');

$editor - > open ($image2,'yanying-h.jpg'); //open yanying.jpg and store it in $image2
$editor->resizeFill($image2 , 200,200);
$editor->save($image2 , 'yanying2.jpg');

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic ProcessingPHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

4、Resize Exact Width

Equal width zoom. Similar to the first function, it is eventually 200 PX wide and scaled at the same ratio regardless of height.

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor - > open ($image1,'yanying. jpg'); // open yanying. JPG and store it in $image1
$editor->resizeExactWidth($image1 , 200);
$editor->save($image1 , 'yanying1.jpg');

$editor - > open ($image2,'yanying-h.jpg'); //open yanying.jpg and store it in $image2
$editor->resizeExactWidth($image2 , 200);
$editor->save($image2 , 'yanying2.jpg');

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

5、Resize Exact Height

Constant height zoom. The final height is 200 px, equal to the scale, regardless of the width of the picture.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic ProcessingPHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

Image Contrast Function

1. Image similarity comparison

First, we prepare a basic picture to compare with other pictures. (segmentfault page images may have been processed, and direct use of the images in this article may result in inconsistent results)

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

1. For the first time, we used a gray scale image to compare.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

use Grafika\Grafika;
$editor = Grafika::createEditor();
$result = $editor->compare('yanying.jpg' , 'yanying_grey.jpg');
var_dump($result); // int 2

Explain:Grafika Picture Contrast MethodcompareReturns a number in which the closer the number is to 0, the more similar the picture is. If the number is in the range of 0-10, the pictures may be similar. But if the number is greater than 10, then it may be completely different.

Return to 2 here to show that the similarity is still very high.

2. Let’s use a smaller picture to test, and remember that it’s all compared with the first basic picture.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

use Grafika\Grafika;
$editor = Grafika::createEditor();
$result = $editor->compare('yanying.jpg' , 'yanying-smaller.jpg');
var_dump($result); // int 0

Here the result returns 0, and the similarity is very high.

3. Let’s test with a clipped local picture.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

use Grafika\Grafika;
$editor = Grafika::createEditor();
$result = $editor->compare('yanying.jpg' , 'yanying-half.jpg');
var_dump($result); // int 20

The results exceeded 10 and the similarity was not very high.

4. Let’s test it with a completely different picture.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

use Grafika\Grafika;
$editor = Grafika::createEditor();
$result = $editor->compare('yanying.jpg' , 'yanying-h.jpg');
var_dump($result); // int 39

As a result, 39 is getting bigger and bigger, less and less alike.

2. Compare pictures to see if they are the same

Grafika provides methodsequalTo check if the two pictures are identical. The check here is a pixel-by-pixel detection, so the time may be longer.

Of course, grafika also prechecks and returns directly if the two images are of different sizesfalse. Pixel-by-pixel checks are performed only if all the others are the same.

Let’s compare a thumbnail created before, because the size is not the same, so go back directly.false

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

use Grafika\Grafika;
$editor = Grafika::createEditor();
$result = $editor->equal('yanying.jpg' , 'yanying-smaller.jpg');
var_dump($result); // boolean false

Intelligent tailoring

Intelligent clipping is an important part of automatic recognition image, which tends to retain the key part.

But grafika also provides human-controlled position tailoring, let’s start with that.

Basic position tailoring

Basic position tailoring consists of nine positions

  • top-left
  • top-center
  • top-right
  • center-left
  • center
  • center-right
  • bottom-left
  • bottom-center
  • bottom-right

Let’s talk about it here. Here we use 900 * 600 pictures and divide them into nine pieces.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

use Grafika\Grafika;
$editor = Grafika::createEditor();

$src = 'yanying.jpg';
$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'top-left' );
$editor->save( $image, 'result1.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'top-center' );
$editor->save( $image, 'result2.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'top-right' );
$editor->save( $image, 'result3.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'center-left' );
$editor->save( $image, 'result4.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'center' );
$editor->save( $image, 'result5.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'center-right' );
$editor->save( $image, 'result6.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'bottom-left' );
$editor->save( $image, 'result7.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'bottom-center' );
$editor->save( $image, 'result8.jpg' );
$editor->free( $image );

$editor->open( $image, $src );
$editor->crop( $image, 300, 200, 'bottom-right' );
$editor->save( $image, 'result9.jpg' );
$editor->free( $image );

Look at the results.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

Intelligent tailoring

Original graph

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

We use intelligent clipping to clip pictures to 200*200 PX

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'yanying-smaller.jpg' );
$editor->crop( $image, 200, 200, 'smart' );
$editor->save( $image, 'yanying-smart.jpg' );

Discoveries can still highlight key points

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

GIF thumbnails

Compress GIF without losing animation

Grafika can compress GIF images directly without losing animation.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'sample.gif' );
$editor->resizeFit( $image, 250, 128 );
$editor->save( $image, 'output.gif' );

We compressed the original image to half of the original one and found that the animation was not lost.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

Remove GIF animation effects

Of course, if necessary, we can also directly remove GIF animation effects.

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'sample.gif' );
$editor->flatten( $image );
$editor->save( $image, 'output-no-animation.gif' );

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

Picture merging

Picture merging requires two pictures, one of which serves as the basic picture. The second chapter of the preparation of the picture is placed on the basic picture.

Let’s start with the code.

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open($image1 , 'yanying-h.jpg');
$editor->open($image2 , 'yanying-smaller.jpg');
$editor->blend ( $image1, $image2 , 'normal', 0.9, 'center');
$editor->save($image1,'333/yanying-blend.jpg');

Explain

First, open two pictures, where$image1Based on the picture, that is, put it below. The focus is onblendThis method.

among

  • The first parameter is the base picture.
  • The second parameter is the picture placed above the base picture.normal, multiply, overlay or screen.The type here means the pattern of image superposition. Here are some examples to show the difference between each.
  • The third parameter is transparency, which is easy to think of without saying too much.
  • The fourth is location, with 10 choices. The first nine are user-defined visiting locations, and the last one is intelligent visiting, which is judged by grafika. top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart
  • The fifth parameter is an optional parameter, indicating the distance between picture 2 and the left of picture 1.
  • The sixth parameter is also an optional parameter, indicating the distance between picture 2 and the top of picture 1.

Let’s try to put it in several situations.

1、normal

Location information: center, transparency is 0.9, which is the kind of code above.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

2、multiply

Location information: top-left, other unchanged

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

3、overlay

Location information: bottom-right, other unchanged

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

4、screen

Location information: The last location parameter is not given, which is the default.top-left

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

Image rotation

Image rotation is relatively simple, just give a rotation angle parameter, if you want to fill the background with a color, then give a color parameter. (No background color is black by default)

The code is as follows

use Grafika\Grafika;
use Grafika\Color;
$editor = Grafika::createEditor();
$editor->open($image , 'yanying-smaller.jpg');
$editor->rotate($image ,'45',new Color('#ff0000'));
$editor->save($image,'333/yanying-rotate.jpg');

The last background color parameter also requires a color object

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing

Picture Writing

There are many parameters to write text on the picture, but if it is used normally, only the first two must be filled in. The latter parameters are optional.

Let’s look at the parameters one by one.

  • imagePictures needed to write text
  • textWords to be written
  • sizeFont size, default12px
  • x(Selected) The Leftmost Distance of Text from the Leftmost Distance of Picture by Default0
  • y(optional) The distance from the baseline of the text to the top of the picture by default is12pxThat is, the height of the text. (Baseline is the bottom of the text.)
  • color(optional) font color,ColorObjects, needsnew ColorOnce, the default is black.
  • font(Selected) Full path of font, defaultSans font.
  • angle(optional filling) text rotation angle, range of values0-359By default0That is, no rotation.

Let’s try to find a text at random.

use Grafika\Grafika;
use Grafika\Color;
$editor = Grafika::createEditor();
$editor->open($image , 'yanying-smaller.jpg');
$editor->text($image ,'yanying',30,200,100,new Color("#000000"),'',45);
$editor->save($image,'333/yanying-text.jpg');

Look at the effect. Here, if the text is Chinese, you need to find a font that supports Chinese. The default font does not support Chinese, so you write Chinese, that is, small boxes.

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing


Yan Ying, PHP R&D Engineer

Night of 2016-11-07

Blog: Segmentfault Homepage

Recommend a Web Site Navigation for Developers developed by our team: Pen Point Navigation – Make the most concise Web Site Navigation with your heart

You can customize the web site
Classification can be customized
Classification can mark colors
Custom Skin
Custom Search
Drag-and-drop sorting of Web sites
Custom plug-in module

PHP's Extremely Powerful Image Processing Library Grafika Detailed Tutorial (1): Image Basic Processing