Home Backend Development PHP Tutorial PHP graphics image manipulation

PHP graphics image manipulation

Aug 08, 2016 am 09:20 AM
imagepng logo

Introduction to GD library

GD refers to Graphic Device. PHP's GD library is an extension library for processing graphics. Through a series of APIs provided by the GD library, images can be processed or new pictures can be directly generated.

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.

<span>header("content-type: image/png"); $img=imagecreatetruecolor(100, 100); $red=imagecolorallocate($img, 0xFF, 0x00, 0x00); imagefill($img, 0, 0, $red); imagepng($img); imagedestroy($img);<span><span></span></span></span></preubuntu></divmicrosoft></p> <divmicrosoft yahei sans gb neue font-size:14px line-height:21px><p><span>Draw lines</span></p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>To operate graphics, first create a new canvas, and create a true-color blank picture through the imagecreatetruecolor function: </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$img = imagecreatetruecolor(100, 100);</span><p><span>In the GD library for The color used by the brush needs to be allocated through the imagecolorallocate function. The color of the brush is determined by setting the RGB color value via parameters: </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);</span><p><span> Then we draw the line by calling the line segment function imageline, and specify the starting point and end point. Finally get lines. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imageline($img, 0, 0, 100, 100, $red);</span><p><span>After the lines are drawn, the image is output through header and imagepng. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>header("content-type: image/png"); imagepng($img);</span><p><span>Finally, you can call imagedestroy to release the memory occupied by the image. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagedestroy($img);</span><p><span>Through the above steps, you can find that drawing graphics with PHP is very simple, but many times we not only need to output pictures, we may also need to get an image file. We can specify the file name through the imagepng function to save the drawn image. into the file. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagepng($img, 'img.png');</span></preubuntu></preubuntu></preubuntu></preubuntu></preubuntu></preubuntu></divmicrosoft></p> <divmicrosoft yahei sans gb neue font-size:14px line-height:21px><p>Draw text in images</p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>The GD library can perform a variety of basic graphics operations. Commonly used ones include drawing lines, background filling, drawing rectangles, drawing text, etc. </span></p> <p><span>Similar to drawing lines, you first need to create a new picture and initialize the color. </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$img = imagecreatetruecolor(100, 100); $red = imagecolorallocate($img, 0xFF, 0x00, 0x00);</span><p><span>Then use the imagestring function to draw text. This function has many parameters: imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col ), which can be passed $font sets the font size, x and y set the text display position, $s is the text to be drawn, and $col is the color of the text. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagestring($img, 5, 0, 0, "Hello world", $red); header("content-type: image/png"); imagepng($img); imagedestroy</span><span>($img); </span></preubuntu></preubuntu></divmicrosoft></p> <p><span>Output image file</span></p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>We have learned earlier that imagepng can directly output images to the browser, but many times, we want to save the processed image to a file so that Can be used multiple times. Save the image to a file by specifying the path parameter. </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$filename = 'img.png'; imagepng($img, $filename);</span><p><span>Use imagepng to save the image into png format. If you want to save it into other formats, you need to use different functions. Use imagejpeg to save the image into jpeg format, and imagegif to save the image into gif format. It should be noted that imagejpeg The image is compressed, so a quality parameter can also be set. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$filename = 'img.jpg'; ?imagejpeg($img, $filename, 80);</span></preubuntu></preubuntu></divmicrosoft></p> <p>Generate image verification code</p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>A simple verification code actually outputs a few characters in the picture, which can be achieved through the imagestring function we mentioned in the previous chapter. </span></p> <p><span>But in terms of processing, in order to make the verification code more secure and prevent other programs from automatically recognizing it, it is often necessary to perform some interference processing on the verification code. Usually, some noise points are drawn, interference line segments are drawn, and the output characters are tilted , twist and other operations. </span></p> <p><span>You can use imagesetpixel to draw points to achieve noise interference, but drawing only one point has little effect, so loops are often used here to draw randomly. </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>for($i=0;$i<50;$i++) { imagesetpixel($im, rand(0, 100) , rand(0, 100) , $black); imagesetpixel($im, rand(0, 100) , rand(0, 100) , $green); } </span><span></span></preubuntu></divmicrosoft></p> <br><p><br></p> <pre name="code"><?php $img = imagecreatetruecolor(100, 40); $black = imagecolorallocate($img, 0x00, 0x00, 0x00); $green = imagecolorallocate($img, 0x00, 0xFF, 0x00); $white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF); imagefill($img,0,0,$white); //生成随机的验证码 $code = ''; for($i = 0; $i < 4; $i++) { $code .= rand(0, 9); } imagestring($img, 5, 10, 10, $code, $black); //加入噪点干扰 for($i=0;$i<50;$i++) { imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black); imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green); } //输出验证码 header("content-type: image/png"); imagepng($img); imagedestroy($img);
Copy after login

Add watermark to pictures

There are generally two ways to add watermarks to pictures, one is to add a string to the picture, the other is to add a watermark to the picture Add a logo or other picture.

Because what is being processed here is an existing image, you can create a canvas directly from an existing image, and create an image directly from an image file through imagecreatefromjpeg.

<span>$im = imagecreatefromjpeg($filename);</span><p><span>创建图像对象以后,我们就可以通过前面的GD函数,绘制字符串到图像上。如果要加的水印是一个logo图片,那么就需要再建立一个图像对象,然后通过GD函数imagecopy将logo的图像复制到源图像中。</span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$logo = imagecreatefrompng($filename); imagecopy($im, $logo, 15, 15, 0, 0, $width, $height);</span><p><span>当将logo图片复制到原图片上以后,将加水印后的图片输出保存就完成了加水印处理。</span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagejpeg(</span><span>$im, $filename); </span></preubuntu></preubuntu></preubuntu></divmicrosoft></p> <br><p></p> <pre name="code"><?php //这里仅仅是为了案例需要准备一些素材图片 $url = 'http://www.iyi8.com/uploadfile/2014/0521/20140521105216901.jpg'; $content = file_get_contents($url); $filename = 'tmp.jpg'; file_put_contents($filename, $content); $url = 'http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png'; file_put_contents('logo.png', file_get_contents($url)); //开始添加水印操作 $im = imagecreatefromjpeg($filename); $logo = imagecreatefrompng('logo.png'); $size = getimagesize('logo.png'); imagecopy($im, $logo, 15, 15, 0, 0, $size[0], $size[1]); header("content-type: image/jpeg"); imagejpeg($im);
Copy after login

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了PHP图形图像操作,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if my iwatch only lights up with the logo but does not turn on? What should I do if my iwatch only lights up with the logo but does not turn on? Mar 25, 2023 pm 02:00 PM

Reasons and solutions for why iwatch only lights up the logo but does not turn on: 1. It is caused by insufficient power and cannot be turned on. You can put the back of the iwatch on the charger; 2. It is caused by system version problems. You can roll the system back to the stable version; 3. , caused by charger or charging cable failure, you can use another iwatch magnetic charging cable and another USB power adapter; 4. caused by hardware failure, it is recommended to go to a professional third-party testing machine for maintenance.

Foton Motor releases new logo and price information of Xiangling Q series models Foton Motor releases new logo and price information of Xiangling Q series models Sep 12, 2023 pm 09:09 PM

On August 29, Foton Motor held a gorgeous brand refresh conference, bringing a series of exciting news to the industry. The new logo, Auman Zhilan bottom battery replacement products and the new Xiangling Q car became the focus of the press conference. Foton Motor's new logo shows the company's ambitions for the future. Foton Motor said that this new logo symbolizes the renewal and vigorous development of the brand, marking the company's entry into a new stage of development. At the press conference, Foton Motor also launched the much-anticipated Auman Smart Blue bottom battery replacement product to bring users Here comes a more convenient and efficient use experience. At the same time, the newly launched Xiangling Q car series has also attracted a lot of attention. There are 4 models in total, with prices ranging from 167,800 yuan to 168,800 yuan, providing consumers with

Detailed explanation of PHP chart generation function: chart generation guide for gd library, imagepng, imagestring and other functions Detailed explanation of PHP chart generation function: chart generation guide for gd library, imagepng, imagestring and other functions Nov 18, 2023 pm 04:56 PM

Detailed explanation of PHP chart generation functions: Chart generation guide for gd library, imagepng, imagestring and other functions. Chart generation plays an important role in data visualization and can present data change trends and relationships more intuitively. As a popular server-side scripting language, PHP provides a series of powerful chart generation functions. This article will introduce in detail the use of functions such as gd library, imagepng, imagestring, etc., and provide specific code examples to help readers quickly

Black screen problem occurs after Windows 10 starts Black screen problem occurs after Windows 10 starts Dec 28, 2023 pm 01:57 PM

Computers are now an electrical appliance that is basically installed in every household. With the continuous use of computers, some strange problems have slowly emerged. Recently, many friends have reported that the recent computer problems It's a black screen after booting up. What's going on? In fact, it is very simple to solve this problem. Today, the editor will bring you the solution to the black screen after the welcome screen in win10. Friends who need it, please come and take a look. Solution to the black screen after win10 starts with logo: Operation steps: 1. First check whether the power cable is connected properly and check whether the monitor is damaged. The detection method only requires turning off the computer. Only turning on the monitor will show no information. If there is no signal, it proves that the monitor is OK2 and the graphics card driver is incompatible.

Google updates Android logo! Capitalization, fonts, and the green robot have all changed. Google updates Android logo! Capitalization, fonts, and the green robot have all changed. Sep 08, 2023 pm 01:13 PM

[Mobile China News] Before launching the stable version of Android 14 next month, Google has now modified the Android logo, including the logo and image (green robot). The new Android3D logo conforms to the MaterialYou theme and differs from the 2019 logo in several ways. Case: Starting with text, the first letter in Android, the letter A, is now uppercase. It used to be all lowercase. Font weight: Android’s logo font is thicker. In other words, the font weight is increased. Green Robot: The Android robot logo (also known as Bugdroid) is now 3D, with more curves, more dimensions, more characters,

Alipay changes its logo after 4 years Alipay changes its logo after 4 years Feb 23, 2024 pm 07:07 PM

Alipay will change its logo every four years, so what will the logo look like in 2024? Users can see that it has a new look and a more advanced logo style. This introduction to Alipay’s 4-year new logo can tell you the specific content. The following is a detailed introduction, take a look! Alipay usage tutorial What is the new Alipay logo in 4 years? Logo display: Historical styles: 1. 2020 version Logo2, 2016 version Logo3, all versions of logo Software introduction: 1. This software was established in 2004 and has more than 1,000 life services . 2. This software is responsible for providing products and services to digital service providers, and many merchants have settled in it.

OPPO announced a gradual reduction in the use of color logos, causing the nickname 'Green Factory Turns Black Factory' and the topic of major platforms replacing black-bottom logos OPPO announced a gradual reduction in the use of color logos, causing the nickname 'Green Factory Turns Black Factory' and the topic of major platforms replacing black-bottom logos Aug 03, 2023 pm 06:25 PM

According to news from this site on August 3, many of our friends recently discovered that OPPO has changed the logos of its official accounts on all major platforms to black backgrounds, removing the previous iconic green, so it has also been nicknamed by many people. "The green factory turns into a black factory." In response to netizens, OPPO’s official Douyin account expressed concern about this change. In order to better assist brand image building and provide a more technological and friendly experience, in the future, our brand logo will gradually reduce the use of colors to focus on The color form is exposed to help communicate brand and product information more concisely and efficiently. Of course, green is still a very important part of OPPO’s brand. We will also use green and various colors embellished in interactive visual design to enrich every scene where we meet users. Upon inquiry, it was found that

Two methods to solve the black screen problem after win10 starts up Two methods to solve the black screen problem after win10 starts up Dec 21, 2023 pm 03:45 PM

The screen goes black after the computer starts up and the logo is displayed. This is a problem encountered by users using the Win10 system. You can first check whether the connection cable is normal, or you can restart the computer. This article is shared by this site to solve the problem after the Win10 computer starts up and the logo is displayed. The solution to the black screen. Method 1: 1. First, press the [Ctrl+Alt+Del] key combination, and then click Task Manager. In the Task Manager window, click [File] in the upper left corner, and in the drop-down menu that opens, select [Run New Task]. Create a new task window, enter the [Explorer.exe] command, and then press [OK or Enter] (check to create this task with system administrative rights); 2. Press the [Win+S] key combination to open Windows Search

See all articles