PHP Image Function Large example (not original) _php tips

Source: Internet
Author: User
Tags 0xc0 image identifier imagejpeg rand random seed
The following approach is one way:
if (!function_exists (' imagecreate ')) {
Die (' This server does not support GD module ');
}
If not, how do I configure it? Download the GD module DLL file, modify the php.ini, restart the server.
the following is referred to as PS PHP mapping.
When you intend to PS, you should complete the following steps, which are required.
1: Create the basic PS object (I assumed to be $image), fill the background (the default black), all future PS operations are based on this background image.
2: Drawing on the $image.
3: Output this image.
4: Destroy the object, clear the use of memory.
First, we'll get to know a couple of commonly used functions, which are described in detail in the PHP manual, which is generally referenced here.
Resource imagecreate (int x_size, int y_size)
Imagecreate () returns an image identifier that represents a blank image with a size of x_size and y_size.
This function is basically the same as Imagetruecolor ($width, $height).
---------------------------------------------------------
int Imagecolorallocate (resource image, int red, int green, int blue)
Imagecolorallocate () returns an identifier that represents a color composed of the given RGB component. The image parameter is the return value of the Imagecreatetruecolor () function. Red,green and Blue are the red, green, and blue components of the desired color. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF. Imagecolorallocate () must be called to create each color in the image that is represented by images.
---------------------------------------------------------
BOOL Imagefill (resource image, int x, int y, int color)
Imagefill () performs a region fill with a color color in the coordinate x,y of the image image (0, 0) in the upper-left corner (that is, the same color as the x, Y point, and adjacent points are filled).
---------------------------------------------------------
BOOL Imageline (resource image, int x1, int y1, int x2, int y2, int color)
Imageline () Draws a line segment from the coordinate x1,y1 to the x2,y2 (0, 0) in image images in color color.
---------------------------------------------------------
BOOL Imagestring (resource image, int font, int x, int y, string s, int col)
Imagestring () uses the Col color to draw the string s to the x,y coordinates of the image it represents (this is the upper-left coordinate of the string and the upper-left corner of the image is 0,0). If font is 1,2,3,4 or 5, the built-in font is used.
---------------------------------------------------------
Array Imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text)
This function is more important, more parameters, no longer listed here, it is mainly written to the image, and the above function similar, but the former strong.
---------------------------------------------------------
BOOL Imagefilltoborder (resource image, int x, int y, int border, int color)
Imagefilltoborder () starts the area fill with a color color from the x,y (0, 0) point in the upper-left corner of the image until it touches the boundary of the border. "Note: All colors within the boundary will be filled." If the specified boundary color is the same as the point color, there is no padding. If the boundary color is not in the image, the entire image is populated. 】
------------------------------------------------
BOOL Imagefilledellipse (resource image, int cx, int cy, int w, int h, int color)
Imagefilledellipse () draws an ellipse at the center of the image represented by Cx,cy (0, 0 in the upper-left corner of the image). W and H Specify the width and height of the ellipse respectively. The ellipse is filled with a color color. Returns TRUE if successful, and returns FALSE if it fails.
=================================================
Output image data: Imagepng ($image [, $filename])
======================================================
Example one: Output blue background and crossed white line graphics
<?php
$width = 35;
$height = 35;
Creating objects
$image =imagecreate ($width, $height);
Extract Color
$color _white=imagecolorallocate ($image, 255,255,255);//White
$color _blue=imagecolorallocate ($image, 0,0,108);//Blue
Imagefill ($image, 0,0, $color _blue);
Drawing
Line width
Imagesetthickness ($image, 3);
Imageline ($image, 0,0, $width, $height, $color _white);
Imageline ($image, $width, 0,0, $height, $color _white);
Send object to Header
Header (' content-type:image/png ');
Imagepng ($image);
/*
Send object to File
$filename = "Ex1.png";
Imagepng ($image, $filename);
*/
Destroying objects
Imagedestroy ($image);
?>
Output Image:
Online Demo: http://www.phzzy.org/temp/5do8/ex1.php
Example two: Yin and Yang Map
<?php
$width = 400;
$height = 400;
$image =imagecreatetruecolor ($width, $height);
Extract Color
$color _black=imagecolorallocate ($image, 0,2,0);
$color _white=imagecolorallocate ($image, 255,255,255);//White
$color _blue=imagecolorallocate ($image, 0,0,108);//Blue
$color _red=imagecolorallocate ($image, 151,0,4);//Red
$color _my=imagecolorallocate ($image, 192,192,255);//Background
$color _temp=imagecolorallocate ($image, 199,199,199);//Background
Drawing
Imagefill ($image, 0,0, $color _white);
The first is the big circle.
Imagefilledarc ($image, $width/2, $height/2, $height, $height, 0,360, $color _blue,img_arc_pie);
Two Little Circles
Imagefilledellipse ($image, $width/2, $height/4, $height/2, $height/2, $color _red);
Imagefilledellipse ($image, $width/2, $height/4 * 3, $height/2, $height/2, $color _blue);
/*imagefilledellipse--Draw an oval and fill it
Imagefilledarc ($image, $width/2, $height/2, $height, $height, -90,90, $color _red,img_arc_pie);
Imagefilledellipse ($image, $width/2, $height/4 * 3, $height/2, $height/2, $color _blue);
Send object to Header
Header (' content-type:image/png ');
Imagepng ($image);
/*
Send object to File
$filename = "Ex1.png";
Imagepng ($image, $filename);
*/
Destroying objects
Imagedestroy ($image);
?>
Demonstrate:
http://www.phzzy.org/temp/5do8/ex2.php
Example three: 3D image--cool
<?php
$width = 400;
$height = 400;
$image = Imagecreatetruecolor ($width, $height);
$white = Imagecolorallocate ($image, 0xFF, 0xFF, 0xFF);
$gray = Imagecolorallocate ($image, 0xc0, 0xc0, 0xc0);
$darkgray = Imagecolorallocate ($image, 0x90, 0x90, 0x90);
$navy = Imagecolorallocate ($image, 0x00, 0x00, 0x80);
$darknavy = Imagecolorallocate ($image, 0x00, 0x00, 0x50);
$red = Imagecolorallocate ($image, 0xFF, 0x00, 0x00);
$darkred = Imagecolorallocate ($image, 0x90, 0x00, 0x00);
Imagefill ($image, 0,0, $white);
Make the 3D effect
for ($i = $height/2 +20; $i > $height/2; $i-) {
Imagefilledarc ($image, $width/2, $i, $width/2, $height/2, 0,, $darknavy, Img_arc_pie);
Imagefilledarc ($image, $width/2, $i, $width/2, $height/2,, $darkgray, Img_arc_pie);
Imagefilledarc ($image, $width/2, $i, $width/2, $height/2, 360, $darkred, Img_arc_pie);
}
Imagefilledarc ($image, $width/2, $height/2, $width/2, $height/2, 0,, $navy, Img_arc_pie);
Imagefilledarc ($image, $width/2, $height/2, $width/2, $height/2, $, $gray, Img_arc_pie);
Imagefilledarc ($image, $width/2, $height/2, $width/2, $height/2, 360, $red, Img_arc_pie);
Flush image
Header (' content-type:image/png ');
Imagepng ($image);
Imagedestroy ($image);
/*
Send object to File
$filename = "Ex1.png";
Imagepng ($image, $filename);
*/
?>
Output:
Demo: http://www.phzzy.org/temp/5do8/ex3.php

Example four: Simple verification Code
PHP to create a CAPTCHA is very easy, easy to die, the simple idea is this:
Random seed generation, extraction of random characters, connected to print to graphics, output. In order to prevent color blindness, you can randomly extract colors, you can also customize the color, see below:
<?php
Session_Start ();
$width = 65;
$height = 20;
$sourcestrings = "0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$image =imagecreate ($width, $height);
$colorarrs =array (
Imagecolorallocate ($image, 255,255,255),//white
Imagecolorallocate ($image, 0, 0, 0)//black
);
Unset ($sessionval);
Imagesetthickness ($image, 3);
Get the number of strings randomly
$strsize =rand (3,5);
Imagefill ($image, 0,0, $colorarrs [0]);
Write a string to the picture
for ($i =0; $i < $strsize; $i + +) {
$i _temp=rand (1,62);
$sessionval. = $sourcestrings [$i _temp];
$fontcolor =imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
$y _i = $height/2 + $font _SIZE/3;
Imagechar ($image, 5, 1+ $i * $width/$strsize, 5, $sourcestrings [$i _temp], $fontcolor);
}
Write session, later validated with
unset ($_session[' Cjjer '));
$_session[' cjjer ' = $sessionval;
Add Miscellaneous points
for ($i =0; $i < $width/$height *2; $i + +)
{$i _x=rand (0, $width);
$i _y=rand (0, $height);
$pixelcolor =imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($image, $i _x, $i _y, $pixelcolor);
}
Header (' content-type:image/png ');
Imagepng ($image);
Imagedestroy ($image);
?>
Generated style Demo:
Online Demo: http://www.phzzy.org/temp/5do8/ex4_login.php
There is a very obvious problem is that the resulting picture is not gorgeous, so many users do not look clear, so we set ourselves a few more gorgeous colors and then output, extended Colorarrs array:
$colorarrs =array (
Imagecolorallocate ($image, 255,255,255),
Imagecolorallocate ($image, 0,0,0),
Imagecolorallocate ($image, 0,70,0),
Imagecolorallocate ($image, 92,0,12),
Imagecolorallocate ($image, 0,0,128),
Imagecolorallocate ($image, 233,10,216)
);
Then change the 23 lines (17 lines):
$fontcolor = $colorarrs [Rand (1,count ($COLORARRS)-1)];
Output:
Online Demo: http://www.phzzy.org/temp/5do8/ex5_login.php
Example five: Big point comparison cool verification code
PS image is relatively small, sometimes for some reason (personal site in order to play cool,just me, the commercial site play style, attract users, just Google, something), Verification code is not limited to more than 10 px, sometimes completely 200 more, no problem, this time, One scenario is to force the small graphs that are generated earlier to be large, can you? Yes, but it doesn't look smooth enough, it is true, obviously, broadband is no longer the most important issue, not to mention the other, the following demo a few more good-looking way to build:
<?php
Session_Start ();
$width = 600;
$height = 100;
if ($height < $width/6)
$height = $width/4;
$sourcestrings = "0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
Creating objects
$image =imagecreate ($width, $height);
$white =imagecolorallocate ($image, 255,255,255);
Imagefill ($image, 0,0, $white);
Load Font Library
$fonts = DirName (__file__);
Putenv (' gdfontpath= '. $fonts =. "");
$fontname = ' Arial ';
$font _size=floor ($height/2);
Get string
Unset ($sessionval);
$strsize =rand (5,8);
for ($i =0; $i < $strsize; $i + +) {
$i _temp=rand (1,62);
$sessionval. = $sourcestrings [$i _temp];
$x _i = $font _size + $i * $width/($strsize + 1);
$y _i = $height/2;
$angle _i=rand (-120,120);
$fontcolor _a=imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
Imagettftext ($image, $font _size, $angle _i, $x _i, $y _i, $fontcolor _a, $fontname, $sourcestrings [$i _temp]);
}
unset ($_session[' Cjjer '));
$_session[' cjjer ' = $sessionval;
Number of miscellaneous points
For ($i =0 $i < $width * $height/100; $i + +)
{
$i _x=rand (0, $width);
$i _y=rand (0, $height);
Imagesetpixel ($image, $i _x, $i _y,imagecolorallocate ($image, Rand (0,255), Rand (0,2550), Rand (0,255)));
}
Send Object
Header (' content-type:image/png ');
Imagepng ($image);
Imagedestroy ($image);
?>

Online test: http://www.phzzy.org/temp/5do8/ex6_login.php
Explanatory Note:
First is wide and high, Gao Tai small print can not see clearly. Randomly extracted characters or a few:
$sourcestrings = "0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
To create an object and fill it with white:
$image =imagecreate ($width, $height);
$white =imagecolorallocate ($image, 255,255,255);
Imagefill ($image, 0,0, $white);
Then load the font you want to verify the code in:
$fonts = dirname (__file__)//Return the current root directory, font file copied here, font file is *.ttf file
Putenv (' gdfontpath= '. $fonts =. "");
$fontname = ' Arial ';
Define the height of the character, where I set it to a height of half:
$font _size=floor ($height/2);
Clear variables, randomly set the number of characters to be generated:
Unset ($sessionval);
$strsize =rand (5,8);
Loop, each of the characters to play up:
Get the string for this loop., and add to the variable after a moment write session
$i _temp=rand (1,62);
$sessionval. = $sourcestrings [$i _temp];
Gets the position of the string to which the image is written (x and y coordinates)
$x _i = $font _size + $i * $width/($strsize + 1);
$y _i = $height/2;
Set the tilt, which is seen from the front,.
$angle _i=rand (-120,120);
Randomly generated colors,
$fontcolor _a=imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
Write to the image;
Imagettftext ($image, $font _size, $angle _i, $x _i, $y _i, $fontcolor _a, $fontname, $sourcestrings [$i _temp]);
If you have questions about this function, please refer to the relevant information. Very easy.
Write to session, while the registration code is used:
unset ($_session[' Cjjer '));
$_session[' cjjer ' = $sessionval;
Add Miscellaneous points:
Number of miscellaneous points
For ($i =0 $i < $width * $height/100; $i + +)
{
$i _x=rand (0, $width);
$i _y=rand (0, $height);
Imagesetpixel ($image, $i _x, $i _y,imagecolorallocate ($image, Rand (0,255), Rand (0,2550), Rand (0,255)));
}
Output head:
Header (' content-type:image/png ');//This line indicates a PNG image, which can be output by default. But not the header format of the image
Imagepng ($image);
Imagedestroy ($image);
You can load your own font library, set the rotation angle $angle_i=rand (-120,120), set the font height $font_size=floor ($height/2), the font color $fontcolor_a and the number of random numbers: $strsize = Rand (5,8);

Example six: Watermark the picture and generate an indented graph
Traditional ASP page watermark and generate indented graph is more cumbersome, generally use other components or something, but, PHP can easily do these things, as you expect, less than 30 lines of the program to take care of all this, please see this source program:
<?php
$source = "My.jpg";
$zoom = 0.5;
$str = ' I am handsome, are you mm? '
$image =imagecreatefromjpeg ($source);
$width =imagesx ($image);
$height =imagesy ($image);
$color _red=imagecolorallocate ($image, 111,0,0);//Red
$font =dirname (__file__). "//SIMSUN.TTC";
$str =iconv (' GB2312 ', ' UTF-8 ', $str);
$fontsize = 30;
$angle = 25;
$FROMX = $width/5;
$fromy = $height/2;
Imagettftext ($image, $fontsize, $angle, $fromx, $fromy, $color _red, $font, $STR);
$width _temp=imagesx ($image) * $zoom;
$height _temp=imagesy ($image) * $zoom;
$img =imagecreatetruecolor ($width _temp, $height _temp);
Imagecopyresized ($img, $image, 0,0,0,0, $width _temp, $height _temp, $width, $height);
Imagedestroy ($image);
$file _zoomname= "My_zoom_jpeg.jpg";
Imagejpeg ($img, $file _zoomname);
Imagedestroy ($IMG);
?>
Original Picture:
Generated JPG pictures:
Original picture 70K, here Next, if generated GIF, file 18k more, and PNG to use to 76K,SO we generate LIETU in JPEG format.
Code Analysis:
Here I set a few parameters first:
$source = "My.jpg"; SOURCE picture
$zoom = 0.5; Zoom percent
$str = ' I am handsome, are you mm? ' The text of the watermark
Load source diagram (do not load watermark):
$image =imagecreatefromjpeg ($source);
To get a long, wide size:
$width =imagesx ($image);
$height =imagesy ($image);
Set watermark font, because we use Chinese, we must import Chinese font library, otherwise write or garbled, and then must convert string encoding
$font =dirname (__file__). "//SIMSUN.TTC";
$str =iconv (' GB2312 ', ' UTF-8 ', $str);
Set start point, font size, angle of view:, writing string:
$fontsize = 30;
$angle = 25;
$FROMX = $width/5;
$fromy = $height/2;
Imagettftext ($image, $fontsize, $angle, $fromx, $fromy, $color _red, $font, $STR);
Objects of a new size are required to be generated according to the size of the scale:
$width _temp=imagesx ($image) * $zoom;
$height _temp=imagesy ($image) * $zoom;
$img =imagecreatetruecolor ($width _temp, $height _temp);
Copy the source map to the new diagram, the imagecopyresized of the GD library automatically scales the size of the
Imagecopyresized ($img, $image, 0,0,0,0, $width _temp, $height _temp, $width, $height);
To generate a small picture, clear the object:
Imagedestroy ($image);
$file _zoomname= "My_zoom_jpeg.jpg";
Imagejpeg ($img, $file _zoomname);
Imagedestroy ($IMG);
To generate a Qinglie graph, the watermark is basically the core technology of this point.

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.