PHP怎么实现图片合并

后端开发   发布日期:2025年03月12日   浏览次数:271

本篇内容介绍了“PHP怎么实现图片合并”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

准备工作

1、需要海报的底图

2、小程序码的图片

代码部分结合YII2但不影响使用

完整过程

第一步:生成小程序码图片

第二步:缩放小程序码的图片大小 (如果尺寸符合海报大小可省略) 280-1280px

第三步:将缩放后的小程序图片合成到背景图片

第四步:合成文字信息

第一步:生成小程序码图片 (我使用的场景是无限制小程序码code地址 三种自行选择)

  1. //微信小程序 小程序码
  2. public static function getWeChatSmallProgramCode($scene)
  3. {
  4. $AccessToken = self::getAccessToken();
  5. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $AccessToken;
  6. $postData = [
  7. 'scene' => $scene,
  8. 'page' => 'pages/index/index',
  9. 'width'=>930
  10. ];
  11. $postData = json_encode($postData);
  12. $contentData = self::sendPost($url, $postData);
  13. return $contentData; //如果图片大小符合这开启base64位图片地址也可以完成图片的合并合文字的合并
  14. // return self::base64UrlCode($contentData, 'image/png');
  15. }
  16. protected static function sendPost($url, $post_data)
  17. {
  18. $options = array(
  19. 'http' => array(
  20. 'method' => 'POST',
  21. 'header' => 'Content-type:application/json',
  22. //header 需要设置为 JSON
  23. 'content' => $post_data,
  24. 'timeout' => 60
  25. //超时时间
  26. )
  27. );
  28. $context = stream_context_create($options);
  29. return file_get_contents($url, false, $context);
  30. }
  31. //二进制转图片image/png
  32. public static function base64UrlCode($contents, $mime)
  33. {
  34. $base64 = base64_encode($contents);
  35. return ('data:' . $mime . ';base64,' . $base64);
  36. }

第二步:缩放小程序码的图片大小

  1. /**
  2. * 缩放图片尺寸
  3. * @param $img_path string 图片地址
  4. * @param $new_width
  5. * @param $new_height
  6. * @param $new_img_path string 新的图片地址
  7. */
  8. public static function picZoom($img_path,$new_width,$new_height,$new_img_path)
  9. {
  10. //获取尺寸
  11. list($width, $height, $img_type, $attr) = getimagesize($img_path);
  12. $imageinfo = [
  13. 'width' => $width,
  14. 'height' => $height,
  15. 'type' => image_type_to_extension($img_type, false),
  16. 'attr' => $attr
  17. ];
  18. $fun = "imagecreatefrom" . $imageinfo['type'];
  19. $image = $fun($img_path);
  20. //创建新的幕布
  21. $image_thump = imagecreatetruecolor($new_width, $new_height);
  22. //复制源文件
  23. imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $imageinfo['width'], $imageinfo['height']);
  24. imagedestroy($image);
  25. $image = $image_thump;
  26. $func = 'image' . $imageinfo['type'];
  27. $func($image, $new_img_path);
  28. }

第三步:将缩放后的小程序图片合成到背景图片

  1. /**
  2. * 图片合并
  3. * 将源图片覆盖到目标图片上
  4. * @param string $dstPath 目标图片路径 背景图
  5. * @param string $srcPath 源图片路径 内容图
  6. * @param int $dstX 源图片覆盖到目标的X轴坐标
  7. * @param int $dstY 源图片覆盖到目标的Y轴坐标
  8. * @param int $srcX
  9. * @param int $srcY
  10. * @param int $pct 透明度
  11. * @param string $filename 输出的文件名,为空则直接在浏览器上输出显示
  12. * @return string $filename 合并后的文件名
  13. */
  14. public static function picMerge($dstPath, $srcPath, $dstX = 0, $dstY = 0, $srcX = 0, $srcY = 0, $pct = 100, $filename = '')
  15. {
  16. //创建图片的实例
  17. $dst = imagecreatefromstring(file_get_contents($dstPath));
  18. $src = imagecreatefromstring(file_get_contents($srcPath));
  19. //获取水印图片的宽高
  20. list($src_w, $src_h) = getimagesize($srcPath);
  21. //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
  22. // imagecopymerge($dst, $src, 80, 125, 0, 0, $src_w, $src_h, 100);
  23. imagecopymerge($dst, $src, $dstX, $dstY, $srcX, $srcY, $src_w, $src_h, $pct);
  24. //如果水印图片本身带透明色,则使用imagecopy方法
  25. //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
  26. //输出图片
  27. list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);
  28. switch ($dst_type) {
  29. case 1://GIF
  30. if (!$filename) {
  31. header('Content-Type: image/gif');
  32. imagegif($dst);
  33. } else {
  34. imagegif($dst, $filename);
  35. }
  36. break;
  37. case 2://JPG
  38. if (!$filename) {
  39. header('Content-Type: image/jpeg');
  40. imagejpeg($dst);
  41. } else {
  42. imagejpeg($dst, $filename);
  43. }
  44. break;
  45. case 3://PNG
  46. if (!$filename) {
  47. header('Content-Type: image/png');
  48. imagepng($dst);
  49. } else {
  50. imagepng($dst, $filename);
  51. }
  52. break;
  53. default:
  54. break;
  55. }
  56. imagedestroy($dst);
  57. imagedestroy($src);
  58. }

第四步:合成文字信息

  1. /**
  2. * 添加文字到图片上
  3. * @param $dstPath string 目标图片
  4. * @param $fontPath string 字体路径
  5. * @param $fontSize string 字体大小
  6. * @param $text string 文字内容
  7. * @param $dstY string 文字Y坐标值
  8. * @param string $filename 输出文件名,为空则在浏览器上直接输出显示
  9. * @return string 返回文件名
  10. */
  11. public static function addFontToPic($dstPath, $fontPath, $fontSize, $text, $dstY, $filename = '')
  12. {
  13. ob_end_clean();
  14. //创建图片的实例
  15. $dst = imagecreatefromstring(file_get_contents($dstPath));
  16. //打上文字
  17. $fontColor = imagecolorallocate($dst, 255, 255, 255);//字体颜色
  18. $width = imagesx($dst);
  19. $height = imagesy($dst);
  20. $fontBox = imagettfbbox($fontSize, 0, $fontPath, $text);//文字水平居中实质
  21. imagettftext($dst, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $dstY, $fontColor, $fontPath, $text);
  22. //输出图片
  23. list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);
  24. switch ($dst_type) {
  25. case 1://GIF
  26. if (!$filename) {
  27. header('Content-Type: image/gif');
  28. imagegif($dst);
  29. } else {
  30. imagegif($dst, $filename);
  31. }
  32. break;
  33. case 2://JPG
  34. if (!$filename) {
  35. header('Content-Type: image/jpeg');
  36. imagejpeg($dst);
  37. } else {
  38. imagejpeg($dst, $filename);
  39. }
  40. break;
  41. case 3://PNG
  42. if (!$filename) {
  43. header('Content-Type: image/png');
  44. imagepng($dst);
  45. } else {
  46. imagepng($dst, $filename);
  47. }
  48. break;
  49. default:
  50. break;
  51. }
  52. imagedestroy($dst);
  53. return $filename;
  54. }

外部的调用

  1. /**
  2. * 根据店铺id 和名称 合成A5 图片小程序图片
  3. * @param $shop_id
  4. * @param $shop_name
  5. * @return array
  6. */
  7. public static function generateWeChatAppletImage($shop_id, $shop_name)
  8. {
  9. //1 生成小程序码
  10. //2 合成小程序码到背景图片
  11. $sceneStr = '?shop_id=' . $shop_id;
  12. $weChatAppImgBaseData = WxTools::getWeChatSmallProgramCode($sceneStr);
  13. $weChatAppImgPath = './weChatAppImg/shop_code_' . $shop_id . '.jpg';
  14. file_put_contents($weChatAppImgPath, $weChatAppImgBaseData);
  15. //合并到背景图片中
  16. $beiJinImgPath = './weChatAppImg/weChatBJ.jpg';
  17. $mergeImgFile = './weChatAppImg/shop_mini_program' . $shop_id . '.jpg';
  18. GenerateCodeImg::picMerge($beiJinImgPath, $weChatAppImgPath, 408, 714, $srcX = 0, $srcY = 0, $pct = 100, $mergeImgFile);
  19. //3 合成文字
  20. $fontPath = './plus/fonts/SourceHanSansCN-Bold.ttf';
  21. $fontSize = 40;
  22. $dstY = 640;
  23. GenerateCodeImg::addFontToPic($mergeImgFile, $fontPath, $fontSize, $shop_name, $dstY, $mergeImgFile);
  24. $weChatCodeImgUrL = Yii::$app->request->hostInfo . '/weChatAppImg/shop_code_' . $shop_id . '.jpg';
  25. $weChatAppImgUrl = Yii::$app->request->hostInfo . '/weChatAppImg/shop_mini_program' . $shop_id . '.jpg';
  26. return [
  27. 'weChatCodeImgUrL' => $weChatCodeImgUrL,
  28. 'weChatAppImgUrl' => $weChatAppImgUrl,
  29. ];
  30. }

常见的问题

1文字合并的时候出现乱码?

第一检测一下字体是否是正常tff字体 如果不知道去C://windows/Fonts 随便找一个 微软雅黑都行

2、英文阿拉布数字正常 中文乱码

  1. $text = mb_convert_encoding("呵呵呵","UTF-8","GBK");

  1. $text = mb_convert_encoding("呵呵呵","html-entities","UTF-8");

设置看看

以上就是PHP怎么实现图片合并的详细内容,更多关于PHP怎么实现图片合并的资料请关注九品源码其它相关文章!