這個代碼是直接將文字轉換為圖片的,然後預設產生的圖片是黑色背景的。。
現在想求高手幫忙如何改為,背景不要色黑色,而是直接為同目錄的.jpg圖片作為背景呢?
也就是說,產生的圖片背景為1.jpg,而不是現在的顏色背景?求如何改。。。研究半天不懂。。
128){$count += 1;$flag += 3;}else{$count += 0.5;$flag += 1 ;}if($count >= $width){$strArr[] = substr($str, 0, $flag);$str = substr($str, $flag);$len -= $flag;$count = 0;$flag = 0;}}$strArr[] = $str;return $strArr;}function str2rgb($str){$color = array('red'=>0, 'green'=>0, 'blue'=>0);$str = str_replace('#', '', $str);$len = strlen($str);if($len==6){$arr=str_split($str,2);$color['red'] = (int)base_convert($arr[0], 16, 10);$color['green'] = (int)base_convert($arr[1], 16, 10);$color['blue'] = (int)base_convert($arr[2], 16, 10);return $color;}if($len==3){$arr=str_split($str,1);$color['red'] = (int)base_convert($arr[0].$arr[0], 16, 10);$color['green'] = (int)base_convert($arr[1].$arr[1], 16, 10);$color['blue'] = (int)base_convert($arr[2].$arr[2], 16, 10);return $color;}return $color;}function makeimger($text = "內容擷取失敗...",$types,$ids){$setStyle = '52A300'; #設定顏色,也可以開發為頁面可選擇並傳遞這個參數,用|格式$haveBrLinker = ""; #超長使用分隔字元$fontFile = 'simfang.ttf'; #字型檔名,放font目錄下,也可以開發為頁面可選擇並傳遞這個參數$userStyle = explode('|', $setStyle); #分開顏色$text = substr($text, 0, 1000); #截取前一萬個字元$text = iconv("GB2312", "UTF-8",$text); $imgpath = "".$types."/"; #圖片存放地址if(!is_dir($imgpath)){ mkdir($imgpath); }$imgfile = $imgpath . $ids . '.gif';if(file_exists($imgfile)){return $imgfile;}else{//這裡是邊框寬度,可以前台傳遞參數$paddingTop = 500;$paddingLeft = 35;$paddingBottom = 260;$copyrightHeight = 0;$canvasWidth = 640;$canvasHeight = 1136;//$canvasHeight = $paddingTop + $paddingBottom + $copyrightHeight;$fontSize = 38;$lineHeight = intval($fontSize * 1.8);$textArr = array();$tempArr = explode("\n", trim($text));$j = 0;foreach($tempArr as $v){$arr = str_div($v, 25);$textArr[] = array_shift($arr);foreach($arr as $v){$textArr[] = $haveBrLinker . $v;$j ++;if($j > 100){ break; }}$j ++;if($j > 100){ break; }}$textLen = count($textArr);$canvasHeight = $lineHeight * $textLen + $canvasHeight;$im = imagecreatetruecolor($canvasWidth, $canvasHeight); #定義畫布$colorArray = str2rgb($userStyle[1]);imagefill($im, 0, 0, imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']));$colorArray = str2rgb('000000');$colorLine = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);$padding = 0;$x1 = $y1 = $x4 = $y2 = $padding;$x2 = $x3 = $canvasWidth - $padding - 1;$y3 = $y4 = $canvasHeight - $padding - 1;//可以開發為頁面可選擇並傳遞這個參數,選擇是否顯示邊框以及顏色。imageline($im, $x1, $y1, $x2, $y2, $colorLine);imageline($im, $x2, $y2, $x3, $y3, $colorLine);imageline($im, $x3, $y3, $x4, $y4, $colorLine);imageline($im, $x4, $y4, $x1, $y1, $colorLine);//字型路徑,,也可以開發為頁面可選擇並傳遞這個參數$fontStyle = 'font/' . $fontFile;if(!is_file($fontStyle)){exit('請先選擇字型檔哦!');}//寫入四個隨即數字$colorArray = str2rgb($userStyle[0]);$fontColor = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);foreach($textArr as $k=>$text){$offset = $paddingTop + $lineHeight * ($k + 1) - intval(($lineHeight-$fontSize) / 2);imagettftext($im, $fontSize, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);}$fontColor = imagecolorallocate($im, 0, 0, 0);$offset += 18;$text = '----------------------------------------------------------------------------------------------';imagettftext($im, 10, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);$offset += 28;$fontColor = imagecolorallocate($im, 255, 0, 0);//也可以開發為頁面可選擇並傳遞這個參數,比如顯示的文字,以及是否顯示,顯示位置等。$text = '該圖片來自...';$text = iconv("GB2312", "UTF-8",$text); imagettftext($im, 14, 0, $paddingLeft + 160, $offset, $fontColor, $fontStyle, $text);imagegif($im, $imgfile);imagedestroy($im);//echo $imgfile; //exit($imgfile);}return $imgfile;}$gg=$_POST['gg'];if($gg==""){//這裡還可以增加字型顏色,背景顏色,邊框顏色,字型大小,邊框是否顯示,邊框寬度,選擇顯示的字型,以及最後行的著作權等... echo"請輸入內容!"; echo""; exit();}else{ $imghtml=makeimger($gg,"WeiBo",time().rand(1111,9999)); echo""; }?>
回複討論(解決方案)
$im = imagecreatetruecolor($canvasWidth, $canvasHeight); #定義畫布
改為
$im = imagecreatefromjpeg('xxx.jpg'); #定義畫布
$im = imagecreatetruecolor($canvasWidth, $canvasHeight); #定義畫布
改為
$im = imagecreatefromjpeg('xxx.jpg'); #定義畫布
謝謝~!另外有個問題,就是發現背景圖產生後的背景圖畫質很差,這個可以改它畫質好點的嘛?
不能!
你只是在圖片上寫字,不應該影響畫質的
不能!
你只是在圖片上寫字,不應該影響畫質的
另外再問一個問題。$_POST['gg']所產生的文字,能不能弄有背影效果?或者外光暈效果?
不能!
你只是在圖片上寫字,不應該影響畫質的
版主。再請教下。。產生的字型能不能讓它置中?現在是用 paddingleft控制的。。想弄它左右自動置中。。可以嗎?如何改?
座標平移還記得嗎?