PHP拼图+画线+图片文字自动换行[作品]

最近网站又要改版,将会员的评酒以图片形式发到绑定的微博上。真是难为我呀,很少整图片处理的程序。还好老天对我不薄。让我给调出来了。

最终效果:


接招吧:

<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/cls_image.php');
$goods_id = 177;
$sql = "select g.goods_name, g.original_img, gt.taster_name, gt.taster_career, gt.taster_xuanyan, gt.taster_pic1 from goods as g, goods_taster as gt, goods_taster_rank as gtr where g.goods_id=gtr.goods_id and gtr.taster_id=gt.id and g.goods_id=".$goods_id;
$goods_taster = $db->getRow($sql);

$sql = "select * from goods_rank where goods_id=".$goods_id." and user_id=".(empty($user_id) ? 0 : $user_id);
$goods_rank = $db->getRow($sql);

$sql = "select * from goods_taster_rank where goods_id=".$goods_id;
$goods_t_rank = $db->getRow($sql);
define('IMG_MB', ROOT_PATH . '/temp/mb.png');
define('FONT',   ROOT_PATH . '/temp/simsun.ttc');
define('TMP_DIR', ROOT_PATH . '/temp/wbimg/');
$clsimg   = new cls_image;
$goodsimg = $clsimg->make_thumb($goods_taster['original_img'], 420, 180, ROOT_PATH . '/temp/');
$im1 = imagecreatefromstring(file_get_contents(IMG_MB)); //大图
$im_taster = imagecreatefromstring(file_get_contents(ROOT_PATH . '/' . $goods_taster['taster_pic1'])); //小图
$im_goods = imagecreatefromstring(file_get_contents(ROOT_PATH . '/' . $goodsimg)); //小图
//goods
imagecopymerge($im1, $im_goods, 0, 62, 0, 0, 420, 180, 80);
//taster
imagecopymerge($im1, $im_taster, 16, 256, 0, 0, 90, 108, 80);
//添加字体
$strcolor = 0x00000000;
$color_xy = 0x00727171;
$color_gc = 0x00036EB8;
imagettftext($im1, 9,  0, 115, 265, $strcolor, FONT, $goods_taster['taster_name']);
imagettftext($im1, 10, 0, 115, 285, $strcolor, FONT, $goods_taster['taster_career']);
$xuanyuan = autowrap(10, 0, FONT, $goods_taster['taster_xuanyan'], 290);
imagettftext($im1, 10, 0, 115, 330, $color_xy, FONT, $xuanyuan);
$gc  = autowrap(10, 0, FONT, $goods_t_rank['goods_comment'], 375);
imagettftext($im1, 10, 0, 16,  406, $color_gc, FONT, $gc);

//画线
$line_hy  = 0x0066C7C2;
$line_pjs = 0x0068A8D4;
$rate     = 100 * 0.65;
$line_hy_height = 8;
$line_pjs_height= 3;
//外观
imagefilledrectangle ($im1, 51, 504, 51 + ($goods_rank['qingche'] * $rate),   504 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 512, 51 + ($goods_t_rank['qingche'] * $rate), 512 + $line_pjs_height, $line_pjs);
imagefilledrectangle ($im1, 51, 532, 51 + ($goods_rank['sedu'] * $rate),   532 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 540, 51 + ($goods_t_rank['sedu'] * $rate), 540 + $line_pjs_height, $line_pjs);
//香气
imagefilledrectangle ($im1, 51, 599, 51 + ($goods_rank['fenfang'] * $rate),   599 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 607, 51 + ($goods_t_rank['fenfang'] * $rate), 607 + $line_pjs_height, $line_pjs);
imagefilledrectangle ($im1, 51, 627, 51 + ($goods_rank['cunxiang'] * $rate),   627 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 635, 51 + ($goods_t_rank['cunxiang'] * $rate), 635 + $line_pjs_height, $line_pjs);
//口感
imagefilledrectangle ($im1, 51, 695, 51 + ($goods_rank['jiuti'] * $rate),   695 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 703, 51 + ($goods_t_rank['jiuti'] * $rate), 703 + $line_pjs_height, $line_pjs);
imagefilledrectangle ($im1, 51, 722, 51 + ($goods_rank['danning'] * $rate),   722 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 730, 51 + ($goods_t_rank['danning'] * $rate), 730 + $line_pjs_height, $line_pjs);
imagefilledrectangle ($im1, 51, 747, 51 + ($goods_rank['suandu'] * $rate),   747 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 755, 51 + ($goods_t_rank['suandu'] * $rate), 755 + $line_pjs_height, $line_pjs);
imagefilledrectangle ($im1, 51, 772, 51 + ($goods_rank['pingheng'] * $rate),   772 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 780, 51 + ($goods_t_rank['pingheng'] * $rate), 780 + $line_pjs_height, $line_pjs);
imagefilledrectangle ($im1, 51, 800, 51 + ($goods_rank['yuyun'] * $rate),   800 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 808, 51 + ($goods_t_rank['yuyun'] * $rate), 808 + $line_pjs_height, $line_pjs);
//参考
imagefilledrectangle ($im1, 51, 862, 51 + ($goods_rank['tiandu'] * $rate),   862 + $line_hy_height,   $line_hy);
imagefilledrectangle ($im1, 51, 870, 51 + ($goods_t_rank['tiandu'] * $rate), 870 + $line_pjs_height, $line_pjs);
$wbimg = str_replace('//', '/', TMP_DIR . '/wbimg' . time().'_'. rand(1,100) .'.png');
imagepng($im1, $wbimg);
$wbimg = str_replace(ROOT_PATH, '', $wbimg);

function autowrap($fontsize, $angle, $fontface, $string, $width) {
    mb_internal_encoding("UTF-8"); // 设置编码
    // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
    $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);
        // 判断拼接后的字符串是否超过预设的宽度
        if (($testbox[2] > $width) && ($content !== "")) {
            $content .= "\n";
        }
        $content .= $l;
    }
    return $content;
}
?>
<img src="<?php echo $wbimg;?>">


要用到一张模板图,就是将效果图里动态的信息去空,还有就是用AI将里面的所有坐标全部标好,不然很难计算高度的。我得意的笑,我得意的笑……

300*300
 文章首页关于迷茫时代关于我写意人生
版权所有:迷茫时代 All rights reserved   
执行时间:0.00437 秒