国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > php教程 > php手冊 > 正文

php原生圖片合成和文字生成圖片

不言
發(fā)布: 2018-05-26 13:36:54
原創(chuàng)
10058人瀏覽過

之前有一個我寫的帖子是可以拿到遠程地址的圖片保存本地的
前段時間搞的很火的朋友圈標簽,可以使用下面僅僅三個自定義函數即可合成,具體參數石頭已經給大家寫出,有需要改進的地方請直接留言!

/**
* a.合成圖片信息 復制一張圖片的矩形區(qū)域到另外一張圖片的矩形區(qū)域
* @param  [type] $bg_image  [目標圖]
* @param  [type] $sub_image [被添加圖]
* @param  [type] $add_x     [目標圖x坐標位置]
* @param  [type] $add_y     [目標圖y坐標位置]
* @param  [type] $add_w     [目標圖寬度區(qū)域]
* @param  [type] $add_h     [目標圖高度區(qū)域]
* @param  [type] $out_image [輸出圖路徑]
* @return [type]            [description]
*/
function image_copy_image($bg_image,$sub_image,$add_x,$add_y,$add_w,$add_h,$out_image){
if($sub_image){
$bg_image_c = imagecreatefromstring(file_get_contents($bg_image));
$sub_image_c = imagecreatefromstring(file_get_contents($sub_image));
imagecopyresampled($bg_image_c, $sub_image_c, $add_x, $add_y, 0, 0, $add_w, $add_h, imagesx($sub_image_c), imagesy($sub_image_c));
//保存到out_image
imagejpeg($bg_image_c, $out_image, 80);
imagedestroy($sub_image_c);
imagedestroy($bg_image_c);
}

}
/**
* b.生成文字圖片并插入廣告圖中
* @param  [type] $filename    [背景路徑]
* @param  [type] $text        [文字內容]
* @param  [type] $font        [文字大小]
* @param  [type] $size        [文字畫布的寬]
* @param  [type] $width_f     [文字顏色]
* @param  [type] $red         [紅]
* @param  [type] $grn	  [綠]
* @param [type] $blu          [藍]
*/
function create_text($filename,$text,$font,$size,$width_f,$red,$grn,$blu){
$rot  = 0; // 旋轉角度
$width = 0; //寬度
$height = 0; //高度
$offset_x = 0; //x偏移
$offset_y = 0; //y偏移
$bounds = array();
$text = autowrap($size, 0, $font, $text,$width_f); // 自動換行處理
/** [字體大小] [角度] [字體名稱] [字符串] [預設寬度] */
// 確定邊框高度.
$bounds = ImageTTFBBox($size, $rot, $font, "W");
if ($rot < 0) {
$font_height = abs($bounds[7]-$bounds[1]);
} else if ($rot > 0) {
$font_height = abs($bounds[1]-$bounds[7]);
} else {
$font_height = abs($bounds[7]-$bounds[1]);
}
// 確定邊框高度.
$bounds = ImageTTFBBox($size, $rot, $font, $text);
if ($rot < 0) {
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;
} else if ($rot > 0) {
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);
} else {
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;
$offset_x = 0;
}
$bg = imagecreatetruecolor($width + 20,$height + 20); // 創(chuàng)建畫布
$color=imagecolorallocatealpha($bg , 0 , 0 , 0 ,127);//拾取一個完全透明的顏色
imagealphablending($bg ,false);//關閉混合模式,以便透明顏色能覆蓋原畫布
imagefill($bg , 0 , 0, $color);//填充
imagesavealpha($bg ,true);//設置保存PNG時保留透明通道信息
$textImg = imagecolorallocate($bg, $red, $grn, $blu); // 創(chuàng)建白色
ImageTTFText($bg, $size, 0, 10, $size + 10, $textImg, $font, $text);
imagepng($bg,$filename);
}
/**
* 文字自動換行
* @param  [type] $fontsize    [字體大小]
* @param  [type] $angle       [角度]
* @param  [type] $fontface    [字體名稱]
* @param  [type] $string      [字符串]
* @param  [type] $width       [預設寬度]
*/
function autowrap($fontsize, $angle, $fontface, $string, $width) {
$content = "";
// 將字符串拆分成一個個單字 保存到數組 letter 中
preg_match_all("/./u", $string, $arr);
$letter = $arr[0];
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
// 判斷拼接后的字符串是否超過預設的寬度
if (($testbox[2] > $width) && ($content !== "")) {
$content .= PHP_EOL;
}
$content .= $l;
}
return $content;
}
登錄后復制

PHP速學教程(入門到精通)
PHP速學教程(入門到精通)

PHP怎么學習?PHP怎么入門?PHP在哪學?PHP怎么學才快?不用擔心,這里為大家提供了PHP速學教程(入門到精通),有需要的小伙伴保存下載就能學習啦!

下載
來源:php中文網
本文內容由網友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現有涉嫌抄襲侵權的內容,請聯系admin@php.cn
作者最新文章
最新問題
開源免費商場系統廣告
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網:公益在線php培訓,幫助PHP學習者快速成長!
關注服務號 技術交流群
PHP中文網訂閱號
每天精選資源文章推送
PHP中文網APP
隨時隨地碎片化學習
PHP中文網抖音號
發(fā)現有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號