圖片合成方法
/** * 生成宣傳海報 * @param array 參數,包括圖片和文字 * @param string $filename 生成海報文件名,不傳此參數則不生成文件,直接輸出圖片 * @return [type] [description] */ function imgTextMerge($config=array(),$filename=""){ //如果要看報什么錯,可以先注釋調這個header if(empty($filename)) header("content-type: image/png"); $imageDefault = array( 'left'=>0, 'top'=>0, 'right'=>0, 'bottom'=>0, 'width'=>100, 'height'=>100, 'opacity'=>100 ); $textDefault = array( 'text'=>'', 'left'=>0, 'top'=>0, 'fontSize'=>32, //字號 'fontColor'=>'255,255,255', //字體顏色 'angle'=>0, ); $background = $config['background'];//海報最底層得背景 //背景方法 $backgroundInfo = getimagesize($background); $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false); $background = $backgroundFun($background); $backgroundWidth = imagesx($background); //背景寬度 $backgroundHeight = imagesy($background); //背景高度 $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight); $color = imagecolorallocate($imageRes, 255, 255, 255); imagefill($imageRes, 0, 0, $color); // imageColorTransparent($imageRes, $color); //顏色透明 imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background)); //處理了圖片 if(!empty($config['image'])){ foreach ($config['image'] as $key => $val) { $val = array_merge($imageDefault,$val); $info = getimagesize($val['url']); $function = 'imagecreatefrom'.image_type_to_extension($info[2], false); if($val['stream']){ //如果傳的是字符串圖像流 $info = getimagesizefromstring($val['url']); $function = 'imagecreatefromstring'; } $res = $function($val['url']); $resWidth = $info[0]; $resHeight = $info[1]; //建立畫板 ,縮放圖片至指定尺寸 $canvas=imagecreatetruecolor($val['width'], $val['height']); imagefill($canvas, 0, 0, $color); //關鍵函數,參數(目標資源,源,目標資源的開始坐標x,y, 源資源的開始坐標x,y,目標資源的寬高w,h,源資源的寬高w,h) imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight); $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left']; $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top']; //放置圖像 imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,寬度,高度,透明度 } } //處理文字 if(!empty($config['text'])){ foreach ($config['text'] as $key => $val) { $val = array_merge($textDefault,$val); list($R,$G,$B) = explode(',', $val['fontColor']); $fontColor = imagecolorallocate($imageRes, $R, $G, $B); $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left']; $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top']; $text = $val['text']; $fontSize = $val['fontSize']; $angle = $val['angle']; $fontPath = $val['fontPath']; $width = $val['width']; $outStyle = $val['outStyle']; $suffix = isset($val['suffix'])?$val['suffix']:''; $text = $this->textWidth($fontSize,$angle,$fontPath,$text,$width,$outStyle,$suffix); imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$text); } } //生成圖片 if(!empty($filename)){ $res = imagejpeg($imageRes,$filename,100); //保存到本地 imagedestroy($imageRes); if(!$res) return false; return $filename; }else{ imagejpeg($imageRes); //在瀏覽器上顯示 imagedestroy($imageRes); } } /** * 文字超出寬度 * @param $fontsize //字體大小 * @param $angle //角度 * @param $fontface //字體名稱 * @param $string //字符串 * @param $width //預設寬度 * @param $outStyle //超出寬度操作類型(wrap:換行 hidden:隱藏 replace:替換) * @param $suffix //outStyle值為replace時,替換的內容 * @return string */ function textWidth($fontsize, $angle, $fontface, $string, $width, $outStyle = 'replace', $suffix = '...') { $content = ""; // 將字符串拆分成一個個單字 保存到數組 letter 中 for ($i=0;$i<mb_strlen($string);$i++) { $letter[] = mb_substr($string, $i, 1); } foreach ($letter as $l) { $teststr = $content.$l; $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr); // 判斷拼接后的字符串是否超過預設的寬度 $is_width = false;//是否超出寬度 if (($testbox[2] > $width) && ($content !== "") && $width != 0) { $is_width = true; } if ($outStyle == 'wrap'){//超出換行 if ($is_width) $content .= "\n"; }elseif ($outStyle == 'hidden'){//超出隱藏 if ($is_width) break; }elseif ($outStyle == 'replace'){//超出替換內容 if ($is_width){$content .= $suffix; break;} } $content .= $l; } return $content; }
調用圖片合成
public function test(){ $config = array( 'text'=>array( array( 'text'=>'標題', 'left'=>50, 'top'=>700, 'fontPath'=>'/font/siyuan.ttf', //字體文件 'fontSize'=>32, //字號 'fontColor'=>'0,0,0', //字體顏色 'angle'=>0, 'width'=>700,//文字超出寬度換行或隱藏,0不限制 'outStyle'=>'wrap',//超出寬度操作類型(wrap:換行 hidden:隱藏 replace:替換) ), array( 'text'=>'用戶名', 'left'=>50, 'top'=>910, 'fontPath'=>'/font/siyuan.ttf', //字體文件 'fontSize'=>28, //字號 'fontColor'=>'76,75,75', //字體顏色 'angle'=>0, 'width'=>450,//文字超出寬度換行或隱藏,0不限制 'outStyle'=>'replace',//超出寬度操作類型(wrap:換行 hidden:隱藏 replace:替換) 'suffix'=>'...',//outStyle值為replace時,替換的內容 ), ), 'image'=>array( array( 'url'=>'/public/logo.png', //圖片資源路徑 'left'=>50, 'top'=>110, 'right'=>0, 'bottom'=>0, 'width'=>700, 'height'=>490, 'stream'=>0, //圖片資源是否是字符串圖像流 'opacity'=>100 //不透明度 ), array( 'url'=>'/public/qrcode.png', 'left'=>530, 'top'=>870, 'right'=>0, 'bottom'=>0, 'width'=>220, 'height'=>220, 'stream'=>0, //圖片資源是否是字符串圖像流 'opacity'=>100 //不透明度 ), ), 'background'=>'/public/bg.png', ); $filename = '/uploads/'.time().'.jpg'; $this->imgTextMerge($config,$filename); }