search
HomeBackend DevelopmentPHP TutorialPHP uses GD library to draw pictures and generate verification code pictures_PHP tutorial

PHP uses the GD library to draw pictures and generate verification code images

First of all, make sure that the GD extension function is turned on in the php.ini settings. The test is as follows

print_r(gd_info());

If the following content is printed out, it means that the GD function is turned on:

Array
(
    [GD Version] => bundled (2.0.34 compatible)
    [FreeType Support] => 1
    [FreeType Linkage] => with freetype
    [T1Lib Support] => 1
    [GIF Read Support] => 1
    [GIF Create Support] => 1
    [JPG Support] => 1
    [PNG Support] => 1
    [WBMP Support] => 1
    [XPM Support] => 
    [XBM Support] => 1
    [JIS-mapped Japanese Font Support] => 
)

The general steps for GD drawing are as follows:

1. Create a canvas resource

2. Create a color brush

3. Drawing

4. Save the picture or export the picture

5. Destroy memory canvas resources

The test code is as follows:

<!--?php
header(Content-type: image/jpeg);

$width = 400;   //宽,高
$height = 400;  

$image = imagecreate($width, $height); //第一步:创建空白图像

$white = imagecolorallocate($image, 0, 0, 0);  //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色,即用 imagecreate() 建立的图像。 

$green = imagecolorallocate($image, 0, 255, 0); //第二步:为图像分配颜色

imageline($image, 0, 20, 400, 20, $green);  //第三步:画线
imagerectangle($image,100,40,300,100,$green);  //画矩形
imagearc($image, 200, 150, 90, 90, 0, 360, $green); //画圆
imagestring($image, 14, 100, 240, PHP is NiuBi HongHong!, $green); //写字符串

$str=abcdefghjklmnpqrstuvwxyz23456789;
$randstr = substr(str_shuffle($str), 0,4);
imagestring($image, 14, 100, 260, $randstr, $green); //验证码

imagettftext($image, 14, 0, 100, 300, $green, &#39;./MSJHBD.TTF&#39;, 中文vsEnglish);  //中文验证

// imagejpeg($image,&#39;./test.jpg&#39;);   //在当前路径下保存图片为test.jpg
imagejpeg($image);  //第四步:不加文件名,直接输出到网页     

imagedestroy($image);   //第五步:销毁,回收资源
?-->

The test pictures are as follows:

Note: The GD library is powerful and can draw various reports (such as bar charts, pie charts, etc.), thumbnails, watermarked images and stock trend charts

<!--?php
header(Content-type: image/png);

$width = 300;   //原图宽,高
$height = 210;  

$thumb_width = (int)$width/2;
$thumb_height = (int)$height/2;


$dst = imagecreate($thumb_width,$thumb_height); //创建缩略图画布

$gray = imagecolorallocate($dst, 100, 100, 100);


$src = imagecreatefrompng(&#39;./me.png&#39;); //读取原图

//把原图copy到缩略图画布上
imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); 

imagepng($dst,&#39;./me_thumb.png&#39;);

imagedestroy($dst);
imagedestroy($src);
?-->


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969598.htmlTechArticlePHP uses the GD library to draw pictures and generate verification code pictures. First, you must make sure whether the GD extension function is turned on in the php.ini settings. The test is as follows print_r(gd_info()); If the printed content is as follows, it means...
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor