php影像處理函數實例大全及php驗證碼

WBOY
發布: 2016-07-25 08:51:45
原創
781 人瀏覽過
    1. //準備畫布

    2. $im = imagecreatetruecolor(500, 300); > >

      //準備塗料

    3. $black = imagecolorallocate($im, 0, 0, 0);
    4. $white = imagecolorallocate($im, 255, 255, 255) ;

    5. //背景填成黑色

    6. imagefill($im,0,0, $black);
    7. //畫一個長方形,填充成白色

    8. imagefilledellipse($im, 258, 151, 200, 200, $white);
    9. //輸出瀏覽器或儲存起來
    10. header("content-type:image/png");
    11. //輸出圖片
    12. imagepng($im);
    13. //關閉畫布

    14. imagedestroy($im);
    15. ?>
    複製程式碼
    php圖片處理函數 1,數學函數 2,圖片處理函數

    數學函數: 1,max(); 2,min(); 3,mt_rand();隨機取一個數字

    1. echomt_rand(1,5);
    2. ?>
    複製程式碼複製程式碼複製程式碼
    複製程式碼

    複製碼

    mt_rand隨機取一個值
    1. //隨機從一個陣列中取一個值

    2. $arr = array("a ","b","c","d","e");
    3. $rkey = mt_rand(0,count($arr)-1);

    4. echo $arr[$rkey];

    ?>
    複製程式碼

    4.ceil() ;天花板 5.floor(); 6.round();四捨五入
    1. echo ceil(2.4);//3

    2. echo floor(2.4);// 2
    3. echo round(2.4);//2
    4. echo round(2.6);//3

    ?>

    複製碼

    6.pi(); //π 取圓周率
    1. echo(pi());
    echo M_PI;
    ?> echo M_PI;
    ?>

    >複製程式碼

    圖片處理函數使用場景 1.驗證碼 2.縮放 3.裁剪 4.水印
      php中穿件影像的五個步驟 1.準備畫布 2.準備塗料 3.在畫布上畫圖像或文字 4.輸出最終影像或曹村最終影像 5.釋放畫布資源
    1. 例:
    2. //1.準備畫布

    3. $im = imagecreatetruecolor(500,300); //2.準備塗料
    4. $black = imagecolorallocate($im, 0, 0, 0);
    5. $white = imagecolorallocate($im, 255, 255, 255);
    6. //3.在畫布上畫圖像或文字
    7. //如果不填滿背景,預設是黑色
    8. imageellipse($im,258,151,200,200,$white);
    9. //4.輸出最終影像或儲存最終影像

    10. header("content-type:image/png");
    imagepng($im);
    //5.釋放畫布資源imagedestroy ($im); ?>

    複製程式碼

    繪製影像: imagefill(); imagesetpixel();//畫像素點 imageline();//畫線 imagerectangle();//畫一個長方形 imagepolygon();//畫一個多邊形 imageellipse();//畫一個橢圓 imageare();畫一個圓弧 imagechar();//水平的畫一個字符 imagestring();//水平的畫一行字串
    1. 例:
    2. //畫線

    3. //1.準備畫布
    4. $im = imagecreate (500,300);
    5. //2.準備塗料
    6. $black = imagecolorallocate($im, 0, 0, 0);
    7. $white = imagecolorallocate($im, 255, 255, 255); /p>
    8. //3.在畫布上畫圖像或文字

    9. //如果不填滿背景,預設是黑色
    10. imageline($im,0,0,500,300,$white);
    11. imageline($im,0,300,500,0,$white);
    12. imageline($im,0,150,500,150,$white);
    13. imageline($im,250,0,250,300,$p. 🎜>

      //4.輸出最終影像或儲存最終影像

    14. header("content-type:image/png");
    15. imagepng($im);
    //5.釋放畫布資源
    imagedestroy($im); ?>
    複製程式碼

    例:

    1. //添加干擾素

    2. //1.準備畫布
    3. $im = imagecreatetruecolor(500,300);
    4. //2.準備塗料
    5. $black = imagecolorallocate($im, 0, 0, 0);
    6. $white = imagecolorallocate($im, 255, 255, 255);
    7. //3.在畫布上畫圖像或文字

    8. //產生隨機的點
    9. for ($i=0; $i
    10. imagesetpixel($im,mt_rand(0,500),mt_rand(0,300),$white);

    11. }

    12. //產生隨機的線 p>
    13. for ($j=0; $j imageline($im, mt_rand(0,500), mt_rand(0,300), mt_rand(0,500), mt_rand(0,300 ), $white);

    14. }//4.輸出最終影像或儲存最終影像
    15. header("content-type:image/png");
    16. imagepng($im);
    17. / /5.釋放畫布資源
    18. imagedestroy($im);
    19. ?>
    複製程式碼

    範例:

    1. //畫長方形:

    2. //1.準備畫布
    3. $im = imagecreatetruecolor(500,300);
    4. //2.準備塗料
    5. $black = imagecolorallocate($im, 0, 0, 0);
    6. $white = imagecolorallocate($im, 255, 255, 255);
    7. //3.在畫布上畫圖像或文字

    8. imagerectangle($im, 20, 20, 480, 280, $white);//
    9. imagefilledrectangle($im , 20, 20, 480, 280, $white);//填滿
    10. //4.輸出最終影像或儲存最終影像

    11. header("content-type:image/png ");
    12. imagepng($im);
    13. //5.釋放畫布資源
    14. imagedestroy($im);
    15. ?>
    複製程式碼

    範例:

    1. //imagepolygon 畫多邊形_畫三角形

    2. //1.準備畫布
    3. $im = imagecreatetruecolor(500,300);
    4. //2.準備塗料
    5. $black = imagecolorallocate($im, 0, 0, 0);
    6. $white = imagecolorallocate($im, 25,
    7. $white = imagecolorallocate($im, 25, 255,5, 25 255);
    8. //3.在畫布上畫圖像或文字

    9. $arr = array(
    10. 250,20,
    11. 60,240,
    12. 440,240
    13. 60,240,
    14. 440,240
    15. );
    16. imagepolygon($im, $arr, 3, $white);
    17. //4.輸出最終影像或儲存最終影像

    18. header("content-type :image/png");
    19. imagepng($im);
    20. //5.釋放畫布資源
    imagedestroy($im);
    ?>

    複製程式碼

    例子,畫一個3D餅狀圖
    1. //1.準備畫布

    2. $im = imagecreatetruecolor(500,300); //2.準備塗料
    3. $black = imagecolorallocate($im, 0, 0, 0);
    4. $red = imagecolorallocate($im, 255, 0, 0);
    5. $grayred = imagecolorallocate( $im, 255, 100, 100);
    6. $green = imagecolorallocate($im, 0, 255, 0);
    7. $blue = imagecolorallocate($im, 0, 0, 255);
    8. $ gray = imagecolorallocate($im, 200, 200, 200);
    9. $white = imagecolorallocate($im, 255, 255, 255);
    10. //3.圖像或文字

    11. for ($i=0; $i imagefilledarc($im, 250, 150 $i, 200, 200, 0, 70, $gray,IMG_ARC_PIE);
    12. imagefilledarc($im, 250, 150 $i, 200, 200, 70, 190, $grayred,IMG_ARC_PIE);
    13. imagefilledarc($im, 250, 150 $i, 200, 20,209 $green,IMG_ARC_PIE);
    14. imagefilledarc($im, 250, 150 $i, 200, 200, 270, 360, $blue,IMG_ARC_PIE); imagefilledarc($im, 250, 150, 200, 200, 70, 190, $red); imagefilledarc($im, 250, 150, 200, 200, 190, 270, $green,IMG_ARC_PIE);
    15. imagefilledarc($im, 250, 150, 200, 200, 270,700, 150, 200, 200, 270,360,70,030 /p>
    16. //4.輸出最終影像或儲存最終影像

    17. header("content-type:image/png");
    18. imagepng($im);
    19. // 5.釋放畫布資源
    20. imagedestroy($im);
    21. ?>
    複製程式碼

    例:

    1. //寫文字:

    2. //1.準備畫布
    3. $im = imagecreatetruecolor(500,300);
    4. //2.準備塗料
    5. $black = imagecolorallocate($im, 0, 0, 0);
    6. $red = imagecolorallocate($im, 255, 0, 00);
    7. $grayred = imagecolorallocate($im, 255, 100, 100);
    8. $green = imagecolorallocate($im, 0, 255, 0);
    9. $blue = imagecolorallocate( , 255);
    10. $gray = imagecolorallocate($im, 200, 200, 200);
    11. $white = imagecolorallocate($im, 255, 255, 255); imagecolorallocate($im, 255, 255, 255);
    12. //3.在畫布上畫圖像或文字
    13. $str= "PHP is very much";

    14. imagestring($im, 5, 260, 160, $str, $green);

    15. //4.輸出最終影像或儲存最終影像
    16. header("content-type:image/png");
    17. imagepng($im);
    18. //5.釋放畫布資源
    19. imagedestroy($im);
    20. ?>
    複製代碼

    例子:

    1. //寫單字:

    2. //1.準備畫布
    3. $im>//1.準備畫布
    4. $im = imagecreatetruecolor(500,300);
    5. //2.準備塗料
    6. $black = imagecolorallocate($im, 0, 0, 0);
    7. $red = imagecolorallocate($im, 255, 0, 000) ;
    8. $grayred = imagecolorallocate($im, 255, 100, 100);
    9. $green = imagecolorallocate($im, 0, 255, 0);
    10. $blue = imagecolorallocate 0, 255);
    11. $gray = imagecolorallocate($im, 200, 200, 200);
    12. $white = imagecolorallocate($im, 255, 255, 255); >//3.在畫布上畫圖像或文字
    13. $str= "P";

    14. imagechar($im, 5, 260, 160, $str, $green);

    15. //4.輸出最終影像或儲存最終影像
    16. header("content-type:image/png");
    17. imagepng($im);
    18. / /5.釋放畫布資源
    19. imagedestroy($im);
    20. ?>
    複製程式碼

    例子,
    1. //在圖片上寫字

    2. //1.準備畫布
    3. $im = imagecreatetruecolor(500,300);
    4. //2.準備塗料
    5. $black = imagecolorallocate($im, 0, 0, 0);
    6. $red = imagecolorallocate($im, 255, 0, 000) ;
    7. $grayred = imagecolorallocate($im, 255, 100, 100);
    8. $green = imagecolorallocate($im, 0, 255, 0);
    9. $blue = imagecolorallocate 0, 255);
    10. $gray = imagecolorallocate($im, 200, 200, 200);
    11. $white = imagecolorallocate($im, 255, 255, 255); >//3.在畫布上畫圖像或文字
    12. $str= "junzaivip";

    13. $file = "E:/PHP/fonts/SIMYOU.TTF";
    14. // $file = "fonts/SIMYOU.TTF";
    15. imagettftext($im, 50, 0, 100, 200, $green, $file, $str);

    16. //4.輸出最終影像或儲存最終影像

    17. header("content-type:image/png");
    18. imagepng($im);
    19. //5.釋放畫布資源
    20. imagedestroy($im);
    21. ?>
    複製程式碼

    PHP 驗證碼的設計

    1. //準備畫布

    2. $im = imagecreatetruecolor(100,50); //準備塗料
    3. $black = imagecolorallocate($im, 0, 0, 0);
    4. $gray = imagecolorallocate($im, 200, 200, 200);
    5. //填入背景

    6. imagefill($im, 0, 0, $gray);
    7. //文字座標

    8. $x = (100-4*20)/2 6 ;
    9. $y = (50-20)/2 20;
    10. //在畫布上畫圖像或文字

    11. //把三個陣列連結起來

    12. $strarr = array_merge(range(1, 9),range(a, z),range(A, Z));
    13. //打亂陣列

    14. shuffle($strarr);
    15. //array_slice:取陣列的前幾位

    16. //Join 將陣列變成字串,並且以第一個變數做分隔符號
    17. $str = join('',array_slice($strarr, 0,4));
    18. $file = "E:/PHP/fonts/msyh.ttf";

    19. // $file = "fonts/msyh.ttf";
    20. imagettftext($im, 20, 0, $x, $y, $black, $file, $str); p>

    21. //輸出最終影像或儲存最終影像

    22. header("content-type:image/png");
    23. imagepng($im);
    24. //釋放畫布資源
    25. imagedestroy($im);
    26. ?>
    複製程式碼

    php驗證碼設計:這裡牽涉到兩個頁:index.php & reg.php 說明:

    這個驗證碼版本只實現了驗證圖片的動態獲取 前台註冊頁面的驗證碼和產生圖片的驗證碼進行比較 驗證碼是由數字 小寫字母 大寫字母 隨機組成

    index.php//實現使用者的註冊

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