php圖片放大合成加入字體操作示例介紹
前言
之前只是生成二維碼,現(xiàn)在需要把二維碼放在一個(gè)背景圖并且需要有文字介紹。之前沒做過,現(xiàn)在做好了記錄一下。
一. 獲取圖片
$background_path = root_path() . 'public/event/template.jpg'; //背景圖片地址 $qrcode_path = root_path() . 'public/event/qrcode/1653635892.png'; // 二維碼圖片地址 // 背景圖 $background_image = imagecreatefromjpeg($background_path); // 二維碼圖 $qrcode_image = imagecreatefrompng($qrcode_path);
備注:圖片地址必須是絕對(duì)地址
二. 把二維碼圖片放大
//獲取圖片的屬性,第一個(gè)寬度,第二個(gè)高度,類型1=>gif,2=>jpeg,3=>png list($qrcode_x, $qrcode_y) = getimagesize($qrcode_path); // 把二維碼圖片放大到1200像素 $size = 1200; // 新建一個(gè)畫布 $finalQrcode = imagecreatetruecolor($size, $size); // 把二維碼圖片放到新的畫布上 imagecopyresampled($finalQrcode, $qrcode_image, 0, 0, 0, 0, $size, $size, $qrcode_x, $qrcode_y);
備注:圖片放大其實(shí)就是新建一個(gè)你需要尺寸的大小的畫布,把之前的圖片移到新的畫布上,通過參數(shù)去控制圖片在畫布的位置及大小
三. 多個(gè)圖片合成
// 把背景和二維碼圖片合成在一起 //獲取圖片的屬性,第一個(gè)寬度,第二個(gè)高度,類型1=>gif,2=>jpeg,3=>png list($background_width,$background_height) = getimagesize($background_path); // 新建一個(gè)畫布,用來填充背景 $finalImage = imageCreatetruecolor($background_width,$background_height); // 圖像分配顏色 $color = imagecolorallocate($finalImage, 255, 255, 255); //設(shè)置居中圖片的X軸坐標(biāo)位置 $x = ($background_width-$size)/2; //設(shè)置居中圖片的Y軸坐標(biāo)位置 $y = 430; // 用于用給定的顏色填充圖像 imagefill($finalImage, 0, 0, $color); // 將顏色定義為透明色 imageColorTransparent($finalImage, $color); // 用背景來填充畫布 // 目標(biāo)圖 源圖 目標(biāo)X坐標(biāo)點(diǎn) 目標(biāo)Y坐標(biāo)點(diǎn) 源的X坐標(biāo)點(diǎn) 源的Y坐標(biāo)點(diǎn) 目標(biāo)寬度 目標(biāo)高度 源圖寬度 源圖高度 imagecopyresampled($finalImage,$background_image,0,0,0,0,$background_width,$background_height,$background_width,$background_width); //二維碼圖片在背景上的位置 $x橫坐標(biāo),$y縱坐標(biāo) imagecopymerge($finalImage,$finalQrcode, $x,$y,0,0,$size,$size, 100);
四. 添加文字并居中
圖片的文字進(jìn)行居中,需要我們通過圖片的尺寸,文字需要占的尺寸去計(jì)算。
現(xiàn)在有一個(gè)composer庫(stil/gd-text)可以實(shí)現(xiàn)該功能。
composer require stil/gd-text
$text = '測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試';
// 字體的不同會(huì)導(dǎo)致漢字寫入圖片亂碼
$font = root_path() . 'public/font/Alibaba-PuHuiTi-Medium.ttf'; // 字體的絕對(duì)地址
$showY = 2480-800;
$box = new Box($finalImage);
$box->setFontFace($font);
$box->setFontColor(new Color(0, 60, 121));//字體顏色
$box->setFontSize(160);//字體大小
$box->setLineHeight(2);//行高
$box->setBox(-22, $showY, 2480, 200);
$box->setTextAlign('center', 'top'); // 字體居中
$box->draw($text);
Header("Content-type: image/jpeg");
//將畫布保存到指定的文件
imagejpeg($finalImage, root_path() . 'public/event/qrcode/111.png');五. 完整的代碼
$background_path = root_path() . 'public/event/template.jpg'; //背景圖片地址
$qrcode_path = root_path() . 'public/event/qrcode/1653635892.png'; // 二維碼圖片地址
// 背景圖
$background_image = imagecreatefromjpeg($background_path);
// 二維碼圖
$qrcode_image = imagecreatefrompng($qrcode_path);
//獲取圖片的屬性,第一個(gè)寬度,第二個(gè)高度,類型1=>gif,2=>jpeg,3=>png
list($qrcode_x, $qrcode_y) = getimagesize($qrcode_path);
// 把二維碼圖片放大到1200像素
$size = 1200;
$finalQrcode = imagecreatetruecolor($size, $size);
imagecopyresampled($finalQrcode, $qrcode_image, 0, 0, 0, 0, $size, $size, $qrcode_x, $qrcode_y);
// 把背景和二維碼圖片合成在一起
//獲取圖片的屬性,第一個(gè)寬度,第二個(gè)高度,類型1=>gif,2=>jpeg,3=>png
list($background_width,$background_height) = getimagesize($background_path);
$finalImage = imageCreatetruecolor($background_width,$background_height);
$color = imagecolorallocate($finalImage, 255, 255, 255);
//設(shè)置居中圖片的X軸坐標(biāo)位置
$x = ($background_width-$size)/2;
//設(shè)置居中圖片的Y軸坐標(biāo)位置
$y = 430;
imagefill($finalImage, 0, 0, $color);
imageColorTransparent($finalImage, $color);
imagecopyresampled($finalImage,$background_image,0,0,0,0,$background_width, $background_height,$background_width,$background_width);
//圖片在背景上的位置 $x橫坐標(biāo),$y縱坐標(biāo)
imagecopymerge($finalImage,$finalQrcode, $x,$y,0,0,$size,$size, 100);
$text = '測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試';
$font = root_path() . 'public/font/Alibaba-PuHuiTi-Medium.ttf';
$showY = 2480-800;
$box = new Box($finalImage);
$box->setFontFace($font);
$box->setFontColor(new Color(0, 60, 121));//字體顏色
$box->setFontSize(160);//字體大小
$box->setLineHeight(2);//行高
$box->setBox(-22, $showY, 2480, 200);
$box->setTextAlign('center', 'top'); // 字體居中
$box->draw($text);
Header("Content-type: image/jpeg");
//將畫布保存到指定的文件
imagejpeg($finalImage, root_path() . 'public/event/qrcode/111.png');
exit();到此這篇關(guān)于php圖片放大合成加入字體操作示例介紹的文章就介紹到這了,更多相關(guān)php圖片放大內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP實(shí)現(xiàn)登陸并抓取微信列表中最新一組微信消息的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)登陸并抓取微信列表中最新一組微信消息的方法,涉及php針對(duì)微信接口的登陸、抓取、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
php獲取通過http協(xié)議post提交過來xml數(shù)據(jù)及解析xml
php 如何獲取請(qǐng)求的xml數(shù)據(jù),對(duì)方通過http協(xié)議post提交過來xml數(shù)據(jù),php如何獲取到這些數(shù)據(jù)呢?2012-12-12
PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
這篇文章主要介紹了PHP創(chuàng)建XML的方法,結(jié)合實(shí)例形式分析了php基于DOMDocument類及SimpleXMLElement類創(chuàng)建xml文件的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09

