This article mainly shares with you the basic tutorial on using the GD library for operating PHP images and graphics. I hope it can help you.
1>Introduction to GD library
GD refers to Graphic Device. PHP’s GD library is an extension library used to process graphics. Through a series of APIs provided by the GD library, you can Process images or directly generate new images.
In addition to text processing, PHP can also process JPG, PNG, GIF, SWF and other images through the GD library. The GD library is commonly used in image watermarking, verification code generation, etc.
PHP has integrated the GD library by default, you just need to enable it during installation.
General process of creating images
Set the header to tell the browser the MIME type you want to generate
Create an image area, and all subsequent operations will be based on this image area
Draw a filled background in the blank image area
Draw the graphic outline on the background and input text
Output the final graphic
-
Clear all resources
Other page calls
header("content-type: image/png");$img=imagecreatetruecolor(100, 100);$red=imagecolorallocate($img, 0xFF, 0x00, 0x00); imagefill($img, 0, 0, $red); imagepng($img); imagedestroy($img);
-
Draw lines
imageline()
Syntax: imageline(sX,
eX,
col);
-
Draw a circle
imagearc()
Syntax: imagearc (cx ,
w ,
startAngle,
color )
$img = imagecreatetruecolor(200, 200);// 分配颜色$red = imagecolorallocate($img, 255, 0, 0);$white = imagecolorallocate($img, 255, 255, 255);//背景填充白色 imagefill($img,0,0,$white);// 画一个红色的圆 imagearc($img, 100, 100, 150, 150, 0, 360, $red); imagepng($img);// 释放内存 imagedestroy($img);
-
Draw a rectangle
imagerectangle()
Syntax: imagerectangle (x1 ,
x2 ,
col)
$img = imagecreatetruecolor(200, 200);// 分配颜色$red = imagecolorallocate($img, 255, 0, 0);$white = imagecolorallocate($img, 255, 255, 255); imagefill($img,0,0,$white);// 画一个红色的矩形 imagerectangle ($img,50,50,100 ,100 ,$red); imagepng($img);// 释放内存 imagedestroy($img);
-
Draw text
Syntax 1: imagestring (font ,
y ,
col )
Syntax 2: imagettftext(size,
x,
color,
text)
header("content-type: image/png");//imagestring字体大小设置不了$img = imagecreatetruecolor(100, 100);$red = imagecolorallocate($img, 0xFF, 0x00, 0x00); imagestring($img, 5, 10, 10, "Hello world", $red); imagepng($img); imagedestroy($img);$img1=imagecreatetruecolor(200,200);$red=imagecolorallocate($img1,255,0,0);$white=imagecolorallocate($img1,255,255,255); imagefill($img1,0,0,$red);$font="C:\Windows\Fonts\simhei.ttf"; imagettftext($img1,23,0,100,100,$white,$font,"你好吗"); imagepng($img1); imagedestroy($img1);
-
Drawing noise
Syntax: imagesetpixel(x,
col)
//绘制10个噪点for($i=0;$i<blockquote> <p>Output image file<br> Through imagepng, you can directly output the image to the browser and save the image to the file by specifying the path parameter <br> 1. imagepng() <br> Meaning: Save the picture in png format<br> Syntax: imagepng(</p>filename)<br> 2. imagejpeg()<br> Meaning: Save the picture into jpeg format<br> Syntax: imagepng(filename,$quality)<br> 3. imagegif()<br> Meaning: Save the picture into gif format<br> Syntax: imagegif(filename)<p>Case: <br> 1. Randomly generate verification code (php)<br> 2. Add a watermark to the picture</p> </blockquote><p>Related recommendations:</p><p><a href="http://www.php.cn/php-weizijiaocheng-382962.html" target="_self">Solution to the garbled watermark generated by the GD library</a></p><p><a href="http://www.php.cn/php-weizijiaocheng-377028.html" target="_self">Detailed explanation of how to use PHP Tutorial on using the GD library to complete the verification code effect</a></p><p><a href="http://www.php.cn/php-weizijiaocheng-360873.html" target="_self">What is the GD library? Detailed introduction to loading GD library in PHP</a></p>
The above is the detailed content of Basic tutorial on using PHP image graphics GD library. For more information, please follow other related articles on the PHP Chinese website!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools
