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


Posted in PHP onNovember 14, 2017

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

<?php
/**
 * 作品合成程序
 * 针对单面,封面不做特殊处理
 */
$src_path = $argv[1]; // php该文件,第一个参数是文件夹名(作品集),可相对路径
$dst_path = '../image/'.$src_path; // 生成文件存放的目标位置
if (!file_exists($dst_path)){
 mkdir($dst_path);
}
// 合成图推荐大小,单页大小建议:1120*1600
$g_width = 1120;
$g_height = 1600;
$g_border = 20; // 边框
// 模板
// 图片张数=>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 相关文章推荐
phpadmin如何导入导出大数据文件及php.ini参数修改
Feb 18 PHP
基于Zookeeper的使用详解
May 02 PHP
php中通过curl检测页面是否被百度收录
Sep 27 PHP
常见的四种POST 提交数据方式(小总结)
Oct 08 PHP
PHP中strcmp()和strcasecmp()函数字符串比较用法分析
Jan 07 PHP
Yii2实现ajax上传图片插件用法
Apr 28 PHP
总结PHP如何获取当前主机、域名、网址、路径、端口和参数等
Sep 09 PHP
php+ajax实现带进度条的上传图片功能【附demo源码下载】
Sep 14 PHP
Ubuntu 16.04中Laravel5.4升级到5.6的步骤
Dec 07 PHP
laravel 使用事件系统统计浏览量的实现
Oct 16 PHP
浅谈PHP之ThinkPHP框架使用详解
Jul 21 PHP
基于PHP的微信公众号的开发流程详解
Aug 07 PHP
PHP封装的XML简单操作类完整实例
Nov 13 #PHP
PHP开发中解决并发问题的几种实现方法分析
Nov 13 #PHP
三个思路解决laravel上传文件报错:413 Request Entity Too Large问题
Nov 13 #PHP
kindeditor 加入七牛云上传的实例讲解
Nov 12 #PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
Nov 12 #PHP
PHP 断点续传实例详解
Nov 11 #PHP
PHP+AJAX 投票器功能
Nov 11 #PHP
You might like
wiki-shan写的php在线加密的解密程序
2008/09/07 PHP
thinkphp实现数组分页示例
2014/04/13 PHP
php+xml编程之SimpleXML的应用实例
2015/01/24 PHP
一个高效的JavaScript压缩工具下载集合
2007/03/06 Javascript
ASP.NET jQuery 实例16 通过控件CustomValidator验证RadioButtonList
2012/02/03 Javascript
jquery获取div宽度的实现思路与代码
2013/01/13 Javascript
js 阻止子元素响应父元素的onmouseout事件具体实现
2013/12/23 Javascript
Jquery图片延迟加载插件jquery.lazyload.js的使用方法
2014/05/21 Javascript
jquery复选框多选赋值给文本框的方法
2015/01/27 Javascript
JavaScript里四舍五入函数round用法实例
2015/04/06 Javascript
Javascript中的方法和匿名方法实例详解
2015/06/13 Javascript
json+jQuery实现的无限级树形菜单效果代码
2015/08/27 Javascript
JavaScript中rem布局在react中的应用
2015/12/09 Javascript
基于jQuery实现美观且实用的倒计时实例代码
2015/12/30 Javascript
详解JS转换数值函数Number()、parseInt()、parseFloat()
2018/08/24 Javascript
element-ui 上传图片后清空图片显示的实例
2018/09/04 Javascript
JS实现的杨辉三角【帕斯卡三角形】算法示例
2019/02/26 Javascript
vue实现滑动到底部加载更多效果
2020/10/27 Javascript
Element-ui 自带的两种远程搜索(模糊查询)用法讲解
2021/01/29 Javascript
[45:59]EG vs OG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
浅谈Python程序与C++程序的联合使用
2015/04/07 Python
python实现单链表中删除倒数第K个节点的方法
2018/09/28 Python
Python+OpenCV图片局部区域像素值处理改进版详解
2019/01/23 Python
详解小白之KMP算法及python实现
2019/04/04 Python
Python Pandas 箱线图的实现
2019/07/23 Python
django创建最简单HTML页面跳转方法
2019/08/16 Python
python GUI库图形界面开发之PyQt5打印控件QPrinter详细使用方法与实例
2020/02/28 Python
Python文件夹批处理操作代码实例
2020/07/21 Python
利用python+ffmpeg合并B站视频及格式转换的实例代码
2020/11/24 Python
用你熟悉的语言写一个连接ORACLE数据库的程序,能够完成修改和查询工作
2012/06/11 面试题
Windows和Linux动态库应用异同
2016/04/17 面试题
外语系毕业生自荐信范文
2013/12/16 职场文书
读书活动总结范文
2014/04/26 职场文书
2015元旦晚会主持词(开场白+结束语)
2014/12/14 职场文书
简历自荐信范文
2015/03/09 职场文书
Spring boot admin 服务监控利器详解
2022/08/05 Java/Android