PHP合成圖片 --制作商品分享海報


 

 

 

不說廢話,直接上代碼

<?php

$data = [
    'title' =>'【2雙裝】秋冬棉拖條紋/格子【8色可選】',
    'price_market' => '¥34.9',
    'price_member' => '¥29.9',
    'goods_img' => 'img/bigImg.jpg',
    'new_img' => 'newimg/666_12.jpg'

];
$codeImg = "img/qrCode.png";

$newImg = createImg($data,$codeImg);
echo $newImg;
echo "<img src=".$newImg." />";

/**
 * 總生成圖片方法
 * @param $data        商品數據
 * @param $codeImg     二維碼
 * @return new      返回圖片image資源
 */
function createImg($data,$codeImg){
    $backImg = "img/background.jpg";
    $new =  $data['new_img'];
    $goods_img = $data['goods_img'];
    // 添加二維碼
    addPic($backImg,$codeImg,150,150,285,510,$new);
    // 添加產品
    addPic($new,$goods_img,400,400,25,100,$new);

    // 添加產品描述,對描述進行分行
    $theTitle = cn_row_substr($data['title'],2,11);
    addWord($theTitle[1],25,540,16,'black',$new);
    addWord($theTitle[2],25,565,16,'black',$new);

    // 添加價格1
    addWord('特價'.$data['price_market'],25,610,24,'red',$new);
    // 添加價格2
    addWord('原價'.$data['price_member'],25,640,18,'black',$new);

    return $new;
}

插入圖片、文字代碼

/**
 * 添加圖片
 * @param $path_base    原圖
 * @param $path_logo     添加圖
 * @param $imgWidth     添加圖寬
 * @param $imgHeight     添加圖高
 * @param $dst_x         在原圖寬x處添加
 * @param $dst_y         在原圖高y處添加
 * @param $new             生成圖
 * @return resource    返回圖片image資源
 */
function addPic($path_base,$path_logo,$imgWidth,$imgHeight,$dst_x,$dst_y,$new){

    $image_base = ImgInfo($path_base);
    $image_logo = ImgInfo($path_logo);

    imagecopyresampled($image_base, $image_logo, $dst_x, $dst_y, 0, 0,$imgWidth,$imgHeight,imagesx($image_logo), imagesy($image_logo));

    // 生成一個合並后的新圖
    imagejpeg($image_base,$new);
    // 載入新圖像資源
    $new_pic = imagecreatefromjpeg($new);
    // 生成寫入文字的的新圖
    imagejpeg($new_pic,$new);

}

/**
 * 添加文字
 * @param $str        要添加的文字
 * @param $posX     在寬x處添加
 * @param $poxY     在高y處添加
 * @param $font     字體大小
 * @param $color     字體顏色
 * @param $new         生成圖
 * @return resource    返回圖片image資源
 */
function addWord($str,$posX,$poxY,$font,$color,$new)
{
    $ori_img = $new;    //原圖
    $new_img = $new;    //生成水印后的圖片
    
    $s_original = ImgInfo($ori_img);
    $tilt = 0;    //文字的傾斜度
    $ImgColor = [                    //為一幅圖像分配顏色 
        'black' => imagecolorallocate($s_original,0,0,0),
        'red' => imagecolorallocate($s_original,255,0,0),
        ] ;
    
    imagettftext($s_original, $font, $tilt, $posX, $poxY, $ImgColor[$color], 'C:/Windows/Fonts/simfang.ttf', $str);    
    
    $loop = imagejpeg($s_original, $new_img);    //生成新的圖片(jpg格式)

}

調用工具方法

 

/**
 * 從圖片文件創建Image資源
 * @param $file 圖片文件,支持url
 * @return bool|resource    成功返回圖片image資源,失敗返回false
 */
function ImgInfo($img){
    if(preg_match('/http(s)?:\/\//',$img)){
        $fileSuffix = getNetworkImgType($img);
    }else{
        $fileSuffix = pathinfo($img, PATHINFO_EXTENSION);
    }

    if(!$fileSuffix) return false;

    switch ($fileSuffix){
        case 'jpeg':
            $theImage = @imagecreatefromjpeg($img);
            break;
        case 'jpg':
            $theImage = @imagecreatefromjpeg($img);
            break;
        case 'png':
            $theImage = @imagecreatefrompng($img);
            break;
        case 'gif':
            $theImage = @imagecreatefromgif($img);
            break;
        default:
            $theImage = @imagecreatefromstring(file_get_contents($img));
            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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM