PHP Graphics Processing _php Tutorial

Source: Internet
Author: User
Graphics processing

PHP graphics processing, the main function is focused on the PHP graphics processing functions.

Need to master some points first. What is called a picture, how to show the picture.

The so-called picture, is actually a kind of document, but the content is not directly visible to our naked eye. If we open a picture with Notepad, we will see a garbled piece. In fact, these garbled, just relative to me is garbled. It's not a mess for programs that can read and write to it. If we know the format of a picture, we can generate a picture ourselves. Just like we did when we first made a message book.

Save some special formatting data to a file, and you can create a picture. Conversely, if we use PHP to output the content directly, the browser will also think this is a picture.

Let's check this out first, students prepare a picture first. Suggest a smaller, JPG format is available. Then we use PHP to read this image, just like normal files.

$file = "1.jpg";

$fp = fopen ($file, "RB");

$data = Fread ($fp, FileSize ($file));

Fclose ($FP);

Echo $data;

?>

I believe there is nothing wrong with this code for the students.

Open the file, read the contents (all bytes), close the file, and output the content.

Have a classmate try, this code run will output what, cut a picture to look at.

Perhaps some classmates will be strange, how is garbled.

Because the browser defaults, think our PHP output is text. Even if it is garbled, we need to tell the browser that this is a picture. This needs to use the header function, send header information, tell the browser, the current output of the content, is the grid format.

Header ("Content-type:image/jpeg");

This is a JPG format image using the header information, content type: Picture/jpeg, need to be added before Echo.

$file = "1.jpg";

$fp = fopen ($file, "RB");

$data = Fread ($fp, FileSize ($file));

Fclose ($FP);

Header ("Content-type:image/jpeg");

Echo $data;

?>

Students try again.

This code proves a point: even if the output is garbled, as long as the browser knows what it is, it will be displayed correctly.

In contrast, if we save the read content to another file, we can copy the image.

Here, there may be some misunderstanding among the classmates. Think that this is still a Web page. Students can try, in the open page blank, click the right mouse button, view the source files, you will find that there is no option. Even if there is, it is gray. This is because, this is not a page, the browser has thought, we this PHP program, is a picture.

Usually when we use PHP for any kind of output, the browser thinks this is an HTML page to see the source code.

If we output the image content, to be able to display correctly, we must tell the browser, this output, do not treat me as a Web page, but as a picture. In other words, our PHP program is a picture. If you want to use this image on another page, you need to do it as usual.

This PHP program as a picture to treat, and not directly in this program to write.

"/>

This is wrong, and it only leads to a result.

It is impossible to show it.

Therefore, the use of PHP processing pictures of the program, must be a single function of the PHP program. Unless you don't export the picture, just save it.

$file = "1.jpg";

$fp = fopen ($file, "RB");

$data = Fread ($fp, FileSize ($file));

Fclose ($FP);

$fp = fopen ("2.jpg", "WB");

Fwrite ($fp, $data);

Fclose ($FP);

?>

If that's what the code says, it's another thing, no mistake. Save the picture to another place, and then output the HTML to call this image. To directly output the image of the PHP program, it must not contain any other output, no use. Only the content of the output picture, is that heap of garbled characters.

OK, read the picture, the output picture is no problem.

The content of the file is a special format, we can not read it, how to deal with it?

PHP provides specialized graphics processing functions.

Graphics processing function Library, there is a special name, called the GD Library.

This library is not PHP's own. You need to set up the load in php.ini when you install PHP.

The library file is named Php_gd2.dll. PHP is installed in the installation package, in the Ext directory. If a student's php.ini has not yet loaded the GD library, please open it now.

In this way, the graphics processing function is available in PHP.

The current version of the GD library that comes with PHP is 2.0.28. PHP 4 with the GD 2.0,php 3 era is GD 1.6.

Why do you want to mention this?

Because of GD 2.0, the copyright issue because of GIF pictures. PHP is a free, open source language and cannot provide copyright fees to GIF copyright owners, so you can only pause support for GIF images. After PHP 5, GIF copyright expires and our PHP supports GIF images again.

OK, let's Open the PHP manual and take a look at the image function.

Many functions, however, can be broadly divided into four categories: Create, paint, set, output.

If you want to create a picture, you can use the Imagecreate function. You can see it from the function name and create the image. The syntax format can be seen from the manual.

Returns the image resource Imagecreate (width, height)

The width and height are measured in pixels.

There is another function, Imagecreatetruecolor creates a new true color image that supports more colors.

The function that creates the class returns a picture resource, somewhat similar to the return of the fopen function. Resource-based data, which contains a read-write pointer inside, allows us to edit the image.

Let's try to create a picture first with the Imagecreate function.

In order to see this picture, we need to output. But our experience tells us that resource-based content cannot be exported directly.

The picture-shaped function gives us some functions, namely:

imagejpeg output in JPG format

Imagegif output in GIF format

Imagepng output in PNG format

The function format is the same.

Imagejpeg (Image resource, [Save path])

If you need to save this picture, just write the filename on the second parameter. If you just want the output to be direct, the second parameter is not written on the line.

Let's start by outputting the image directly.

Header ("Content-type:image/jpeg");

$img =imagecreate (100, 100);

Imagejpeg ($IMG);

Create an image of 100*100, and then output it in JPG format. Remember to tell the browser that this is the picture.

What is the result of the operation? One of the students cut a picture to see.

Yes, it will output a black picture. Because we didn't draw anything on it, nor did we tell it what color to use.

Let's start with a little bit of a simple operation and put some color on it first. This requires the use of the Imagecolorallocate function.

function function is: Assign color to an image

The function format is

Imagecolorallocate (image resource, red, green, blue)

Three-color, respectively, with 0 to 255 of the number is expressed. 0 is the darkest, 255 is the brightest. If you need white, three colors are 255, black is the tri-color 0, if only red, is 255 0 0.

Header ("Content-type:image/jpeg");

$img =imagecreate (100, 100);

Imagecolorallocate ($img, 255,0,0);

Imagejpeg ($IMG);

I'll just fill it up with pure red. Students can try their own, the red and green blue set some values, to see how the results. This color, need some knowledge. Students can also get this color directly in various drawing tools.

The red and green blue values in the lower right corner can be used directly. Many of the software has a similar palette, and it's easy to get a variety of color values.

Here's one thing to note.

The Imagecolorallocate function, when used only for the first time, fills the image with a background color and re-uses it without changing the background color.

Header ("Content-type:image/jpeg");

$img =imagecreate (100, 100);

Imagecolorallocate ($img, 255,0,0);

Imagecolorallocate ($img, 0, 0, 255);

Imagejpeg ($IMG);

This code does not output the expected blue. However, it does not indicate that the function is useless. The function is still valid, but the color is not used. We can use this color for other purposes, such as writing.

Let's try it and write some words on the image.

PHP gives us the function inside, there are two functions can be used to write on the image, respectively, imagestring and Imagestringup.

Imagestring is horizontal writing

Imagestringup is vertical writing

If I write with Imagestringup, I'll have to look at the word and twist my neck. Well, let's use imagestring first.

imagestring function prototypes

Imagestring (image resource, font, start x coordinate, start y coordinate, Word to write, color)

PHP comes with only 5 fonts, which need to be represented by numbers 1 through 5.

Let's try it out.

Header ("Content-type:image/jpeg");

$img =imagecreate (100, 100);

Imagecolorallocate ($img, 255,0,0);

$color = imagecolorallocate ($img, 0, 0, 255);

Imagestring ($img, 4, 0,0, ' abcdef ', $color);

Imagejpeg ($IMG);

Your uncle's, good dazzling, I change white bottom better.

Does any classmate try to write a Chinese? The results will be disappointing.

Because the PHP comes with the font, the weak explosion, and Ben can not display the normal Chinese. What do we do?

Custom Fonts.

The image function gives us another function that you can use to write with a custom font. Strictly speaking, "stooped". Need a font file with bitmap format, and to support Chinese, the most common is the TTF type of font. If you have done a graphic design of the classmate, to this must not be unfamiliar, did not do the classmate, also does not matter. We can dig a few out of our system and use it. System comes with the font file, in the C:\windows\fonts directory, we can find a support in the Chinese font. WIN system comes with fonts, most of which support Chinese, I pick a Microsoft Jas. Back to our PHP.

To use this font file for stooped, you need to use the Imagettftext function.

Imagettftext function prototypes

Imagettftext (image resource, font size, font orientation, start x coordinate, start y sit, font color, font file, Word to write)

My Niang oh, a lot of parameters. It is estimated that the students are also the first time to use so many parameters of the function bar.

Create a picture and set the white background

$img =imagecreate (100, 100);

Imagecolorallocate ($img, 255,255,255);

Prepare a color,

$color = imagecolorallocate ($img, 0, 0, 255);

Prepare a font file

$font = "Msyhbd.ttf";

Image resource, font size, font orientation, start x coordinate, start y sit, font color, font file, Word to write

Imagettftext ($img, 0, $color, $font, "Chinese support");

JPG format output image

Imagejpeg ($IMG);

This image file, if the path is not in the current directory, to tell PHP where.

For example $font = "./font/msyhbd.ttf";

or $font = "C:\\windows\\fonts\\msyhbd.ttf";

You have to get PHP to find this font file

Imagettftext ($img, +, $color, $font, "Chinese support");

The font direction is 180 degrees, and the result is this. In addition, the Chinese must be UTF8 encoded here. This is easy to do, convert PHP files to UTF8 encoding. Students who use GBK encoding can change the encoding for the font.

$text = Iconv ("GBK", "Utf-8", "Chinese support");

As long as there are enough fonts, we can let PHP output any style

$font = "FZKANGFW. TTF ";

$text = Iconv ("GBK", "Utf-8", "Chinese support");

Imagettftext ($img, 0, $color, $font, $text);

This effect, in conjunction with the predetermined variable $_server, can do an effect.

$_server[' REMOTE_ADDR ']

This server variable can be used to get the IP address of the caller. All kinds of pictures with IP display on the internet are made out of this. Just write a few words on the picture.

All right. And then we'll learn a little bit. Draw lines on top.

There are many drawing functions.

Imagedashedline-Draw a dashed line

Imagedestroy-Destroying an image

Imageellipse-Draw an ellipse

Imagefill-area Fill

Imagefilledarc-Draw an elliptical arc and fill

Imagefilledellipse-Draw an ellipse and fill

Imagefilledpolygon-Draw a polygon and fill

Imagefilledrectangle-Draw a rectangle and fill

Drawing line is imageline, other drawing functions, students can refer to the manual.

It seems that the principle is: in the image resources, with what color, from which coordinates start, draw what.

Imageline function prototypes

(image data, start x-coordinate, start y-coordinate, end x-coordinate, end y-coordinate, color)

In mathematics, we know that XY coordinates can determine a point on a plane, and two points can determine a segment.

Create a picture and set the white background

$img =imagecreate (300, 300);

Imagecolorallocate ($img, 200,200,200);

Prepare a color,

$color = imagecolorallocate ($img, 0, 0, 255);

Draw a line in this color

Imageline ($img, 0,150, 300,150, $color);

JPG format output image

Imagejpeg ($IMG);

My image is the size of the 300*300. From 0,150 is the leftmost, middle position, to the far right, middle position.

If you want to draw a grid, you can slowly calculate the coordinates and then repeat the painting.

for ($i =0; $i < 6; $i + +) {

$y = $i *50;

Imageline ($img, 0, $y, $y, $color);

}

A vertical line or something, not much to say.

There are other kinds of lines, shapes, you can follow the instructions of the Manual, self-practice.

Many times, our pictures are ready-made. All we have to do is zoom out and cut the size.

The picture is ready, we need to load the picture, and according to the different types of pictures, we need different functions to load.

Imagecreatefromjpeg Create an image from a JPG file

Imagecreatefromgif GIF

Imagecreatefrompng PNG

Imagecreatefromwbmp WBMP

IMAGECREATEFROMXBM XBM

What is the type of picture, and what function is used to load it. These functions are all part of creating a class function. Before, we used to write, draw lines, all belong to the painting class function. That way, it doesn't feel like a lot of functions. Many functions are actually duplicated, just for different types. All the operations are for the canvas and what to draw on the canvas.

In fact, PHP does not shrink a picture, it is just a way of thinking skills. The image copy function Imagecopymerge is used.

You can copy a part of the image to another image, and another function imagecopyresampled the same. Copies a portion of the image and copies it to another image. This means that two images are required for such operations. One is the original picture, and the other is the newly generated one.

The original, we can use the Imagefrom series function to load in. New diagram, we can create it ourselves. Then use the image copy function to copy from the original to the new diagram.

If you are copying only one of these areas, that is the cropping feature. If the copy is the entire image size, the new image size and the original size are different, that is, the image size is changed, that is, thumbnails, or enlarge the image.

$image = "1.jpg";

Read picture size

List ($width, $height) = getimagesize ($image);

Loading pictures

$bimg = Imagecreatefromjpeg ($image);

New picture, size is half of the original

$simg =imagecreatetruecolor ($width * 0.5, $height * 0.5);

In the original image, the whole of the original, reduced by half, copied over

Imagecopyresampled ($simg, $bimg, 0,0, 0,0, $width *0.5, $height *0.5, $width, $height);

JPG format output small image

Imagejpeg ($SIMG);

There should be a classmate found out, here is the use of Imagecreatetruecolor to create a new image. True color images, not imagecreate. Students can try, with imagecreate is what results.

Copy the image, I chose the imagecopyresampled function, copy and resize it. Similar functions also have imagecopyresized.

Imagecopyresized

Imagecopyresampled

Both of these functions are copied and resized in the image. Students can test their differences on their own. As there are too many parameters, I will help you make a comment here.

The new picture, the original,

New figure beginning X, new figure beginning Y,

Original beginning X, original Origin y,

New figure width, new figure height,

Original width, original height

Two a group, you can look together.

Loading pictures

$bimg = Imagecreatefromjpeg ($image);

New picture, size is half of the original

$simg =imagecreatetruecolor ($width * 0.5, $height * 0.5);

In the original image, the whole of the original, reduced by half, copied over

Imagecopyresampled ($simg, $bimg, 0,0, $width *0.5, $height *0.5, $width, $height, $width, $height);

The meaning of this code is

$simg, $bimg,//New image, original

0,0,//Start drawing from the 0*0 of the new figure

$width *0.5, $height *0.5,//sampling from the center point of the original

$width, $height,//new figure and original size.

$width, $height//To the end of the lower-right corner of the original

Since only half of the image is created, this code will cause the final image to show only the center point of the original. The contents of the lower right corner. That is, 1/4 of the original content, is the so-called cutting.

The parameters of these functions are too many, not too easy to see, the calculation of each sample coordinates, need to be more careful.

The principle of watermark is the same. Load two graphs to create two image objects. Then the watermark is copied to the original image. Surface. In fact, it's just a technique of thinking.

The graph function is here.

Let me summarize the main points of this lesson.

1. Be sure to create the image first, load create or create it yourself.

2, only in the image above painting, line, shape

3, the text can only use UTF8 code, to display Chinese need Chinese font

4, the image itself can not be directly adjusted, only in the process of duplication adjustment.

5, if you want to output the image directly, this program can not output other superfluous things

6, a direct output image of the program, to consider it as a picture to call.

Image functions. Divided into several categories:

Create class: Imagecreate, Imagecreatefromjpeg

Set class: Imagecolorallocate, GetImageSize

Drawing classes: imagecopyresampled, Imageline, Imagettftext

Output class: Imagejpeg, Imagepng

Where the functions of the drawing class are the most, and the function format is similar:

In the XX coordinates of the image, draw xx things

The function of the copy operation, the coordinates are often 8 of many.

The original coordinates, the original width is high, this is four;

New map starting point coordinates, new figure width is high, here again four;

Plus the original, new picture. There are 10 or so parameters.

Therefore, the general image processing process, often debug a program, are not willing to go back to re-change. Encapsulate it as a custom function.

such as the thumbnail function.

function size_img ($img, $width, $height) {

You can handle it in the middle.

}

This provides a picture, specifying the size of the output, to generate a thumbnail image. At least it's much easier to use.

http://www.bkjia.com/PHPjc/754899.html www.bkjia.com true http://www.bkjia.com/PHPjc/754899.html techarticle Graphics processing PHP Graphics processing , the main function is focused on PHP graphics processing functions. Need to master some points first. What is called a picture, how to show the picture. The so-called picture, actually also ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.