大家好!http://micccn.gicp.net:10009 我这个多张照片、多段文字 生成 长图 的 php程序 运行后 怎么 没有生成 长图 share.png 参照的是 https://blog.csdn.net/Admin_root/article/details/86063611 的代码。或者您有可用的代码吗?

解决方案 »

  1.   


    // class qImgs{ /**
     * 分享图片生成
     * @param $gData  商品数据,array
     * @param $codeName 二维码图片
     * @param $fileName string 保存文件名,默认空则直接输入图片
     */
     
    function createSharePng($fileName = '',$img1 = '',$img2 = '',$img3 = '',$img4 = '',$txt1 = '',$txt2 = '',$txt3 = '',txt4 = ''){
    //创建画布
    $im = imagecreatetruecolor(618, 2600);
     
    //填充画布背景色
    $color = imagecolorallocate($im, 255, 255, 255);
    imagefill($im, 0, 0, $color);  
     
    //字体文件
    $font_file = "simhei.ttf";  // "code_png/msyh.ttf";
    // $font_file_bold = "code_png/msyh_bold.ttf";
     
    //设定字体的颜色
    $font_color_1 = ImageColorAllocate ($im, 140, 140, 140);
    $font_color_2 = ImageColorAllocate ($im, 28, 28, 28);
    $font_color_3 = ImageColorAllocate ($im, 129, 129, 129);
    $font_color_red = ImageColorAllocate ($im, 217, 45, 32);
    $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);  
     

    //温馨提示
    // imagettftext($im, 14,0, 100, 130, $font_color_1 ,$font_file, '温馨提示:喜欢长按图片识别二维码即可前往购买');  
     
        $height = 185; // 初始高度
    $imgSize = 618; // 图片统一尺寸

    //第1张图
    if($img1 && $img1 != ''){
    list($g_w,$g_h) = getimagesize($img1);
    $goodImg = createImageFromFile($img1);
    imagecopyresized($im, $goodImg, 0, $height, 0, 0, $imgSize, $imgSize, $g_w, $g_h);
    }  
     
    //第2张图
    if($img2 && $img2 != ''){
    $height = $height + $imgSize;
    list($code_w,$code_h) = getimagesize($img2);
    $codeImg = createImageFromFile($img2);
    imagecopyresized($im, $codeImg, 0, $height, 0, 0, $imgSize, $imgSize, $code_w, $code_h);
    }
     
    //第3张图
    if($img3 && $img3 != ''){
    $height = $height + $imgSize;
    list($code_w,$code_h) = getimagesize($img3);
    $codeImg = createImageFromFile($img3);
    imagecopyresized($im, $codeImg, 0, $height, 0, 0, $imgSize, $imgSize, $code_w, $code_h);
    }
     
    //第4张图
    if($img4 && $img4 != ''){
    $height = $height + $imgSize;
    list($code_w,$code_h) = getimagesize($img4);
    $codeImg = createImageFromFile($img4);
    imagecopyresized($im, $codeImg, 0, $height, 0, 0, $imgSize, $imgSize, $code_w, $code_h);
    }
     
        $fontSize = 25; // 字体尺寸
        $txtSize = 30; // 文字行高

    //第1段文字
    if($txt1 && $txt1 != ''){
    $height = $height + $txtSize;
        imagettftext($im, $fontSize, 0, $txtSize, $height, $font_color_red, $font_file, $txt1);
    }
     
    //第2段文字
    if($txt2 && $txt2 != ''){
    $height = $height + $txtSize;
        imagettftext($im, $fontSize, 0, $txtSize, $height, $font_color_red, $font_file, $txt2);
    }
     
    //第3段文字
    if($txt3 && $txt3 != ''){
    $height = $height + $txtSize;
        imagettftext($im, $fontSize, 0, $txtSize, $height, $font_color_red, $font_file, $txt3);
    }
     
    //第4段文字
    if($txt4 && $txt4 != ''){
    $height = $height + $txtSize;
        imagettftext($im, $fontSize, 0, $txtSize, $height, $font_color_red, $font_file, $txt4);
    }

    //输出图片
    if($fileName){
    imagepng ($im,$fileName);
    }else{
    Header("Content-Type: image/png");
    imagepng ($im);
    }
    //释放空间
    imagedestroy($im);
    imagedestroy($goodImg);
    imagedestroy($codeImg);
    }  
     
    /**
     * 从图片文件创建Image资源
     * @param $file 图片文件,支持url
     * @return bool|resource    成功返回图片image资源,失败返回false
     */
     
    function createImageFromFile($file){
    if(preg_match('/http(s)?:\/\//',$file)){
    $fileSuffix = getNetworkImgType($file);
    }else{
    $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
    }
     
    if(!$fileSuffix) return false;
    switch ($fileSuffix){
    case 'jpeg':
    $theImage = @imagecreatefromjpeg($file);
    break;
    case 'jpg':
    $theImage = @imagecreatefromjpeg($file);
    break;
    case 'png':
    $theImage = @imagecreatefrompng($file);
    break;
    case 'gif':
    $theImage = @imagecreatefromgif($file);
    break;
    default:
    $theImage = @imagecreatefromstring(file_get_contents($file));
    break;
    }
    return $theImage;
    }  
     
    /**
     * 获取网络图片类型
     * @param $url  网络图片url,支持不带后缀名url
     * @return bool
     */
     
    function getNetworkImgType($url){
     
    $ch = curl_init(); //初始化curl
     
    curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
     
    curl_setopt($ch, CURLOPT_NOBODY, 1);
     
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
     
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
     
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
     
    curl_exec($ch);//执行curl会话
     
    $http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
     
    curl_close($ch);//关闭资源连接
    if ($http_code['http_code'] == 200) {
    $theImgType = explode('/',$http_code['content_type']);
    if($theImgType[0] == 'image'){
    return $theImgType[1];
    }else{
    return false;
    }
    }else{
    return false;
    }
    }  
     
    /**
     * 分行连续截取字符串
     * @param $str  需要截取的字符串,UTF-8
     * @param int $row  截取的行数
     * @param int $number   每行截取的字数,中文长度
     * @param bool $suffix  最后行是否添加‘...’后缀
     * @return array    返回数组共$row个元素,下标1到$row
     */
     
    function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){
    $result = array();
    for ($r=1;$r<=$row;$r++){
    $result[$r] = '';
    }
     
    $str = trim($str);
    if(!$str) return $result;
    $theStrlen = strlen($str);
     
    //每行实际字节长度
    $oneRowNum = $number * 3;
    for($r=1;$r<=$row;$r++){
    if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){
    $result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';
    }else{
    $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);
    }
    if($theStrlen < $r * $oneRowNum) break;
    }
    return $result;

     
    /**
     * 按字节截取utf-8字符串
     * 识别汉字全角符号,全角中文3个字节,半角英文1个字节
     * @param $str  需要切取的字符串
     * @param $len  截取长度[字节]
     * @param int $start    截取开始位置,默认0
     * @return string
     */
     
    function mg_cn_substr($str,$len,$start = 0){
    $q_str = '';
    $q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len);
     
    //如果start不为起始位置,若起始位置为乱码就按照UTF-8编码获取新start
    if($start and json_encode(substr($str,$start,1)) === false){
    for($a=0;$a<3;$a++){
    $new_start = $start + $a;
    $m_str = substr($str,$new_start,3);
    if(json_encode($m_str) !== false) {
    $start = $new_start;
    break;
    }
    }
    }
     
    //切取内容
    for($i=$start;$i<$q_strlen;$i++){
    //ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符
    if(ord(substr($str,$i,1))>0xa0){
    $q_str .= substr($str,$i,3);
    $i+=2;
    }else{
    $q_str .= substr($str,$i,1);
    }
    }
    return $q_str;
    }
    // }
     
     
     
    //使用方法-------------------------------------------------
     
    $img1 = '1.jpg';
    $img2 = '2.jpg';
    $img1 = '3.jpg';
    $img2 = '4.jpg';$txt1 = 'abcdefghijklmnopqrstuvwxyz';
    $txt2 = '123';
    $txt3 = '0000000000000000';
    $txt4 = 'vvvvvvvvvvvvvvvv';
     
    //直接输出
    qImgs.createSharePng('',$img1,$img2,$img3,$img4,$txt1,$txt2,$txt3,txt4);
     
    //输出到图片
    qImgs.createSharePng('share.png',$img1,$img2,$img3,$img4,$txt1,$txt2,$txt3,txt4);