php創建影像具體步驟

藏色散人
發布: 2023-03-11 07:14:02
原創
3007 人瀏覽過

php建立影像特定步驟:1、設定標頭,告訴瀏覽器要產生的MIME類型;2、建立一個畫布;3、進行顏色管理;4、填滿顏色;5、繪製圖形和文字;6、輸出影像;7、銷毀影像。

php創建影像具體步驟

本文操作環境:windows7系統、PHP7.1版,DELL G3電腦

PHP 建立映像的具體步驟

一、設定標頭,告訴瀏覽器你要產生的MIME 類型

共有三種:

  • header('content-type:image/png')
  • header ( 'content-type: image/gif');
  • header ( 'content-type: image/jpeg' );
<?php header("content-type: image/png");
登入後複製

二、建立一個畫布,以後的動作都會基於此畫布區域

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

$width = 200;
$height = 100;
$img = imagecreatetruecolor($width,$height);
登入後複製

三、顏色管理

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

$color = imagecolorallocate($img,0xcc,0xcc,0xcc); // 灰色
登入後複製

四、填滿顏色

bool imagefill ( resource $image , int $x , int $ y , int $color )
image 圖像的座標x , y (圖像左上角為0, 0)處以color 顏色執行區域填充(即與x, y 點顏色相同且相鄰的點都會被填充)。

imagefill($img,0,0,$color);
登入後複製

五、繪製圖形、文字

  1. imagesetpixel() 在image 圖像中用color 顏色在x ,y 座標(圖像左上角為0, 0)上畫一個點
    bool imagesetpixel ( resource $image , int $x , int $y , int $color )
    例:在畫布上隨機畫100個點
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 中從座標x1 , y1 到x2 , y2 (圖片左上角為0, 0)畫一條線段
    bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
    例:在畫布上隨機畫出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 影像中畫一個矩形,其左上角座標為x1, y1,右下角座標為x2, y2。圖像的左上角座標為 0, 0。

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








  1. ##imagefilledrectangle()
在image 圖片中畫一個

用color 顏色填滿了的矩形

,其左上角座標為x1,y1 ,右下角座標為x2 , y2 。0, 0 是映像的最左上角。

  • bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
  • 繪製文字

  • 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 :字體的尺寸。根據GD 的版本,為像素尺寸(GD1)或點(磅)尺寸(GD2)。

angle :角度製表示的角度,0 度為從左向右讀的文字。更高數值表示逆時針旋轉。例如90 度表示從下向上讀的文字。

由x , y 所表示的座標定義了第一個字元的基本點(大概是字元的左下角)。

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 格式輸出到標準輸出(通常為瀏覽器),或如果使用filename 給出了檔案名稱則將其輸出到該檔案。 ######bool imagegif ( resource $image [, string $filename ] )######bool imagejpeg ( resource $image [, string $filename [, int### $quality ]])### ###
imagepng($img);
登入後複製
###七、銷毀影像#########bool imagedestroy ( resource $image )###### 釋放與image 關聯的記憶體###
imagedestroy($img);
登入後複製

  推荐学习:《PHP视频教程

以上是php創建影像具體步驟的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!