Home >Backend Development >PHP Tutorial >photoshop cs2 v9.0 green Chinese version php image function example (not original)

photoshop cs2 v9.0 green Chinese version php image function example (not original)

WBOY
WBOYOriginal
2016-07-29 08:40:051144browse

The following method is one method:
if(!function_exists('imagecreate')) {
die('This server does not support the GD module');
}
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 as PS.
When you plan to use PS, you should complete the following steps, which are necessary.
1: Create a basic PS object (I assume it is $ image), fill the background (default black), all subsequent PS operations are based on this background image.
2: Draw on $image.
3: Output this image.
4: Destroy the object and clear the used memory.
First, let’s get to know a few commonly used functions. These functions are introduced in detail in the PHP manual and are generally quoted here.
resource imagecreate (int x_size, int y_size)
imagecreate() returns an image identifier, representing A blank image of size x_size and y_size is obtained.
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 the A color composed 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() is executed with color at the coordinates x, y of the image (the upper left corner of the image is 0, 0) Area filling (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 coordinates x1, y1 to x2, y2 in the image image (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() uses col color to draw string s to x of the image represented by image , at the y coordinate (this is the coordinate of the upper left corner of the string, and 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 has many parameters, so it will not be discussed here. List, it mainly writes words to images, similar to the above function, but more powerful than the former.
--------------------------- -------------------------------
bool imagefilltoborder ( resource image, int x, int y, int border, int color )
imagefilltoborder() starts from x, y (the upper left corner of the image is 0, 0) point and performs area filling with 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() Draw an image centered on cx, cy (the upper left corner of the image is 0, 0) in the image represented by image oval. 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 graphics 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 the object to the file
$filename="ex1.png";
imagepng($image,$filename);
*/
//Destroy the object
imagedestroy($ image);
?>
Output image:
Online demonstration: 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
//Drawing
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*/
imagefilledellipse ($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 the object to the file
$filename="ex1.png";
imagepng($image,$filename);
*/
//Destroy the 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
It is very easy to create a verification code in PHP. It is so 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 Extract colors randomly or customize colors, 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);
//Get the number of strings randomly
$strsize=rand(3,5);
imagefill($image,0,0,$colorarrs[0]);
//One Write strings to images
for($i=0;$i<$strsize;$i++){
$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
$ f
$y_i = $height/2 + $font_size /3;
imagechar($image,5, 1+ $i * $width /$strsize,5,$sourcestrings[$i_temp],$fontcolor);
}
//Write into session, use
unset($_SESSION['cjjer']) for later verification;
$_SESSION['cjjer'] = $sessionval;
//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);
?>
Generated style demo:
Online demo: http://www.phzzy.org/temp/5do8/ex4_login.php
An obvious problem is that the generated images are not enough Gorgeous, so many users don’t see it clearly, so let’s 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):
$f
Output:
Online demonstration: http://www.phzzy.org/temp/5do8/ex5_login.php
Example Five: Larger and cooler verification codes
PS images are still relatively small, sometimes for some reasons (personal sites to be cool, just me, commercial sites to play with style and attract users, just google, more later), The verification code is not limited to a dozen pixels. Sometimes it can be more than 200 pixels. There is no problem. At this time, one solution is to force the previously generated small image to be larger. Is that okay? Yes, but, look It is not smooth enough. This is a fact. Obviously, broadband is no longer the most important issue. 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
$f
putenv('"gdf
$f
$f / 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);
$f
imageTTFText($image,$font_size,$angle_i,$x_i,$y_i,$fontcolor_a,$fontname,$sourcestrings[$i_temp]);
}
unset($_SESSION ['cjjer']);
$_SESSION['cjjer'] = $sessionval;
//Number of noise
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 to be seen clearly. The randomly extracted characters are still the same:
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
Create the object and fill it Make it white:
$image=imagecreate($width,$height);
$white=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
Then load the verification code you want Font:
$f//Return to the current root directory, copy the font file here, the font file is a *.ttf file
putenv('"gdf
$f
Define the height of the character, here I set it to half the height:
$ f / 2);
Clear the variables and randomly set the number of characters to be generated:
unset($sessionval);
$strsize=rand(5,8);
Loop and type the characters one by one:
Get this The string of the loop., and add it to the variable and write it into the session later
$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
Get the position of the string written into 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,
$f
Write to the 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 into the session and 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 $f / 2); font color $fontcolor_a and the number of random numbers: $strsize= rand(5,8);
Example 6: Watermark pictures and generate thumbnails
Traditional ASP page watermarking and thumbnail generation are relatively cumbersome, and other components are generally used, but , 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'm a handsome guy, are you MM?';
$image=imagecreatefromjpeg($source);
$width=imagesx($image);
$height=imagesy($image);
$color_red= imagecolorallocate($image,111,0,0);//red
$f "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
$f
$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 picture:
Original picture 70K. Here, if you generate gif, the file will be more than 18k, and png will use 76k, so we use jpeg to generate thumbnails Format.
Code analysis:
Here I set a few parameters first:
$source="my.jpg"; //Source image
$zoom=0.5; //Zoom percentage
$str='I am a handsome guy, you Is it MM?'; //Watermark text
Load the source image (no loading without watermark):
$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 it will not be written or the characters will be garbled, and then the string encoding must be converted
$f "//simsun. ttc";
$str=iconv('GB2312','UTF-8',$str);
Set the starting point, font size, angle of view:, write the string:
$f
$angle=25;
$ fromx=$width/5;
$fromy=$height/2;
imagettftext($image,$fontsize,$angle,$fromx,$fromy,$color_red,$font,$str);
According to the zoom size requirement Generate an object of new size:
$width_temp=imagesx($image) * $zoom;
$height_temp=imagesy($image) * $zoom;
$img=imagecreatetruecolor($width_temp,$height_temp);
Copy the source image To a new image, the gd library's imagecopyresized automatically scales the size
imagecopyresized ($img,$image,0,0,0,0,$width_temp, $height_temp,$width,$height);
Generate a small picture and clear the object:
imagedestroy($image);
$file_zoomname="my_zoom_jpeg.jpg";
imagejpeg($img,$file_zoomname);
imagedestroy($img);
Generate thumbnails. This is the core technology of watermarking.

The above has introduced a large example of photoshop cs2 v9.0 green Chinese version PHP image function (not original), including the content of photoshop cs2 v9.0 green Chinese version. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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