PHP編實(shí)現(xiàn)程動(dòng)態(tài)圖像的創(chuàng)建代碼
Jun 13, 2016 pm 12:26 PM
在使用基本的圖像創(chuàng)建函數(shù)之前,需要安裝GD庫(kù)文件。如果要使用與JPEG有關(guān)的圖像創(chuàng)建函數(shù),還需要安裝jpeg-6b,如果要在圖像中使用Type 1型字體,則必須安裝t1lib。
在建立圖像創(chuàng)建環(huán)境之前,還需要做一些準(zhǔn)備工作。首先,安裝t1lib接著安裝jpeg-6b,然后再安裝GD庫(kù)文件。在安裝時(shí)一定要按這里給定的順序進(jìn)行安裝,因?yàn)樵诰幾gGD入庫(kù)時(shí)會(huì)用到j(luò)peg-6b,如果沒(méi)有安裝jpeg-6b,在編譯時(shí)就會(huì)出錯(cuò)。
在安裝完這三個(gè)組件后,還需要重新配置一次PHP,這也是你對(duì)采用DSO方式安裝PHP感到慶幸的地方之一。運(yùn)行make clean,然后在當(dāng)前的配置中添加下面的內(nèi)容:
--with-gd=[/path/to/gd]
--with-jpeg-dir=[/path/to/jpeg-6b]
--with-t1lib=[/path/to/t1lib]
完成添加后執(zhí)行make命令,然后再執(zhí)行make install命令,重新啟動(dòng)Apache后運(yùn)行phpinfo()來(lái)檢查一下新的設(shè)置是否生效了?,F(xiàn)在,我們就可以開(kāi)始圖像創(chuàng)建工作了。
根據(jù)所安裝的GD庫(kù)文件的版本將決定你是否能創(chuàng)建GIF或PNG格式的圖形文件。如果安裝的是gd-1.6或以前的版本,可以使用GIF格式的文件但不能創(chuàng)建PNG格式,如果安裝的是gd-1.6以后的版本,可以創(chuàng)建PNG文件但不能創(chuàng)建GIF格式的文件。
創(chuàng)建一幅簡(jiǎn)單的圖像也需要用到許多的函數(shù),我們將一步一步地進(jìn)行說(shuō)明。
在下面的例子中,我們將創(chuàng)建一個(gè)PNG格式的圖像文件,下面的代碼是一個(gè)包含所創(chuàng)建的圖像的MIME類型的頭部:
?。? header ("Content-type: image/png");
使用ImageCreate()創(chuàng)建一個(gè)代表空白圖像的變量,這個(gè)函數(shù)要求以像素為單位的圖像大小的參數(shù),其格式是ImageCreate(x_size, y_size)。如果要?jiǎng)?chuàng)建一個(gè)大小為250×250的圖像,就可以使用下面的語(yǔ)句:
$newImg = ImageCreate(250,250);
由于圖像還是空白的,因此你可能會(huì)希望用一些彩色來(lái)填充它。你需要首先使用ImageColorAllocate()函數(shù)用其RGB值為這種顏色指定一個(gè)名字,這一函數(shù)的格式為ImageColorAllocate([image], [red], [green], [blue])。如果要定義天藍(lán)色,可以使用如下的語(yǔ)句:
$skyblue = ImageColorAllocate($newImg,136,193,255);
接下來(lái),需要使用ImageFill()函數(shù)用這種顏色填充這個(gè)圖像,ImageFill()函數(shù)有幾個(gè)版本,例如ImageFillRectangle()、ImageFillPolygon()等。為簡(jiǎn)單起見(jiàn),我們通過(guò)如下的格式使用ImageFill()函數(shù):
ImageFill([image], [start x point], [start y point], [color])
ImageFill($newImg,0,0,$skyblue);
最后,在圖像建立后釋放圖像句柄和所占用的內(nèi)存:
ImagePNG($newImg);
ImageDestroy($newImg); ?>
這樣,創(chuàng)建圖像的全部代碼如下所示:
?。? header ("Content-type: image/png");
$newImg = ImageCreate(250,250);
$skyblue = ImageColorAllocate($newImg,136,193,255);
ImageFill($newImg,0,0,$skyblue);
ImagePNG($newImg);
ImageDestroy($newImg);
?>
如果把這個(gè)腳本文件保存為skyblue.php,并用瀏覽器訪問(wèn)它,我們會(huì)看到一個(gè)天藍(lán)色的250×250的PNG格式的圖像。
我們還可以使用圖像創(chuàng)建函數(shù)對(duì)圖像進(jìn)行處理,例如把一個(gè)較大圖像作成一個(gè)小圖像:
假設(shè)你有一幅圖像,想從中裁剪出一個(gè)35×35大小的圖像。你所需要作的是創(chuàng)建一個(gè)35×35大小的空白圖像,創(chuàng)建一個(gè)包含原來(lái)圖像的圖像流,然后把一個(gè)經(jīng)過(guò)調(diào)整大小的原來(lái)的圖像放到新的空白圖像中。
要完成這一任務(wù)的關(guān)鍵函數(shù)是ImageCopyResized(),它要求的格式如下所示:ImageCopyResized([new image handle],[original image handle],[new image X], [new Image Y], [original image X], [original image Y], [new image X], [new image Y], [original image X], [original image Y])。
?。? /*發(fā)送一個(gè)頭部,以便讓瀏覽器知道該文件所包含的內(nèi)容類型*/
header("Content-type: image/png");
/*建立保存新圖像高度和寬度的變量*/
$newWidth = 35;
$newHeight = 35;
/*建立給定高度和寬度的新的空白圖像*/
$newImg = ImageCreate($newWidth,$newHeight);
/*從原來(lái)較大的圖像中得到數(shù)據(jù)*/
$origImg = ImageCreateFromPNG("test.png");
/*拷貝調(diào)整大小后的圖像,使用ImageSX()、ImageSY()得到原來(lái)的圖像在X、Y方面上的大小*/
ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));
/*創(chuàng)建希望得到的圖像,釋放內(nèi)存*/
ImagePNG($newImg);
ImageDestroy($newImg); ?>
如果把這一小段腳本保存為resized.php,然后用瀏覽器對(duì)它進(jìn)行訪問(wèn),就會(huì)看到一個(gè)35×35大小的PNG格式的圖像。

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.
