php產生文字圖片效果

來源:互聯網
上載者:User

最近看到php的GD功能,試著做了一個基本產生文字圖片效果的代碼:
測試環境:windows+apache+php
顯示文字圖片頁面:demo.php
<?php
$str = $_REQUEST['str'] ? $_REQUEST['str']:"暫無輸入";
//$str = "中華人民共和國";
$im = imagecreate(200,200);
$white = imagecolorallocate($im,0xFF,0xFF,0xFF);
imagecolortransparent($im,$white);  //imagecolortransparent() 設定具體某種顏色為透明色,若注釋

$black = imagecolorallocate($im,0x00,0x00,0x00);
imagefilledrectangle($im,50,50,150,150,$black);
imagestring($im,5,50,160,"happy every day",$black);

imagettftext($im,15,0,50,40,$black,"D:\windows\Fonts\simhei.ttf",$str); //字型設定部分linux和windows的路徑可能不同
header("Content-type:image/png");
imagepng($im);
?>

調用圖片頁面:demo2.php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php $var = "中華人民共和國"; ?>
<img src="demo.php?str=<?php echo $var?>">

文字圖片進階:
環境:Red Hat AS4.0+GD2.0.28+FreeType 2.1.9
目的:產生類似連結的即具有底線的藍色文字映像

//擷取文字並將其轉化成圖片
//$str = urldecode($_REQUEST['str']) ? urldecode($_REQUEST['str']):"暫無輸入";
//$str = "中華人民共和國";
$font_file = "/usr/share/fonts/zh_CN/TrueType/gbsn00lp.ttf";//字型設定部分linux的路徑
$text  = $str; //要顯示的字串
$font_size = 14; //字型大小
$arr = imagettfbbox($font_size,0,$font_file,$text); //確定會變化的字串的位置
$text_width = $arr[2]-$arr[0]; //字串文字框長度
$text_height = $arr[3]-$arr[5]; ////字串文字框高度

$im = imagecreate($text_width,$text_height);
$white = imagecolorallocate($im,0xFF,0xFF,0xFF);
imagecolortransparent($im,$white);  //imagecolortransparent() 設定具體某種顏色為透明色,若注釋
$blue = imagecolorallocate($im,0,0,255);

$arr = imagettftext($im,$font_size,0,0,$text_height-5,$blue,$font_file,$text);
imageline($im,$arr[0],$arr[1],$arr[2],$arr[3],$blue);
//imagettftext($im,12,0,0,20,$black,"c:\windows\Fonts\simhei.ttf",$url);//字型設定windows的路徑

header("Content-type:image/png");
imagepng($im);
imagedestroy($im);

文字浮水印:
<?php 
$pic=imagecreate(250,30); 
$black=imagecolorallocate($pic,0,0,0); 
$white=imagecolorallocate($pic,255,255,255); 
$font="C://WINDOWS//Fonts//simhei.ttf";  
$str ='php'.iconv('gb2312','utf-8','面對對象')." ovliverlin.cnblogs.com"; 
imagettftext($pic,10,0,10,20,$white,$font,$str);
 
header("Content-type: image/jpeg");
$filename='../src/images/photo.jpg';
$im=imagecreatefromjpeg($filename);
imagecopymerge($im,$pic,0,0,0,0,250,30,50);
imagejpeg($im);
?>  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.