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中文网
PHP中文网Original
2016-05-16 09:00:021760browse

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");

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);


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() //绘制线条


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);


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);


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",  "这是要显示的内容");

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);


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);


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文件的路径,你就会发现刚才创建的图片已经被成功的调用了

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"); 
?>

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)!


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