Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PHP Picture processing Library Grafika

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use Grafika in PHP image processing library". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Grafika is a PHP image processing library, is based on Imagick and GD, can be used to resize pictures, clip, compare, add watermarks and other functions. There are also perceptual hashing, advanced image filtering, drawing Bezier curves and other functions, which is very powerful.

Installation

download

1. Download directly:

The official website and Github address of Grafika

2 、 composer:

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

Environmental demand

PHP > = 5.3.Of course, php7 is officially recommended.

GD Library > = version 2.0

Imagick*** (not required) > = 3.3.0, ImageMagick > = 6.5.3

Deployment

The basic structure of the downloaded Grafika directory looks like this:

But composer downloads a little more, you just need to use something in the kosinix/grafika directory.

We create an index.php in the grafika directory, and the rest of the operation is here.

Grafika provides us with a very useful autoloader.php located in the src directory.

Introduce it in index.php (the following examples all need to introduce this autoloader.php file, which we omit by default), and the following can be developed directly.

Require_once 'src/autoloader.php'

Create Editors

1 、 createEditor

Grafika creates an editor through the static method createEditor. It contains all the image processing methods.

Because grafika is based on Imagick and GD libraries, using the createEditor method will automatically select the desired 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 the Imagick library directly.

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

Note: in some cases, this class library may not be supported. You need to check it with the following statement. (however, if you use method 1 directly, there is no such thing).

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 the GD library directly, and in some cases it may not be supported, remember to check

Use Grafika\ Gd\ Editor; / / Import package $editor = new Editor (); / / Gd editor if ($editor- > isAvailable ()) {/ / Safety check / / Your code here}

Create Ima

Grafika allows you to create an image to be processed in four ways

1. Open the image directly

Create an editor + open method

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

2. Use the static method to open the picture

Use to open and create pictures directly

Use Grafika\ Grafika; $image = Grafika::createImage ('path/to/image.jpg'); / / $editor = Grafika::createEditor () is omitted here

3. Create a blank canvas

Create a new canvas as a new image

Use Grafika\ Grafika; $image = Grafika::createBlankImage (100100)

4. Copy a picture from an existing picture

Copy an image for image processing

$copy = clone $image

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

The operations after these methods are more or less the same, and we only choose * conventional methods as examples.

Picture thumbnail

Let's prepare an original picture first.

Next, suppose we want to create a thumbnail length: 200px wide 200px

1 、 Resize Fit

Proportional scale type. Then make sure that the longer side of the picture does not exceed the 200px, scale proportionally, and do not fill the background after zooming.

Use Grafika\ Grafika; $editor = Grafika::createEditor (); $editor- > open ($image1, 'yanying.jpg'); / / Open yanying.jpg and store to $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 require of *.

2 、 Resize Exact

Fixed size scale type. That is, regardless of the aspect ratio of the picture, it all shrinks to 200px, which may cause the picture to deform.

Use Grafika\ Grafika; $editor = Grafika::createEditor (); $editor- > open ($image1, 'yanying.jpg'); / / Open yanying.jpg and store to $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')

3 、 Resize Fill

Cut in the middle. Is to zoom the shorter change to 200px, and then cut out the part of the long side larger than 200px in the middle, so that the image will not be deformed.

4 、 Resize Exact Width

Wait for width and scale. Similar to the function of * *, the final width is 200px, proportional scaling, regardless of height.

Use Grafika\ Grafika; $editor = Grafika::createEditor (); $editor- > open ($image1, 'yanying.jpg'); / / Open yanying.jpg and store to $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')

5 、 Resize Exact Height

Zoom out at the same height. The final height is 200px, proportional scaling, regardless of the width of the picture.

Image contrast function

1. Comparison of picture similarity

First of all, we prepare a basic picture to compare with other pictures. (segmentfault web page images may have been processed, and the results may be inconsistent if you use the pictures in this article directly)

1) We use a grayscale image to compare each other.

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

Description: grafika picture comparison method compare returns a number, in which if the number is closer to 0, then the picture is more similar. If the number is in the range of 0-10, then the pictures may all be similar. But if the number is greater than 10, then it may be completely different.

Return 2 here, indicating that the similarity is still very high.

2) Let's test it with a scaled-down image, keeping in mind that it is compared with the basic picture.

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

The result here returns 0, which is very similar.

3) Let's test it with a partial picture cut out.

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

The result is more than 10, and the similarity is not very high.

4) Let's test it with a completely different picture

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

As a result, 39, bigger and bigger, less and less like

2. Compare whether the pictures are the same.

Grafika provides the method equal to check whether the two images are exactly the same. The check here is a pixel-by-pixel detection, so the time may be longer.

Of course, grafika will also pre-check, if the two images are not the same size, it will directly return false. Pixel-by-pixel checking is performed only if everything else is the same.

Here we compare a thumbnail we created earlier. Because the size is not consistent, we return false directly.

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 image recognition, and it tends to retain the key parts.

But grafika also provides manual position tailoring, so let's talk about this first.

Basic position clipping

Basic position clipping consists of 9 positions

Top-left

Top-center

Top-right

Center-left

Center

Center-right

Bottom-left

Bottom-center

Bottom-right

We said it together here, and here we use a picture of 900 to 600, which is divided into nine pieces.

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 ($crop, 300,200, 'image) $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 ($free) $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)

Take a look at the results.

Intelligent tailoring

Original drawing

We use smart clipping to clip the picture to 200*200px

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

It is found that the key points can be highlighted.

GIF thumbnail

Compress GIF without losing animation

Grafika can compress GIF images directly without losing animation.

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

Here we compress the original image to half of it, and find that the animation is not lost.

Remove GIF animation effects

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

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

Picture merging

The combination of pictures requires two pictures, one of which is used as the basic picture, and the prepared second chapter picture is placed on top of the base picture.

Let's first look at 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 images, of which $image1 is the base image, that is, the one below. The focus is on the blend method.

Among them

* Image based on * * parameters

The second parameter is the image normal, multiply, and overlay or screen., placed on top of the base image. The type here means the mode of image overlay. An example is given below to see how each is different.

The third parameter is transparency, which is easy to think of without saying too much.

The fourth is location, there are 10 options, of which, the first nine are custom visit locations for users, and * one is intelligent visit, where grafika determines where to put it. 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, which represents the distance from picture 2 to the left of picture 1.

The sixth parameter is also optional, indicating the distance between picture 2 and picture 1.

Let's try to set up a few situations.

1 、 normal

Where the location information: center, transparency of 0.9, that is, the kind of code above

2 、 multiply

Location information: top-left, everything else remains the same

3 、 overlay

Location information: bottom-right, other unchanged

4 、 screen

Location information:, * A location parameter is not given, that is, the default top-left

Image rotation

Image rotation is relatively simple, only need to give a rotation angle parameter, if you want to fill the background with a color, and then give a color parameter. (the background color is not 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 years new Color ('# ff0000')); $editor- > save ($image,'333/yanying-rotate.jpg')

* * Color object is also required for a background color parameter

Write words in pictures

There are many parameters to write text on the picture, but if you use it normally, you only need to give the first two required parameters, and the latter parameters are optional.

Let's look at the parameters one by one.

Image: the picture of the text you need to write

Text: words that need to be written

Size: (optional) font size. Default is 12px.

X: (optional) the distance between the leftmost of the text and the leftmost of the picture. The default is 0.

Y: (optional) the distance from the baseline of the text to the top of the picture. The default is 12px, that is, the height of the text. (you can take the baseline as the bottom of the text.)

Color: (optional) Font color, Color object, need to new Color a bit, default is black.

Font: (optional) the full path of the font. Default is Sans font.

Angle: (optional) text rotation angle. Value range: 0-359. Default is 0, that is, no rotation.

Let's try to find a random text.

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

Let's see how it works. As explained here, if the text is Chinese, you need to find a font that supports Chinese. The default font does not support Chinese, so when you write Chinese, it is all in small boxes.

This is the end of the content of "how to use Grafika in PHP Photo processing Library". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How to realize the opening and moving of pages by JavaScript

    Editor to share with you how JavaScript can open and move the page. I hope you will get something after reading this article. Let's discuss it together. The opening and moving of the page adds the following code to the area

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report