未加星标

Resize and Manipulate Images in PHP (With Examples)

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In my previous tutorial, we discussed basic image manipulation using thephp GD library . In that tutorial, I gave a brief introduction to the library and showed you how to load images from a file or create them from scratch in PHP. After that, we learned how to crop, rotate, scale and flip an image using GD. I covered the imagefilter() function to apply different filters to image resources loaded in the script. I also mentioned some useful functions in GD like imagesx() and imagesy() to get the width and height of the loaded image.

By the end of my last GD tutorial, you learned how to use the library to automate basic tasks like resizing all images in a directory or applying filters like grayscale on them before saving the final result. If you have never used the PHP GD library before, I would suggest that you go through that GD introductory article before reading this one.

In this tutorial, we will learn about many more useful functions in GD and how they can be used to automate more of our image manipulation tasks.

Manipulating Images Using a Convolution Matrix

Except for the pixels at the edges, every pixel in an image is surrounded by eight other pixels. Effects like blurring or edge detection are calculated for each pixel depending on that pixel's value and the values of the surrounding pixels. In edge detection, for example, a sharp change in color implies that we have reached the edge of some object in the image. For instance, a sudden change from white to brown in the image below will signify the boundary of the cup and the table.

An easy way to specify this kind of filter is with what is called a "convolution matrix". GD supplies the imageconvolution( $image, $matrix, $div, $offset) function to apply a 3x3 convolution matrix to an image resource $image .

The $matrix parameter is an array of three arrays, each of which contains three float values―i.e. it is a 3x3 matrix. The first element of the first array is multiplied by the color value of the top-left pixel. Similarly, the second element of the first array is multiplied by the color value of the pixel directly on top of the central pixel. The final color of the pixel is obtained by adding the result of all these multiplications and then dividing it by $div for normalization. Normalization generally keeps the final color value below 255.

As we've seen, the $div parameter is used as a divisor for the result of convolution to normalize its value. The $offset parameter, on the other hand, is used to specify an offset value for all colors. You will see how it affects the final result in the examples below.

Convolution Examples

Here is a list of some different convolution matrices that we have applied to the image of a cup on a table.

Box Blur $box_blur = array([1, 1, 1], [1, 1, 1], [1, 1, 1]);
imageconvolution($im_php, $box_blur, 9, 0);

Box blur works by just averaging each pixel with its neighbors. We set the value of the divisor to 9 because the sum of all elements in the three arrays is 9.

Sharpen $sharpen = array([0, -1, 0], [-1, 5, -1], [0, -1, 0]);
imageconvolution($im_php, $sharpen, 1, 0);

Sharpen works by exaggerating the differences between each pixel and its neighbors. This makes the edges a bit clearer. In the case of sharpen, the divisor is still 1 because the sum of all the elements in the three arrays is 1.

Emboss $emboss = array([-2, -1, 0], [-1, 1, 1], [0, 1, 2]);
imageconvolution($im_php, $emboss, 1, 0);

The emboss matrix is similar to the sharpen matrix, except that the values are negative to the upper left and positive to the lower right―that's what creates the emboss effect. The sum of all the elements in the case of the emboss convolution matrix is 1, so we don't have to worry about normalization or color offset.

Edge Detect $edge_detect = array([-1, -1, -1], [-1, 8, -1], [-1, -1, -1]);
imageconvolution($im_php, $edge_detect, 1, 0);
imageconvolution($im_php, $edge_detect, 1, 255);

Edge detection is similar to sharpen, but the effect is even stronger. Also, the original value of the image is given no more weight than the neighbors―that means we only care about the edges, not the original solid colored areas.

With edge detection, the sum of all the array elements is 0. This means that the image we will get will mostly be black unless there is a sharp change in color, which generally occurs at the edges of objects. The mostly black image can be turned to white by setting the offset parameter to 255.

The following image shows the result of all these convolution matrices.


Resize and Manipulate Images in PHP (With Examples)
Image Copy Functions

PHP GD has a lot of functions to copy part of an image and then resize or merge it. When using these functions, it is important to remember that PHP considers the top-left corner of an image resource as its origin. A positive x value will take you to the right of the image, and a positive y value will take you further down.

The simplest of these functions is imagecopy( $dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h) . It will copy the source image onto a destination image. The $dst_x and $dst_y parameters determine the top-left corner, where the copied image will be pasted. The $src_x , $src_y , $src_w , and $src_h parameters determine the rectangular portion of the source image, which will be copied to the destination.

You can use this function to crop images by creating an image from scratch using imagecreatetruecolor() and copying the crop rectangle of the source image into it. You can also use it to add watermarks on images, but you have to remember that with this method, the size of the watermark cannot be changed according to the size of our images.

One solution to this problem is to use the imagecopyresized( $dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) function. It accepts all the parameters of imagecopy() and two additional parameters to determine the size of the destination area where the source image will be copied.

The imagecopyresized() function is not perfect, as it does not scale the image up and down very well. However, you can get better quality resizing using the imagecopyresampled() function, which accepts all the same parameters.

Copy With Transparency

There are two more functions related to copying images which you will find very useful: imagecopymerge() and imagecopymergegray() .

The function imagecopymerge( $dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) is similar to imagecopy() , where the additional $pct parameter determines the transparency of the copied image. A value of 0 means no transparency, and a value of 100 means complete transparency. This will be of great help when you don't want to completely hide the content of the main image behind your watermark.

The imagecopymergegray() function, on the other hand, uses the last parameter to convert the source image to grayscale. If it's set to 0, the source image will lose all its color. If it's set to

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

tags: image,src,will,The,dst,value,im
分页:12
转载请注明
本文标题:Resize and Manipulate Images in PHP (With Examples)
本站链接:https://www.codesec.net/view/597046.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(11)