美文网首页
PHP合成微信小程序分享调用的图片(类似今日头条小程序分享样式)

PHP合成微信小程序分享调用的图片(类似今日头条小程序分享样式)

作者: 科科分享 | 来源:发表于2018-04-26 15:26 被阅读0次
    php代码:
    // ==================== 合成图片 begin ====================
    $bg_width = 480;
    $bg_height = 384;
    $bg = imagecreatetruecolor($bg_width, $bg_height);
    $white = imagecolorallocate($bg, 255, 255, 255);
    // imagecolortransparent($bg, $white);
    imagefill($bg, 0, 0, $white);
    
    // 字体文件
    $font_file = 'SourceHanSansCN-Light.otf';
    $font_size = 14;
    
    // 绘制作者
    $title = ‘测试';
    
    $box = imagettfbbox($font_size, 0, $font_file, $title); // 取得使用 TrueType 字体的文本的范围 x-y
    $title_width = abs($box[2] - $box[0]); // 右下角x - 左下角x  = 计算出title占用的宽度
    
    $color = imagecolorallocate($bg, 192,192,192); // 灰色
    imagettftext($bg, $font_size, 0, 10, 26, $color, $font_file, $title);
    
    // 绘制发布时间
    $title = date("Y-m-d H:i", time());
    imagettftext($bg, $font_size, 0, $title_width+20, 26, $color, $font_file, $title);
    
    // 绘制一行描述
    $title = "景区-推荐 九寨沟:重建将探索“世遗”抢救...";
    $color = imagecolorallocate($bg, 56, 56, 56); // 灰色
    imagettftext($bg, $font_size+1, 0, 10, 66, $color, $font_file, $title);
            
    // 绘制第一张图  
    // $bg = imagecreatefromstring (  file_get_contents($bg)  ); //open picture source
    
    $item_imagessize = getimagesize('0208_b.png');
    $item_width = $item_imagessize[0];
    $item_height = $item_imagessize[1];
    
    // $thumb1 = imagecreatefrompng('0208_b.png');
    
    $precent = $item_height / $item_width;
    $thumb1 = imagecreatefromstring (  file_get_contents('0208_b.png')  );   //open picture source 
            
    //x轴430 y轴670 宽175高175
    imagecopyresampled ( $bg, $thumb1, 10, 86, 0, 0, $bg_width-20, ((($bg_width-20)*$item_height)/$item_width), $item_width, $item_height ); // 这里是背景图与二维码的合成
    
    $result_png = "share_pic.png"; // file name
    $file = $result_png; // 绘制完成的图片
    // header('content-type:image/png'); 
    imagepng ( $bg, $file ); //output picture
    imagedestroy($bg);  
    imagedestroy($thumb1);
    
    最终生成的图片:
    share_pic.png

    相关文章

      网友评论

          本文标题:PHP合成微信小程序分享调用的图片(类似今日头条小程序分享样式)

          本文链接:https://www.haomeiwen.com/subject/fexmlftx.html