Home > Backend Development > PHP Tutorial > Large example of php image function (not original)_PHP tutorial

Large example of php image function (not original)_PHP tutorial

WBOY
Release: 2016-07-21 15:46:14
Original
917 people have browsed it

The following method is a method:
if(!function_exists('imagecreate')) {
die('This server does not support GD module');
}
if If it is not supported, how to configure it? Download the dll file of the gd module, modify php.ini, and restart the server.
Hereinafter referred to as PHP drawing is PS.
When you plan to use PS, The following steps should be completed, which are necessary.
1: Create a basic PS object (I assume it is $image), fill in the background (default black), all subsequent PS operations are based on this background image.
2: Draw on $image.
3: Output the image.
4: Destroy the object and clear the used memory.
First, let’s get to know a few commonly used functions. These functions are in PHP There are detailed introductions in the manual, and they are generally quoted here.
resource imagecreate (int x_size, int y_size)
imagecreate() returns an image identifier, representing a blank image with sizes 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 representing a color consisting of the given RGB components. 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() The coordinates x, y of the image image (the upper left corner of the image is 0, 0 ) is used to perform area filling with the color color (that is, points with the same color as the x, y point and adjacent points will be filled).
-------------------------------------------------- ----------
bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() uses color color from coordinate x1 in image image , y1 to x2, y2 (the upper left corner of the image is 0, 0) draw a line segment.
-------------------------------------------------- ----------
bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() Use col color to draw string s to image The x, y coordinates of the image represented (this is the coordinate of the upper left corner of the string, the upper left corner of the entire 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, and the parameters are smaller There are many, so they will not be listed here. It mainly writes text to images, which is similar to the above function, but is more powerful than the former.
------------------- -------------------------------------
bool imagefilltoborder ( resource image, int x, int y, int border, int color )
imagefilltoborder() Starting from the x, y (the upper left corner of the image is 0, 0) point, perform area filling with color color until it reaches the border with color border. [Note: All colors within the border will be filled. If the specified border color is the same as the point, there is no fill. If the border color is not present in the image, the entire image will be filled. 】
------------------------------------------------- --
bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() In the image represented by image, use cx, cy (the upper left corner of the image is 0, 0) Draw an ellipse for the center. w and h specify the width and height of the ellipse respectively. The ellipse is filled with color. Returns TRUE on success, FALSE on failure.
================================================== ==
Output image data: imagepng($image[,$filename])
============================ ==========================
Example 1: Output a graphic with blue background and crossed white lines
$width=35;
$height=35;
//Create object
$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 the object to the header
header('content-type:image/png');
imagepng($image);
/*
//Send object to file
$filename="ex1.png";
imagepng($image,$filename);
*/
//Destroy object
imagedestroy($image);
?>
Output image:
Online demo: http://www.phzzy.org/temp/5do8/ex1.php
Example 2: 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
//Image
imagefill($image,0,0,$color_white);
//The first one is the big 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 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 the object to the header
header('content- type:image/png');
imagepng($image);
/*
//Send object to file
$filename="ex1.png";
imagepng($image ,$filename);
*/
//Destroy object
imagedestroy($image);
?>
Demo:
http://www.phzzy.org/ temp/5do8/ex2.php
Example 3: 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, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $i, $width/2, $height /2, 45 , 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $i, $width/2, $height /2, 75, 360 , $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, $width/2, $height /2, $width/2, $height /2, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, $width/2 , $height /2, $width/2, $height /2, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $height /2, $width/2, $ height /2, 75, 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 4: Simple verification code
PHP creation verification The code is very easy, damn easy. The simple idea is as follows:
Generate random seeds, extract random characters, connect them to graphics, and output. In order to prevent color blindness, you can randomly extract colors or customize colors, as follows Take a look:
session_start();
$width=65;
$height=20;
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$image=image create ($width,$height);
$colorarrs=array(
imagecolorallocate($image,255,255,255),//white
imagecolorallocate($image,0 ,0 , 0)//black
);
unset($sessionval);
imagesetthickness($image,3);
//Randomly obtain the number of strings
$strsize=rand(3,5);
imagefill ($image,0,0,$colorarrs[0]);
//Write strings to images one by one
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 ; /Add noise
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);
?>
Generate Style demonstration:
Online demonstration: http://www.phzzy.org/temp/5do8/ex4_login.php
An obvious problem is that the generated pictures are not bright enough, so many users do not look good. Clearly, let’s do this. We set a few brighter colors ourselves and then output them, expanding 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 change line 23 to (line 17):
$fontcolor=$colorarrs[rand(1,count($colorarrs)-1 )];
Output:
Online demonstration: http://www.phzzy.org/temp/5do8/ex5_login.php
Example 5: A larger and cooler verification code
PS The image is still relatively small. Sometimes for some reasons (personal sites are to be cool, just me, commercial sites are to play with style and attract users, just Google, more later), the verification code is not limited to a dozen px limit, there are At that time, it can be more than 200, no problem. At this time, one solution is to force the previously generated small picture to be larger, is it okay? Yes, but it does not look smooth enough. This is a fact. Obviously, broadband is no longer is the most important question, not to mention other things, here are a few more good-looking generation methods:
session_start();
$width=600;
$height=100 ;
if($height < $width /6)
$height=$width / 4;
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
//Create object
$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 the 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. The height is too small and the characters cannot be seen clearly. Randomly extracted There are still the same few characters:
$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:
$fonts= dirname(__FILE__);//Return to the current root directory , copy the font file here, the font file is a *.ttf file
putenv('"gdfontpath=".$fonts=.""');
$fontname='arial';
define characters Height, here I set it to half the height:
$font_size=floor($height / 2);
Clear the variable and randomly set the number of characters to be generated:
unset($sessionval);
$strsize=rand(5,8);
Loop, type characters one by one:
Get the string of this loop., and add it after the variable and write it into session
$i_temp =rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
Get the position of the string written to the image (x and y coordinates)
$x_i =$font_size + $ i *$width / ($strsize+1);
$y_i = $height / 2;
Set the inclination, viewed from the front.
$angle_i=rand(-120,120);
Randomly generate 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 any questions about this function, please check the relevant information. It is very easy.
Write to session , use the registration code:
unset($_SESSION['cjjer']);
$_SESSION['cjjer'] = $sessionval;
Add noise points:
//Number of noise 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 the header:
header('content-type:image/png');//This line indicates that it is a png image, which is optional. It can be output by default. But it is 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) ; Font color $fontcolor_a and number of random numbers: $strsize=rand(5,8);

Example 6: Watermark images and generate thumbnails
Traditional ASP page watermarking Generating thumbnails is relatively cumbersome, and other components are generally used. However, PHP can easily do these things. As you would expect, it can be done in less than 30 lines of program. Please see this source program. :
$source="my.jpg";
$zoom=0.5;
$str='I am a handsome guy, 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 image:
Generated jpg image :
The original image is 70K. Let me talk about it here. If you generate gif, the file will be more than 18k, and png will use 76k, so we generate thumbnails in jpeg format.
Code analysis:
Here I set it first Several parameters:
$source="my.jpg"; //Source image
$zoom=0.5; //Zoom percentage
$str='I am a handsome guy, are you a girl?' ; //Watermark text
Load the source image (no watermark, no loading):
$image=imagecreatefromjpeg($source);
Get the length and width size:
$width=imagesx( $image);
$height=imagesy($image);
Set the watermark font, because we are using Chinese, we must import the Chinese font library, otherwise the writing will not be possible or the code will be garbled, and then the string encoding must be converted
$font=dirname(__FILE__). "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
Set the starting point, font size, Perspective:, write the string:
$fontsize=30;
$angle=25;
$fromx=$width/5;
$fromy=$height/2;
imagettftext ($image,$fontsize,$angle,$fromx,$fromy,$color_red,$font,$str);
Generate an object of new size according to the zoom size requirement:
$width_temp=imagesx($image ) * $zoom;
$height_temp=imagesy($image) * $zoom;
$img=imagecreatetruecolor($width_temp,$height_temp);
Copy the source image to the new image, imagecopyresized of the gd library
imagecopyresized with automatic scaling ($img,$image,0,0,0,0,$width_temp, $height_temp,$width,$height);
Generate small image, clear object:
imagedestroy ($image);
$file_zoomname="my_zoom_jpeg.jpg";
imagejpeg($img,$file_zoomname);
imagedestroy($img);
Generate thumbnails and watermark core technology That's all.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320185.htmlTechArticleThe following method is a method: if(!function_exists('imagecreate')) { die('This server does not Support GD module'); } If it is not supported, how to configure it? Download the dll file of the gd module and modify php...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template