13.1.3 繪制各種圖形

13.1.3 繪制各種圖形

imagefill -- 區域填充
語法:bool imagefill(resource image,int x,int y, int color)
imagefill() 在 image 圖像的坐標 x,y(圖像左上角為 0, 0)處用 color 顏色執行區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。
imagesetpixel -- 畫一個單一像素
語法:bool imagesetpixel ( resource image, int x, int y, int color )
imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點。
imageline -- 畫一條線段
語法:bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() 用 color 顏色在圖像 image 中從坐標 x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段。

imagerectangle -- 畫一個矩形
語法:bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int col )
imagerectangle() 用 col 顏色在 image 圖像中畫一個矩形,其左上角坐標為 x1, y1,右下角坐標為 x2, y2。圖像的左上角坐標為 0, 0。
imagefilledrectangle -- 畫一矩形并填充
語法:bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )
imagefilledrectangle() 在 image 圖像中畫一個用 color 顏色填充了的矩形,其左上角坐標為 x1,y1,右下角坐標為 x2,y2。0, 0 是圖像的最左上角。

imageellipse -- 畫一個橢圓
語法:bool imageellipse ( resource image, int cx, int cy, int w, int h, int color )
imageellipse() 在 image 所代表的圖像中畫一個中心為 cx,cy(圖像左上角為 0, 0)的橢圓。w 和 h 分別指定了橢圓的寬度和高度,橢圓的顏色由 color 指定。
imagefilledellipse -- 畫一橢圓并填充
語法:bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() 在 image 所代表的圖像中以 cx,cy(圖像左上角為 0, 0)為中心畫一個橢圓。w 和 h 分別指定了橢圓的寬和高。橢圓用 color 顏色填充。如果成功則返回 TRUE,失敗則返回 FALSE。

imagearc -- 畫橢圓弧
bool imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color )
imagearc() 以 cx,cy(圖像左上角為 0, 0)為中心在 image 所代表的圖像中畫一個橢圓弧。w 和 h 分別指定了橢圓的寬度和高度,起始和結束點以 s 和 e 參數以角度指定。0°位于三點鐘位置,以順時針方向繪畫。
imagefilledarc -- 畫一橢圓弧且填充
bool imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
imagefilledarc() 在 image 所代表的圖像中以 cx,cy(圖像左上角為 0, 0)畫一橢圓弧。如果成功則返回 TRUE,失敗則返回 FALSE。w 和 h 分別指定了橢圓的寬和高,s 和 e 參數以角度指定了起始和結束點。style 可以是下列值按位或(OR)后的值:
IMG_ARC_PIE IMG_ARC_CHORD
IMG_ARC_NOFILL IMG_ARC_EDGED

imagestring -- 水平地畫一行字符串
語法:bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() 用 col 顏色將字符串 s 畫到 image 所代表的圖像的 x,y 坐標處(這是字符串左上角坐標,整幅圖像的左上角為 0,0)。如果 font 是 1,2,3,4 或 5,則使用內置字體。
imagestringup -- 垂直地畫一行字符串
語法:bool imagestringup ( resource image, int font, int x, int y, string s, int col )
imagestring()用 col 顏色將字符串 s 垂直地畫到 image 所代表的圖像的 x, y 座標處(圖像的左上角為 0, 0)。如果 font 是 1,2,3,4 或 5,則使用內置字體。

imagechar -- 水平地畫一個字符
語法:bool imagechar ( resource image, int font, int x, int y, string c, int color )
imagechar() 將字符串 c 的第一個字符畫在 image 指定的圖像中,其左上角位于 x,y(圖像左上角為 0, 0),顏色為 color。如果 font 是 1,2,3,4 或 5,則使用內置的字體(更大的數字對應于更大的字體)。
imagecharup -- 垂直地畫一個字符
語法:bool imagecharup ( resource image, int font, int x, int y, string c, int color )
imagecharup() 將字符 c 垂直地畫在 image 指定的圖像上,位于 x,y(圖像左上角為 0, 0),顏色為 color。如果 font 為 1,2,3,4 或 5,則使用內置的字體。
imagettftext -- 用 TrueType 字體向圖像寫入文本
語法 :array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )

<?php
    $im = imagecreatetruecolor(400, 30);     //創建400x300像素大小的畫布
    $white = imagecolorallocate($im, 255, 255, 255);  //創建白色
    $grey = imagecolorallocate($im, 128, 128, 128);   //創建灰色
    $black = imagecolorallocate($im, 0, 0, 0);           //創建黑色
    imagefilledrectangle($im, 0, 0, 399, 29, $white);     //使用白色作為背景
     //如果有中文輸出,需要將其轉碼,轉換為UTF-8的字符串才可以直接傳遞
    $text=iconv("GB2312", "UTF-8", "LAMP兄弟連--無兄弟,不編程!");
    $font = ‘simsun.ttc’;   //指定字體,將系統中對應字體復制到當前目錄下
    imagettftext($im, 20, 0, 12, 21, $grey, $font, $text);   //輸出灰色字串作為陰影
    imagettftext($im, 20, 0, 10, 20, $black, $font, $text);  //輸出一個黑色的字符串
    header("Content-type: image/png");  //通知瀏覽器將輸出格式為PNG的圖像
    imagepng($im);  //向瀏覽器中輸出PNG格式的圖像
    imagedestroy($im);      //銷毀資源,釋放內存占用的空間

demo.html

<img src="test.php" />

test.php

<?php
    //1 創建資源(畫布的大小)
    $img = imagecreatetruecolor(200, 200);  
    //設置畫布的顏色
    $white =  imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
    $red =  imagecolorallocate($img, 255, 0, 0);
    $blue =  imagecolorallocate($img, 0, 0, 0XFF);
    $pink =  imagecolorallocate($img, 0XFF, 0, 0XFF);
    $green =  imagecolorallocate($img, 0, 0xFF, 0);
    
    
    imagefill($img, 0, 0, $white);
    //2. 制作各種顏色
    
    imageline($img, 0,0, 200,200, $blue);
    imageline($img, 200, 0, 0, 200, $red);


    //畫矩形
    imagerectangle($img, 50, 50, 150, 150, $pink);
    imagefilledrectangle($img, 75,75, 125,125, $blue);
    
    //畫圓
    imageellipse($img, 50, 50, 100, 100, $red);
    imagefilledellipse($img, 150, 150, 100, 100, $red);


    //畫弧形
    imagearc($img, 150, 50, 100, 100,  -90, 0, $blue);


    //畫一個字符串
    imagestring($img, 5, 50, 150, "hello world", $blue);
    imagestringup($img, 5, 50, 150, "hello world", $blue);

    //3. 畫出各種圖形,和寫(畫出)字
    imagettftext($img, 30, 0, 10, 100, $green, "./simkai.ttf", "妹子漂亮嗎?");
    imagettftext($img, 30, 0, 13, 103, $red, "./simkai.ttf", "妹子漂亮嗎?");

    //4保存,或輸出給瀏覽, 寫第二個參數就是保存
    header("Content-Type:images/gif");

    imagegif($img);

    //5. 釋放資源
    imagedestroy($img);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • PHP中GD庫的使用 GD簡介 PHP 不僅限于只產生 HTML 的輸出,還可以創建及操作多種不同格式的圖像文件。...
    dptms閱讀 1,030評論 0 2
  • 本文講解常用GD函數的應用 1.一個簡單的圖像 我們先看一個例子: 運行結果: 這段代碼中我們用了一下幾個函數: ...
    老馬的春天閱讀 1,573評論 2 8
  • 背景 驗證碼就是把一串隨機產品的數字動態生成一幅圖片,再加上干擾元素。此時用戶可以通過肉眼能識別里面的數字或者字符...
    dy2903閱讀 2,138評論 0 7
  • pygame圖形接口 使用pygame.image模塊,可以對圖像進行讀取和保存。 使用pygame.image....
    sssally92閱讀 17,692評論 1 25
  • 舊福印上壓了新福, 黏著油垢的鍋被刷洗了干凈, 鍋里的雞和上次的雞, 一個走了一生另一個走了十二年, 一個去遠足另...
    葡萄美酒閱讀 252評論 0 0