php 圖片水印中文亂碼解決方法是要轉入一款中文字體就以了,如果你在生成水印有中文時,又沒載入相關的字體的話,那中文肯定會是亂碼的,如果是英語字母那載不載入都沒關系,代碼如下:
- $name = iconv("gb2312","utf-8",www.49028c.com php粉絲網);
- $font = 'simhei.ttf';//水印字體
- $im = imagecreatefromjpeg("test.jpg");//載入圖片
- $black = imagecolorallocate($im, 0, 0, 0);//設置顏色
- imagettftext($im, 12, 0, 320, 84, $black, $font, $name);//打印水印
- imagepng($im);//輸出圖片,如果要保存加個保存路徑和文件名,如imagepng($im,'test1.jpg'); //開源代碼Vevb.com
- imagedestroy($im);//清空緩存
下面來看一款生成水印文字函數,代碼如下:
- function str2pic ($string,$source,$destination="",$f,$fontsize=10,$shadowcolor="#ffffff",$f,$x=10,$y=10) {
- //header('content-type:image/png');
- $pi=pathinfo($source);
- $pie=$pi[extension];#獲取擴展名
- if(eregi("jpg|jpeg",$pie))$im=@imagecreatefromjpeg($source);
- if(eregi("gif",$pie))$im=@imagecreatefromgif($source);
- if(eregi("png",$pie))$im=@imagecreatefrompng($source);
- $col1=hex2dec($shadowcolor);#陰影顏色
- $col2=hex2dec($fontcolor);#字體顏色
- $col1=imagecolorallocate($im,$col1[0],$col1[1],$col1[2]);
- $col2=imagecolorallocate($im,$col2[0],$col2[1],$col2[2]);
- imagettftext($im,$fontsize,0,$y+1,$x+1,$col1,$fonturl,$string);
- imagettftext($im,$fontsize,0,$y,$x,$col2,$fonturl,$string);
- imagejpeg($im);
- if($destination)imagejpeg($im,$destination);
- imagedestroy($im);
- }
- function hex2dec($hexcolror) {#十六進制顏色轉換成10進制顏色
- preg_match_all("/([0-f]){2,2}/i",$hexcolror,$matches);
- if(count($matches[0])==3){
- $rr=hexdec($matches[0][0]);
- $gg=hexdec($matches[0][1]);
- $bb=hexdec($matches[0][2]);
- }
- return array($rr,$gg,$bb);
- }
- ?>
- <?php
- str2pic("[url=http://www.49028c.com]www.49028c.com","winter.jpg","winter2.jpg","simhei.ttf",10,"ffffff","330099",10,10[/url]);
- ?>
新聞熱點
疑難解答