Home > Backend Development > PHP Tutorial > Detailed explanation of how PHP uses built-in functions to generate images_php tips

Detailed explanation of how PHP uses built-in functions to generate images_php tips

PHP中文网
Release: 2016-05-16 09:00:02
Original
1738 people have browsed it

the example in this article describes how php uses built-in functions to generate images. share it with everyone for your reference, the details are as follows:

step 1: create an image

create a new php file and name it new-image.php( you can name it arbitrarily to facilitate subsequent calls).

there are two functions in php to create an image: imagecreate() creates an empty image; imagecreatefrompng() creates an image using an existing png image as the background. ("png" here can be replaced with "jpg" or "gif", depending on the format of the background image)


$Detailed explanation of how PHP uses built-in functions to generate images_php tips=imagecreate(400,60); //参数为宽度和高度
$Detailed explanation of how PHP uses built-in functions to generate images_php tips=imagecreatefromjpg("http://farm5.static.flickr.com/418978874_c349c14359_o.jpg");
Copy after login

no matter which of the above methods you use, we have created a picture and saved the picture in the $Detailed explanation of how PHP uses built-in functions to generate images_php tips variable.

step 2: add colors

at this time we need to create some colors through the imagecolorallocate() function and store them in variables for later convenience use.


$white=imagecolorallocate($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 255, 255, 255);
$black=imagecolorallocate($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 0, 0, 0);
$red=imagecolorallocate($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 255, 0, 0);
$green=imagecolorallocate($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 0, 255, 0);
$blue=imagecolorallocate($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 0, 0, 255);
Copy after login


step 3: draw graphics p>

you can also use some built-in functions of php to add some geometric shapes:


imageellipse() //绘制椭圆
imagearc() //绘制曲线
imagepolygon() //绘制多边形
imagerectangle() //绘制矩形
imageline() //绘制线条
Copy after login


p>

the following is an example


imagerectangle($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 50, 20, 200, 15, $blue);
Copy after login


at this time, you will find only the outline of the rectangle is drawn. if you want a filled rectangle, use the imagefilledrectangle() method:


imagefilledrectangle($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 50, 20, 200, 15, $blue);
Copy after login


step 4: add text

we can use the imagettftext() function to add text to the image:

the code is as follows :

imagettftext($Detailed explanation of how PHP uses built-in functions to generate images_php tips, 12, 0, 5, 20, $black, "fonts/oblivious font.ttf",  "这是要显示的内容");
Copy after login

you must have noticed that here you need to choose a font (fonts/oblivious font.ttf is used here, you can use your own favorite font). if you don’t know how to obtain fonts, you can google it. there are many websites for downloading fonts, and the content inside is enough to dazzle you.

step 5: generate pictures

when you have successfully drawn the picture you want to display, you must next displayed on the page. the first thing to do is to tell the page that the data you want to send to him is a picture:


//这里的png可以换成jpg,或者是gif,根据你要生成的图片格式来确定
header("content-type:image/png");
imagepng($Detailed explanation of how PHP uses built-in functions to generate images_php tips);
Copy after login


next, because we no longer need the $Detailed explanation of how PHP uses built-in functions to generate images_php tips variable, we need to release its memory:


imagedestroy($Detailed explanation of how PHP uses built-in functions to generate images_php tips);
Copy after login


step 6: call the picture

through the previous five steps, we have created a picture. here is the picture for you let’s talk about how to call this created picture.

in the html file, we use the url to introduce this image:

<img src="new-image.php的路径" alt="Detailed explanation of how PHP uses built-in functions to generate images_php tips" /> 
//在src中,写入new-image.php文件的路径,你就会发现刚才创建的图片已经被成功的调用了
Copy after login

use imagepng() to generate an image file

for example:

<?php 
$myImage=ImageCreate(400,60); //参数为宽度和高度 
$white=ImageColorAllocate($myImage, 255, 255, 255); 
$black=ImageColorAllocate($myImage, 0, 0, 0); 
$red=ImageColorAllocate($myImage, 255, 0, 0); 
$green=ImageColorAllocate($myImage, 0, 255, 0); 
$blue=ImageColorAllocate($myImage, 0, 0, 255); 
imagettftext($myImage, 12, 0, 5, 20, $red, "stxingka.ttf", "果冻 Elvis"); 
?> 
<?php imagepng($myImage,"xxx.jpg"); 
?>
Copy after login

the above is a detailed explanation of how php uses built-in functions to generate images_php tips. for more related content, please pay attention to the php chinese website (www.php.cn)!


Related labels:
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