PHP GD庫---之商詳合成分享圖片
阿新 • • 發佈:2018-12-18
$item_pic = 'img/item.jpg'; $qcode_pic = 'img/qcode.png'; $user_pic = 'img/user.jpeg'; $item_title = "鎮泰吹風機家用孕嬰負離子護"; $item_title2 = "發藍光大風力 家用"; $font_file = 'fonts/Microsoft_Yahei.ttf'; $item_price = '¥100.50'; $share_title = "來自鄒喆的分享"; //建立畫布 $percent = 0.5; $width = 400; $height = 700; $image = imagecreatetruecolor($width, $height); //建立背景顏色 $image_bg = imagecolorallocate($image, 255, 255, 255); //建立字型顏色 $image_text_bg = imagecolorallocate($image, 0, 0, 0); //建立價格顏色 $price_bg = imagecolorallocate($image, 255, 0, 0); //建立灰色顏色 $qcode_bg = imagecolorallocate($image, 128, 138, 135); //填充背景顏色 imagefill($image, 0, 0, $image_bg); //把商品圖片合成到畫布商品 //先把商品圖片換成圖片流 list($item_pic_width, $item_pic_height) = getimagesize($item_pic); $item_pic = imagecreatefromjpeg($item_pic); $new_item_width = $item_pic_width * $percent; $new_item_height = $item_pic_height * $percent; //加入商品 imagecopyresized($image, $item_pic, 0, 120, 0, 0, $new_item_width, $new_item_height, $item_pic_width, $item_pic_height); //加入商品標題 imagettftext($image, 12, 0, 10, 590, $image_text_bg, $font_file, $item_title); imagettftext($image, 12, 0, 11, 590, $image_text_bg, $font_file, $item_title); imagettftext($image, 12, 0, 25, 610, $image_text_bg, $font_file, $item_title2); imagettftext($image, 12, 0, 26, 610, $image_text_bg, $font_file, $item_title2); //加入商品價格 imagettftext($image, 20, 0, 30, 650, $price_bg, $font_file, $item_price); //加入推薦者頭像 list($user_pic_width, $user_pic_height) = getimagesize($user_pic); $user_pic = imagecreatefromjpeg($user_pic); $new_user_pic_width = $user_pic_width * $percent; $new_user_pic_height = $user_pic_height * $percent; imagecopyresampled($image, $user_pic, 80, 50, 0, 0, $new_user_pic_width, $new_user_pic_height, $user_pic_width, $user_pic_height); //加入推薦者 imagettftext($image, 12, 0, 150, 100, $image_text_bg, $font_file, $share_title); //加入二維碼 list($qcode_pic_width, $qcode_pic_height) = getimagesize($qcode_pic); $new_qcode_width = $qcode_pic_width; $new_qcode_height = $qcode_pic_height; $qcode_pic = imagecreatefrompng($qcode_pic); imagecopyresized($image, $qcode_pic, 260, 550, 0, 0, $new_qcode_width, $new_qcode_height, $qcode_pic_width, $qcode_pic_height); //加入二維碼文字 imagettftext($image, 10, 0, 277, 679, $qcode_bg, $font_file, '長按識別二維碼'); header("Content-type:image/jpeg"); imagejpeg($image); imagejpeg($image, 'img/0.jpg');
效果圖: