The following approach is a method:
if (!function_exists (' imagecreate ')) {
Die (' This server does not support GD module ');
}
How do I configure it if not supported? Download the DLL file of GD module, modify the php.ini, restart the server.
the following is referred to as PHP for PS.
When you intend to PS, you should complete the following steps, this is necessary.
1: Create a basic PS object (I assume $image), fill the background (default black), and all subsequent PS operations are based on this background image.
2: Drawing on the $image.
3: Output this image.
4: Destroy the object and clear use memory.
First, let's get to know a few common functions, which are described in detail in the PHP manual, which is broadly quoted here.
Resource imagecreate (int x_size, int y_size)
Imagecreate () returns an image identifier that represents a blank image of size 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 made up of a 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 respectively. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF. Imagecolorallocate () must be called to create each color used in the image represented by image.
---------------------------------------------------------
BOOL Imagefill (resource image, int x, int y, int color)
Imagefill () performs an area fill with a color color at the coordinates x, y (0, 0) of the image image (that is, the same as x, Y point color, and adjacent points are populated).
---------------------------------------------------------
BOOL Imageline (resource image, int x1, int y1, int x2, int y2, int color)
Imageline () Draws a line segment from coordinates x1,y1 to X2,y2 (0, 0 in the upper left corner of the image) with a 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 represented by image (this is the upper-left coordinate of the string, and the upper-left corner of the entire image is 0,0). If the 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 is 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 x, Y (the 0, 0) point in the upper-left corner of the image until it encounters a boundary with a color of border. Note: All colors within the boundary will be populated. If the specified boundary color is the same as the point color, there is no fill. If the boundary color does not appear in the image, the entire image is filled. 】
------------------------------------------------
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 color. Returns TRUE if successful, and FALSE if it fails.
=================================================
Output image data: Imagepng ($image [, $filename])
======================================================
Example one: A graphic that outputs a blue background and a crossed white line
$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 diagram
$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 one is a great circle.
Imagefilledarc ($image, $width/2, $height/2, $height, $height, 0,360, $color _blue,img_arc_pie);
Two small 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 ellipse and fill in */
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
$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, (a), $darkgray, Img_arc_pie);
Imagefilledarc ($image, $width/2, $i, $width/2, $height/2, Img_arc_pie, $darkred
}
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, Img_arc_pie, $red
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: a simple verification code
PHP is very easy to create verification code, easy to die, the simple idea is this:
Random seed generation, extraction of random characters, linked to print to graphics, output. To prevent color blindness, you can randomly extract colors, or you can customize the colors, and see below:
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);
Randomly get the number of strings
$strsize =rand (3,5);
Imagefill ($image, 0,0, $colorarrs [0]);
Write a string to a 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);
}
The session is written and later validated with
unset ($_session[' Cjjer ');
$_session[' cjjer '] = $sessionval;
Add a noise point
for ($i =0; $i < $width/$height * *; $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);
?>
The resulting style shows:
Online Demo: http://www.phzzy.org/temp/5do8/ex4_login.php
There is a very obvious problem is that the generated picture is not bright enough, so many users do not look clear, so we set ourselves a few more gorgeous color and then output, expand the 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 turn 23 rows into (17 rows):
$fontcolor = $colorarrs [Rand (1,count ($COLORARRS)-1)];
Output:
Online Demo: http://www.phzzy.org/temp/5do8/ex5_login.php
Example five: Big point of comparison cool verification code
PS image is still relatively small, sometimes for some reason (personal site in order to play cool,just me, commercial site play style, to attract users, just Google, something), Verification code is not limited to more than 10 px limit, sometimes completely 200, there is no problem, this time, One way is to force the small plot of the previous generation to be large. Can, however, look not smooth enough, it is true, obviously, broadband is no longer the most important issue, not to mention the other, the following shows a few more beautiful ways to build:
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);
Loading font libraries
$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 notes:
The first is the width and height, kao tai Small print can not see clearly. The randomly extracted characters are still a few:
$sourcestrings = "0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
Create the 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 for:
$fonts = DirName (__file__);//returns the current root directory, the font file is copied here, the font file is *.ttf file
Putenv (' "gdfontpath=". $fonts =. "");
$fontname = ' Arial ';
Define the height of the character, where I set it to half the height:
$font _size=floor ($height/2);
To clear variables, randomly set the number of characters to generate:
Unset ($sessionval);
$strsize =rand (5,8);
Cycle, each of the characters to hit up:
Get the string for this loop, and add it to the session after the variable.
$i _temp=rand (1,62);
$sessionval. = $sourcestrings [$i _temp];
Gets the position of the string where the image was 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 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 consult the relevant information. Very easy.
Write to session, while the registration code is used:
unset ($_session[' Cjjer ');
$_session[' cjjer '] = $sessionval;
To add a miscellaneous point:
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 to head:
Header (' content-type:image/png ');//This line indicates that it is a PNG image and can not be output by default. But not the head 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), font color $fontcolor_a and number of random numbers: $strsize = Rand (5,8);
Example six: Watermark a picture, generate a thumbnail
The traditional ASP page sub-dozen watermark and generate the indentation diagram are more cumbersome, generally used to other components or something, but, PHP can easily do these things, as you expect, less than 30 lines of the program to do all this, see this source program:
$source = "My.jpg";
$zoom = 0.5;
$str = ' I am handsome, are you a 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 images:
Original image 70K, here is the next, if the GIF, file 18k more, and PNG to use to 76K,SO we generate diagram in JPEG format.
Code Analysis:
Here I set a few parameters first:
$source = "My.jpg"; SOURCE picture
$zoom = 0.5; Zoom percentage
$str = ' I am handsome, are you a mm? '; The text of the watermark
Load source graph (does not hit watermark not loaded):
$image =imagecreatefromjpeg ($source);
Get a long, wide size:
$width =imagesx ($image);
$height =imagesy ($image);
Set the watermark font, because we use Chinese, you must import the Chinese font library, otherwise it will not be written or garbled, then you must convert the string encoding
$font =dirname (__file__). "//SIMSUN.TTC";
$str =iconv (' GB2312 ', ' UTF-8 ', $str);
Set start point, font size, angle of view:, Write on string:
$fontsize = 30;
$angle = 25;
$FROMX = $width/5;
$fromy = $height/2;
Imagettftext ($image, $fontsize, $angle, $fromx, $fromy, $color _red, $font, $STR);
To generate a new size object 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 GD library's imagecopyresized auto-scale size
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);
Generate a aftertaste graph, the watermark is the core of the technology so point.
http://www.bkjia.com/PHPjc/320185.html www.bkjia.com true http://www.bkjia.com/PHPjc/320185.html techarticle The following is a method: if (!function_exists (' imagecreate ')) {die (' This server does not support GD module '); If not, how do I configure it? Download the DLL file of GD module, modify PHP ...