侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP实现将几张照片拼接到一起的合成图片功能【便于整体打印输出】

php  /  管理员 发布于 7年前   210

本文实例讲述了PHP实现将几张照片拼接到一起的合成图片功能。分享给大家供大家参考,具体如下:

array(位置=>array(x,y,width,height))$g_models = array( 1=>array( // 单页总张数  0=>array( // 位置   'x' => 0 + $g_border,   'y' => 0 + $g_border,   'w' => $g_width - 2*$g_border,   'h' => $g_height - 2*$g_border,  ), ), 3=>array(  0=>array(   'x' => 0 + $g_border,   'y' => 0 + $g_border,   'w' => $g_width - 2*$g_border,   'h' => ($g_height - 3*$g_border)/2,  ),  1=>array(   'x' => 0 + $g_border,   'y' => 0 + $g_border + ($g_height - 3*$g_border)/2 + $g_border,   'w' => ($g_width - 3*$g_border)/2,   'h' => ($g_height - 3*$g_border)/2,  ),  2=>array(   'x' => 0 + $g_border + ($g_width - 3*$g_border)/2 + $g_border,   'y' => 0 + $g_border + ($g_height - 3*$g_border)/2 + $g_border,   'w' => ($g_width - 3*$g_border)/2,   'h' => ($g_height - 3*$g_border)/2,  ), ), 4=>array(  0=>array(   'x' => 0 + $g_border,   'y' => 0 + $g_border,   'w' => ($g_width-3*$g_border)/2,   'h' => ($g_height-3*$g_border)/2,  ),  1=>array(   'x' => 0 + $g_border + ($g_width-3*$g_border)/2 + $g_border,   'y' => 0 + $g_border,   'w' => ($g_width-3*$g_border)/2,   'h' => ($g_height-3*$g_border)/2,  ),  2=>array(   'x' => 0 + $g_border,   'y' => 0 + $g_border + ($g_height-3*$g_border)/2 + $g_border,   'w' => ($g_width-3*$g_border)/2,   'h' => ($g_height-3*$g_border)/2,  ),  3=>array(   'x' => 0 + $g_border + ($g_width-3*$g_border)/2 + $g_border,   'y' => 0 + $g_border + ($g_height-3*$g_border)/2 + $g_border,   'w' => ($g_width-3*$g_border)/2,   'h' => ($g_height-3*$g_border)/2,  ), ),);// 排版$g_tasks = array( 0 => array(0), // 封面封底 1 => array(1), 2 => array(2), 3 => array(3), 4 => array(4,5,6), 5 => array(7), 6 => array(8), 7 => array(9,10,11), 8 => array(12), 9 => array(13), 10 => array(14,15,16), 11 => array(17), 12 => array(18), 13 => array(19,20,21), 14 => array(22), 15 => array(23), 16 => array(24,25,26), 17 => array(27,28,29), 18 => array(30), 19 => array(31), 20 => array(32,33,34), 21 => array(35), 22 => array(36), 23 => array(37), 24 => array(38,39,40,41), 25 => array(42,43,44), 26 => array(45), 27 => array(46), 28 => array(47,48,49), 29 => array(50), 30 => array(51),);// 获取文件夹下的所有图片名$jpgs = array();$files = scandir($src_path); // 目录下所有文件名foreach($files as $file){ $path_parts = pathinfo($src_path.'/'.$file); if($path_parts['extension'] == 'jpg'){  $jpgs[] = $src_path.'/'.$file; }}// 判断图片总数if(count($jpgs) != 52){ echo '图片总数有误:'.count($jpgs).'/52'.nl2br("\n"); die();}// 自然排序usort($jpgs, "strnatcmp");foreach($g_tasks as $page=>$photos){ $files = array(); foreach($photos as $r){  $files[] = $jpgs[$r]; } $image_all = imagemake($files); $filename = $page.'.jpg'; imagejpeg($image_all, $dst_path.'/'.$filename); unset($files); echo $filename.nl2br("\n");}echo 'ok'.nl2br("\n");die();/** * 合成图片 * @param array $images 本页图片集合 * @return resource 合成后的图片 */function imagemake($files=array()){ global $g_width,$g_height,$g_models; // 合成后的图片 $image_all = imageCreatetruecolor($g_width,$g_height); // 为真彩色画布创建白色背景 $color = imagecolorallocate($image_all, 255, 255, 255); imagefill($image_all, 0, 0, $color);// imageColorTransparent($image_all, $color); // 背景透明 //function imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) // 排版合成 $type = count($files); switch($type){  case 2:   break;  case 1:  case 3:  case 4:   // 用于合成的图片集   $images = array();   // 修正图片   for($i=0;$i<$type;$i++){    $images[] = imagecropper($files[$i],$g_models[$type][$i]['w'],$g_models[$type][$i]['h']);   }   // 排版合成   for($i=0;$i<$type;$i++){    imagecopyresampled($image_all,$images[$i],     $g_models[$type][$i]['x'],$g_models[$type][$i]['y'],0,0,     $g_models[$type][$i]['w'],$g_models[$type][$i]['h'],imagesx($images[$i]),imagesy($images[$i]));   }   break;  default:   break; } return $image_all;}/** * 修剪图片:居中裁剪等比缩放 * @param $source_path 原图路径 * @param $target_width 目标宽度 * @param $target_height 目标高度 * @return bool|resource */function imagecropper($source_path, $target_width, $target_height){ $source_info = getimagesize($source_path); $source_width = $source_info[0]; $source_height = $source_info[1]; $source_mime = $source_info['mime']; $source_ratio = $source_height / $source_width; $target_ratio = $target_height / $target_width; switch ($source_mime) {  case 'image/gif':   $source_image = imagecreatefromgif($source_path);   break;  case 'image/jpeg':   $source_image = imagecreatefromjpeg($source_path);   break;  case 'image/png':   $source_image = imagecreatefrompng($source_path);   break;  default:   return false;   break; } // 横竖构图不同,旋转 if(($target_width-$target_height)*($source_width-$source_height)<0){  // 旋转  $source_image = imagerotate($source_image, 90, 0);  $source_width = $source_info[1]; // [0]  $source_height = $source_info[0]; // [1]  $source_ratio = $source_height / $source_width; } // 源图过高 if ($source_ratio > $target_ratio) {  $cropped_width = $source_width;  $cropped_height = $source_width * $target_ratio;  $source_x = 0;  $source_y = ($source_height - $cropped_height) / 2; } // 源图过宽 elseif ($source_ratio < $target_ratio) {  $cropped_width = $source_height / $target_ratio;  $cropped_height = $source_height;  $source_x = ($source_width - $cropped_width) / 2;  $source_y = 0; } // 源图适中 else {  $cropped_width = $source_width;  $cropped_height = $source_height;  $source_x = 0;  $source_y = 0; } $target_image = imagecreatetruecolor($target_width, $target_height); $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height); // 裁剪 imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); // 缩放 imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height); return $target_image;}

PS:该代码应用于命令行模式,且需要注意图片文件夹路径。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《PHP数学运算技巧总结》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例
  • php图片合成方法(多张图片合成一张)
  • PHP将身份证正反面两张照片合成一张图片的代码
  • PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
  • PHP基于php_imagick_st-Q8.dll实现JPG合成GIF图片的方法
  • php curl优化下载微信头像的方法总结
  • PHP仿微信多图片预览上传实例代码
  • PHP实现微信图片上传到服务器的方法示例
  • 微信小程序上传图片到php服务器的方法
  • PHP 图片合成、仿微信群头像的方法示例


  • 上一条:
    浅谈PHP中如何实现Hook机制
    下一条:
    PHP封装的XML简单操作类完整实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客