1. 程式人生 > >php學習筆記4--php中GD2擴充套件庫的學習總結

php學習筆記4--php中GD2擴充套件庫的學習總結

    關於php GD2擴充套件庫,首先你要確定php環境已配置完成,才能進行接下來的操作,以下是我試驗成功的幾種題型:

GD2建立真彩圖像:

<?php
header("Content-type:image/png");
$height=300;
$width=300;
$im =imagecreatetruecolor($width,$height);//建立真彩色的影象
$white =imagecolorallocate($im ,255,255,255);
$blue =imagecolorallocate($im ,0,0,64);
imagefill($im,0,0,$blue);//淺藍色的背景
imageline($im,0,0,$width,$height,$white);//在影象上面畫一條白線
imagestring($im,4,80,150,"PHP",$white);//寫出白色的"PHP"

imagepng($im);
imagedestroy($im);
?>

(1)GD2畫圖之八卦的畫法:

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

$width=400;
$height=400;
$image=imagecreatetruecolor($width,$height);
$bgcolor=imagecolorallocate($image,255,153,0);
$black=imagecolorallocate($image,0,0,0);
$green=imagecolorallocate($image,0,255,0);
$white=imagecolorallocate($image,255,255,255);

imagefilledellipse($image,200,200,299,299,$black);
imagefilledarc($image,200,200,300,300,90,-90,$white,IMG_ARC_PIE);
imagefilledellipse($image,200,125,150,150,$black);
imagefilledellipse($image,200,275,150,150,$white);

imagefilledellipse($image,200,125,75,75,$white);
imagefilledellipse($image,200,275,75,75,$black);

imagestring($image,5,125,380,"http://www.baidu.com",$white);
imagepng($image);
imagedestroy($image);

?>

(2)GD2畫圖之濾鏡效果:

<?php
header('content-type:image/jpeg');

$logo = imagecreatefromjpeg('i.jpg');
$ww = imagesx($logo);
$hh = imagesy($logo);


//imagefilter($logo,IMG_FILTER_GRAYSCALE);
//imagejpeg($logo);
//imagedestroy($logo);

$i = imagecreatetruecolor($ww*2,$hh);
imagecopy($i,$logo,0,0,0,0,$ww,$hh);

imagefilter($logo,IMG_FILTER_COLORIZE,255,0,255);
//imagefilter($logo,IMG_FILTER_NEGATE);  //將影象中所有顏色反轉。
//imagefilter($logo,IMG_FILTER_MEAN_REMOVAL); //用平均移除法來達到輪廓效果。
//imagefilter($logo,IMG_FILTER_EDGEDETECT); //用邊緣檢測來突出影象的邊緣。
//imagefilter($logo,IMG_FILTER_GRAYSCALE); //調節灰度,將影象轉換為灰度的。
//imagefilter($logo,IMG_FILTER_EMBOSS);      //使影象浮雕化。
//imagefilter($logo,IMG_FILTER_SMOOTH,255);    //使影象更柔滑。用 arg1 設定柔滑級別。
imagecopy($i,$logo,$ww,0,0,0,$ww,$hh);

imagejpeg($i);
imagedestroy($i);

?>

(3)GD2畫圖之“logo圖片加到另一張圖片上”:

<?php
header("Content-type:image/png");
$image =imagecreatefromjpeg("img/s.jpg");
$watermark =imagecreatefrompng("img/2.png");
$width =imagesx($watermark);
$height = imagesy($watermark);
//將水印加到圖片左上角
imagecopyresampled($image,$watermark,0,0,0,0,$width,$height,$width,$height);
imagejpeg($image,"images/s_water.jpg",100);
imagejpeg($image);
imagedestroy($image);
?>

(4)GD2畫圖之“將一群圖片打上文字水印並另外生成一群不重名的圖片”:

<?php
$files = scandir("img/a/");
foreach($files as $v){
    if($v=='.' || $v=='..'){
        continue;    
    }
    $i = imagecreatefromjpeg('img/a/'.$v);
    $c = imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagettftext($i,15,0,10,20,$c,'Fonts/h.ttf','教育培訓版權所有');
    imagejpeg($i,'img/a/t_'.$v);
    imagedestroy($i);
}

?>

(5)GD2畫圖之“將漢字水印新增到圖片上”:

<?php
header("Content-type: image/png");
$image =imagecreatefromjpeg("images/i.jpg");
$pink = imagecolorallocate($image,255,255,255);
//$font_file 字型的路徑,視作業系統而定,可以是simhei.ttf(黑體)
//SIMKAI.TTF(楷體),SIMFANG.TTF(仿宋),SIMSUN.TTC(宋體&新宋體)等GD支援的中文字型
$font_file ="C:\WINDOWS\Fonts\msyhbd.ttf";
$str ="hello!中國 >_< ";
//$str =mb_convert_encoding($str,"GBK","UTF-8");
imagettftext($image,25,10,100,200,$pink,$font_file,$str);//設定字型顏色
imagejpeg($image,"images/i_text.jpg",100);//將帶有文字的圖片儲存到資料夾
imagejpeg($image);
imagedestroy($image);
?>

(6)GD2畫圖之“縮圖--寬度一定、高度按比例縮放”:

<?php
header('content-type:image/jpeg');

$logo = imagecreatefromjpeg('i.jpg');
$ww = imagesx($logo);
$hh = imagesy($logo);

$w = 150;
$h = $w/$ww * $hh;
$i = imagecreatetruecolor($w,$h);

imagecopyresampled($i,$logo,0,0,0,0,$w,$h,$ww,$hh);
imagejpeg($i,'t_i.jpg');
imagejpeg($i);
imagedestroy($i);

?>