溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php怎么創(chuàng)建圖像

發(fā)布時間:2021-07-02 09:33:33 來源:億速云 閱讀:123 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)php怎么創(chuàng)建圖像,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

php創(chuàng)建圖像具體步驟:1、設(shè)定標(biāo)頭,告訴瀏覽器要生成的MIME類型;2、創(chuàng)建一個畫布;3、進(jìn)行顏色管理;4、填充顏色;5、繪制圖形和文字;6、輸出圖像;7、銷毀圖像。

本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

PHP 創(chuàng)建圖像的具體步驟

一、設(shè)定標(biāo)頭,告訴瀏覽器你要生成的MIME 類型

共三種類型:

  • header(‘content-type:image/png’)

  • header ( ‘content-type: image/gif’);

  • header ( ‘content-type: image/jpeg’ );

<?php header("content-type: image/png");

二、創(chuàng)建一個畫布,以后的操作都將基于此畫布區(qū)域

resource imagecreatetruecolor ( int $width , int $height ) 新建一個真彩色圖像
返回一個圖像標(biāo)識符,代表了一幅大小為 width 和 height 的黑色圖像。
返回值:成功后返回圖象資源,失敗后返回 FALSE 。

$width = 200;
$height = 100;
$img = imagecreatetruecolor($width,$height);

三、顏色管理

**int imagecolorallocate ( resource $image , int $red , int $green , int $blue )**為一幅圖像分配顏色
red , green 和 blue 分別是所需要的顏色的紅,綠,藍(lán)成分。這些參數(shù)是 0 到 255 的整數(shù)或者十六進(jìn)制的 0x00 到 0xFF

$color = imagecolorallocate($img,0xcc,0xcc,0xcc); // 灰色

四、填充顏色

bool imagefill ( resource $image , int $x , int $y , int $color )
image 圖像的坐標(biāo) x , y (圖像左上角為 0, 0)處用 color 顏色執(zhí)行區(qū)域填充(即與 x, y 點(diǎn)顏色相同且相鄰的點(diǎn)都會被填充)。

imagefill($img,0,0,$color);

五、繪制圖形、文字

  1. imagesetpixel() 在 image 圖像中用 color 顏色在 x , y 坐標(biāo)(圖像左上角為 0,0)上畫一個點(diǎn)。
    bool imagesetpixel ( resource $image , int $x , int $y , int $color )
    例:在畫布上隨機(jī)畫100個點(diǎn)

for ($i= 0; $i < 100; $i++) { 
	$color = imagecolorallocate($img,rand(0,25), rand(0,255), rand(0,255));
	$x = rand(0,$width);
	$y = rand(0,$height);
	imagesetpixel($img, $x, $y, $color);
}
  1. imageline() 用 color 顏色在圖像 image 中從坐標(biāo) x1 , y1 到 x2 , y2 (圖像左上角為0, 0)畫一條線段。
    bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
    例:在畫布上隨機(jī)畫10條線段

for ($i= 0; $i < 10; $i++) { 
	$color = imagecolorallocate($img,rand(0,25), rand(0,255), rand(0,255));
	$x = rand(0,$width);
	$y = rand(0,$height);
	$x1 = rand(0,$width);
	$y1 = rand(0,$height);
	imageline($img, $x, $y, $x1, $y1,$color);
}
  1. imagerectangle() 用 col 顏色在 image 圖像中畫一個矩形,其左上角坐標(biāo)為 x1, y1,右下角坐標(biāo)為x2, y2。圖像的左上角坐標(biāo)為 0, 0。

bool imagerectangle ( resource $image , intbool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )

  1. imagefilledrectangle() 在 image 圖像中畫一個用 color 顏色填充了的矩形,其左上角坐標(biāo)為 x1,y1 ,右下角坐標(biāo)為 x2 , y2 。0, 0 是圖像的最左上角。

bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

  1. 繪制文字
    array imagettftext ( resource $image , float $size , float $anglearray imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text)
    size :字體的尺寸。根據(jù) GD 的版本,為像素尺寸(GD1)或點(diǎn)(磅)尺寸(GD2)。
    angle :角度制表示的角度,0 度為從左向右讀的文本。更高數(shù)值表示逆時針旋轉(zhuǎn)。例如 90 度表示從下向上讀的文本。
    由 x , y 所表示的坐標(biāo)定義了第一個字符的基本點(diǎn)(大概是字符的左下角)。
    color :顏色索引
    fontfile :是想要使用的 TrueType 字體的路徑。 (從我的電腦c盤里的Windows的fonts文件夾里找)
    text :UTF-8 編碼的文本字符串。

$color = imagecolorallocate($img,154, 75, 65));
$font = "simsunb.ttf";
$str = "hello , how are you?";
imagettftext($img, 30, 45, $x, $y, $color, $font, $str);

六、輸出圖像

三種方式:

  • bool imagepng ( resource $image [, string $filename ] )
    將 GD 圖像流(image )以 PNG 格式輸出到標(biāo)準(zhǔn)輸出(通常為瀏覽器),或者如果用 filename 給出了文件名則將其輸出到該文件。

  • bool imagegif ( resource $image [, string $filename ] )

  • bool imagejpeg ( resource $image [, string $filename [, int
    $quality ]])

imagepng($img);

七、銷毀圖像

bool imagedestroy ( resource $image )
釋放與 image 關(guān)聯(lián)的內(nèi)存

imagedestroy($img);

關(guān)于“php怎么創(chuàng)建圖像”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI