Heim > Artikel > Backend-Entwicklung > 急生成图片的代码,改了半日不行,求高手了!
急~生成图片的代码,改了半天不行,求高手了!!
这个代码是直接将文字转换为图片的,然后默认生成的图片是黑色背景的。。
现在想求高手帮忙如何改为,背景不要色黑色,而是直接为同目录的.jpg图片作为背景呢?
也就是说,生成的图片背景为1.jpg,而不是现在的颜色背景?求如何改。。。研究半天不懂。。
<br><?php <br />function str_div($str, $width = 10){<br> $strArr = array();<br> $len = strlen($str);<br> $count = 0;<br> $flag = 0;<br> while($flag if(ord($str[$flag]) > 128){<br> $count += 1;<br> $flag += 3;<br> }<br> else{<br> $count += 0.5;<br> $flag += 1 ;<br> }<br> if($count >= $width){<br> $strArr[] = substr($str, 0, $flag);<br> $str = substr($str, $flag);<br> $len -= $flag;<br> $count = 0;<br> $flag = 0;<br> }<br> }<br> $strArr[] = $str;<br> return $strArr;<br>}<br><br>function str2rgb($str)<br>{<br> $color = array('red'=>0, 'green'=>0, 'blue'=>0);<br> $str = str_replace('#', '', $str);<br> $len = strlen($str);<br> if($len==6){<br> $arr=str_split($str,2);<br> $color['red'] = (int)base_convert($arr[0], 16, 10);<br> $color['green'] = (int)base_convert($arr[1], 16, 10);<br> $color['blue'] = (int)base_convert($arr[2], 16, 10);<br> return $color;<br> }<br> if($len==3){<br> $arr=str_split($str,1);<br> $color['red'] = (int)base_convert($arr[0].$arr[0], 16, 10);<br> $color['green'] = (int)base_convert($arr[1].$arr[1], 16, 10);<br> $color['blue'] = (int)base_convert($arr[2].$arr[2], 16, 10);<br> return $color;<br> }<br> return $color;<br>}<br><br>function makeimger($text = "内容获取失败...",$types,$ids){<br> $setStyle = '52A300'; #设置颜色,也可以开发为页面可选择并传递这个参数,用|格式<br> $haveBrLinker = ""; #超长使用分隔符<br> $fontFile = 'simfang.ttf'; #字体文件名,放font目录下,也可以开发为页面可选择并传递这个参数<br> $userStyle = explode('|', $setStyle); #分开颜色<br> $text = substr($text, 0, 1000); #截取前一万个字符<br> $text = iconv("GB2312", "UTF-8",$text); <br> $imgpath = "".$types."/"; #图片存放地址<br> if(!is_dir($imgpath)){ mkdir($imgpath); }<br> $imgfile = $imgpath . $ids . '.gif';<br><br> if(file_exists($imgfile))<br> {<br> return $imgfile; <br> }<br> else<br> {<br> //这里是边框宽度,可以前台传递参数<br> $paddingTop = 500;<br> $paddingLeft = 35;<br> $paddingBottom = 260;<br> $copyrightHeight = 0;<br> <br> $canvasWidth = 640;<br> $canvasHeight = 1136;<br> //$canvasHeight = $paddingTop + $paddingBottom + $copyrightHeight;<br> <br> $fontSize = 38;<br> $lineHeight = intval($fontSize * 1.8);<br> <br> $textArr = array();<br> $tempArr = explode("\n", trim($text));<br> $j = 0;<br> foreach($tempArr as $v){<br> $arr = str_div($v, 25);<br> $textArr[] = array_shift($arr);<br> foreach($arr as $v){<br> $textArr[] = $haveBrLinker . $v;<br> $j ++;<br> if($j > 100){ break; }<br> }<br> $j ++;<br> if($j > 100){ break; }<br> }<br> <br> $textLen = count($textArr);<br> <br> $canvasHeight = $lineHeight * $textLen + $canvasHeight;<br> $im = imagecreatetruecolor($canvasWidth, $canvasHeight); #定义画布<br> $colorArray = str2rgb($userStyle[1]);<br> imagefill($im, 0, 0, imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']));<br> <br> $colorArray = str2rgb('000000');<br> $colorLine = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);<br> $padding = 0;<br> $x1 = $y1 = $x4 = $y2 = $padding;<br> $x2 = $x3 = $canvasWidth - $padding - 1;<br> $y3 = $y4 = $canvasHeight - $padding - 1;<br> //可以开发为页面可选择并传递这个参数,选择是否显示边框以及颜色。<br> imageline($im, $x1, $y1, $x2, $y2, $colorLine);<br> imageline($im, $x2, $y2, $x3, $y3, $colorLine);<br> imageline($im, $x3, $y3, $x4, $y4, $colorLine);<br> imageline($im, $x4, $y4, $x1, $y1, $colorLine);<br><br> //字体路径,,也可以开发为页面可选择并传递这个参数<br> $fontStyle = 'font/' . $fontFile;<br> if(!is_file($fontStyle)){<br> exit('请先选择字体文件哦!');<br> }<br> <br> //写入四个随即数字<br> $colorArray = str2rgb($userStyle[0]);<br> $fontColor = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);<br> <br> foreach($textArr as $k=>$text){<br> $offset = $paddingTop + $lineHeight * ($k + 1) - intval(($lineHeight-$fontSize) / 2);<br> imagettftext($im, $fontSize, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);<br> }<br> <br> $fontColor = imagecolorallocate($im, 0, 0, 0);<br> $offset += 18;<br> $text = '----------------------------------------------------------------------------------------------';<br> imagettftext($im, 10, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);<div class="clear"> </div>