php图片放大合成加入字体操作示例介绍

吾爱主题 阅读:141 2022-11-15 16:25:00 评论:0

前言

之前只是生成二维码,现在需要把二维码放在一个背景图并且需要有文字介绍。之前没做过,现在做好了记录一下。

一. 获取图片

?
1 2 3 4 5 6 $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 );

备注:图片地址必须是绝对地址

二. 把二维码图片放大

?
1 2 3 4 5 6 7 8 //获取图片的属性,第一个宽度,第二个高度,类型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 );

备注:图片放大其实就是新建一个你需要尺寸的大小的画布,把之前的图片移到新的画布上,通过参数去控制图片在画布的位置及大小

三. 多个图片合成

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // 把背景和二维码图片合成在一起 //获取图片的属性,第一个宽度,第二个高度,类型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); //设置居中图片的X轴坐标位置 $x = ( $background_width - $size )/2; //设置居中图片的Y轴坐标位置 $y = 430; // 用于用给定的颜色填充图像 imagefill( $finalImage , 0, 0, $color ); // 将颜色定义为透明色 imageColorTransparent( $finalImage , $color ); // 用背景来填充画布 // 目标图 源图 目标X坐标点 目标Y坐标点 源的X坐标点 源的Y坐标点 目标宽度 目标高度 源图宽度 源图高度 imagecopyresampled( $finalImage , $background_image ,0,0,0,0, $background_width , $background_height , $background_width , $background_width ); //二维码图片在背景上的位置 $x横坐标,$y纵坐标 imagecopymerge( $finalImage , $finalQrcode , $x , $y ,0,0, $size , $size , 100);

四. 添加文字并居中

图片的文字进行居中,需要我们通过图片的尺寸,文字需要占的尺寸去计算。

现在有一个composer库(stil/gd-text)可以实现该功能。

?
1 composer require stil/gd-text
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $text = '测试测试测试测试测试测试测试' ; // 字体的不同会导致汉字写入图片乱码 $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' );

五. 完整的代码

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 $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 ); //获取图片的属性,第一个宽度,第二个高度,类型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 ); // 把背景和二维码图片合成在一起 //获取图片的属性,第一个宽度,第二个高度,类型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); //设置居中图片的X轴坐标位置 $x = ( $background_width - $size )/2; //设置居中图片的Y轴坐标位置 $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横坐标,$y纵坐标 imagecopymerge( $finalImage , $finalQrcode , $x , $y ,0,0, $size , $size , 100); $text = '测试测试测试测试测试测试测试' ; $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 ();

到此这篇关于php图片放大合成加入字体操作示例介绍的文章就介绍到这了,更多相关php图片放大内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_39688201/article/details/125179237

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容