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 相关文章推荐
PHP4 与 MySQL 数据库操作函数详解
Dec 06 PHP
cache_lite试用
Feb 14 PHP
PHP 木马攻击防御技巧
Jun 13 PHP
同台服务器使用缓存APC效率高于Memcached的演示代码
Feb 16 PHP
PHP压缩html网页代码(清除空格,换行符,制表符,注释标记)
Apr 02 PHP
通过5个php实例细致说明传值与传引用的区别
Aug 08 PHP
PHP简单实现“相关文章推荐”功能的方法
Jul 19 PHP
PHP实现webshell扫描文件木马的方法
Jul 31 PHP
如何直接访问php实例对象中的private属性详解
Oct 12 PHP
php+ajax实现仿百度查询下拉内容功能示例
Oct 20 PHP
对于Laravel 5.5核心架构的深入理解
Feb 22 PHP
浅谈PHP SHA1withRSA加密生成签名及验签
Mar 18 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
php中使用Curl、socket、file_get_contents三种方法POST提交数据
2011/08/12 PHP
一个JS函数搞定网页标题(title)闪动效果
2014/05/13 Javascript
js加入收藏夹代码(兼容ie/ff/op)
2014/05/16 Javascript
JS继承用法实例分析
2015/02/05 Javascript
JS实现的数组全排列输出算法
2015/03/19 Javascript
jQuery使用animate创建动画用法实例
2015/08/07 Javascript
基于jQuery全屏焦点图左右切换插件responsiveslides
2015/09/07 Javascript
JavaScript简单遍历DOM对象所有属性的实现方法
2015/10/21 Javascript
点评js异步加载的4种方式
2015/12/22 Javascript
JavaScript代码实现左右上下自动晃动自动移动
2016/04/08 Javascript
js跨域资源共享 基础篇
2016/07/02 Javascript
原生js实现商品放大镜效果
2017/01/12 Javascript
jQuery分页插件jquery.pagination.js使用方法解析
2017/02/09 Javascript
vue 做移动端微信公众号采坑经验记录
2018/04/26 Javascript
浅谈JS和jQuery的区别
2019/03/27 jQuery
jQuery实现移动端图片上传预览组件的方法分析
2020/05/01 jQuery
vue路由跳转传递参数的方式总结
2020/05/10 Javascript
[55:03]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第二场 11.20
2020/11/20 DOTA
Python使用MD5加密算法对字符串进行加密操作示例
2018/03/30 Python
Python实现时钟显示效果思路详解
2018/04/11 Python
python pillow模块使用方法详解
2019/08/30 Python
详解Django admin高级用法
2019/11/06 Python
python 数据库查询返回list或tuple实例
2020/05/15 Python
python 模块导入问题汇总
2021/02/01 Python
AmazeUI 加载进度条的实现示例
2020/08/20 HTML / CSS
英国体育器材进口商店:UK Sport Imports
2017/03/14 全球购物
美国非常受欢迎的Spa品牌:Bliss必列斯
2018/04/10 全球购物
视光学专业毕业生推荐信
2013/10/28 职场文书
婚礼证婚人证婚词
2014/01/08 职场文书
个人承诺书
2014/03/26 职场文书
医疗器械售后服务承诺书
2014/05/21 职场文书
2014年秋季新学期寄语
2014/08/02 职场文书
公司演讲稿开场白
2014/08/25 职场文书
大学生见习报告总结
2014/11/04 职场文书
个人委托函范文
2015/01/29 职场文书
Python数组变形的几种实现方法
2022/05/30 Python