php 远程图片合拼,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

$images[] = imagecropper($files[$i],$g_models[$type][$i]['w'],$g_models[$type][$i]['h']);

}

// 排版合成

for($i=0;$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程序设计有所帮助。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542570.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

bandizip最后一个无广告版本_如果非要选择一款压缩软件的话——Bandizip

全世界只有不到0.00~1 % 的人关注了我们得到你的关注是小帮的幸运压缩解压软件是电脑一个必备软甲&#xff0c;前面的文章介绍了一款开源小巧无广告的压缩解压软件windows工具软件选择之压缩软件——7-Zip&#xff0c;如果有人用不惯的话可以试试今天的这款。Bandizip 是一款来…

[MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现

新人刚开始学习ASP.NET MVC&#xff0c;若有不足之处希望能得到您的指点&#xff0c;不胜感激&#xff01; 先来一张项目的层级结构图: Model&#xff1a;模型层&#xff0c;主要是各种类型、枚举以及ORM框架&#xff0c;框架完成数据库和实体类的映射。项目中选用了微软的开源…

日期getUTCSeconds()方法以及JavaScript中的示例

JavaScript日期getUTCSeconds()方法 (JavaScript Date getUTCSeconds() method) getUTCSeconds() method is a Dates class method and it is used to get seconds from the current time according to the UTC (Universal time coordinated). getUTCSeconds()方法是Date的类方…

dedecms 在模板里引入php文件夹,dedecms如何添加并引入php文件

前言&#xff1a;有些时候我们需要创建一些单独的PHP文件&#xff0c;但是随便放入的PHP文件是不能够编译织梦 dedecms的标签的&#xff0c;所以我们需要引入织梦标签的编译引擎方案。例如&#xff0c;我们在根目录创建 example.php&#xff0c;代码如下&#xff1a;<?php …

mybatisplus代码生成器_想做时间管理大师?你可以试试Mybatis Plus代码生成器

1. 前言对于写Crud的老司机来说时间非常宝贵&#xff0c;一些样板代码写不但费时费力&#xff0c;而且枯燥无味。经常有小伙伴问我&#xff0c;胖哥你怎么天天那么有时间去搞新东西&#xff0c;透露一下秘诀呗。好吧&#xff0c;今天就把Mybatis-plus的代码生成器分享出来&…

安装Oracle 11g RAC R2 之Linux DNS 配置

Oracle 11g RAC 集群中引入了SCAN(Single Client Access Name)的概念&#xff0c;也就是指集群的单客户端访问名称。SCAN 这个特性为客户端提供了单一的主机名&#xff0c;用于访问集群中运行的 Oracle 数据库。如果您在集群中添加或删除节点&#xff0c;使用 SCAN 的客户端无需…

c++ websocket客户端_websocket使用

websocket使用一、介绍在项目开发过程中&#xff0c;很多时候&#xff0c;我们不可避免的需要实现的一个功能&#xff1a; 服务端实时发送信息给客户端。比如实时公告、实时订单通知、实时报警推送等等&#xff0c;登录后的客户端需要知道与它相关的实时信息&#xff0c;以便进…

汉子编码比字母编码长_字母/博客作者编码问题(使用动态编程)

汉子编码比字母编码长Problem statement: 问题陈述&#xff1a; Shivang is a blog writer and he is working on two websites simultaneously. He has to write two types of blogs which are: Shivang是一位博客作家&#xff0c;他同时在两个网站上工作。 他必须写两种类型…

php parent报错,mac brew 安装php扩展报错:parent directory is world writable but not sticky

$ brew install php70-mcrypt报错&#xff1a;Error: parent directory is world writable but not sticky搜索到github的答案https://github.com/Homebrew/legacy-homebrew/issues/40345原因&#xff1a;/tmp目录权限不对$ ls -ld /private/tmp打印出来 /private/tmp 被标黄了…

在cordova中使用HTML5的多文件上传

2019独角兽企业重金招聘Python工程师标准>>> 我们先看看linkface给开放的接口&#xff1a; 字段类型必需描述api_idstring是API 账户api_secretstring是API 密钥selfie_filefile见下方注释需上传的图片文件 1&#xff0c;上传本地图片进行检测时选取此参数selfie_ur…

python dataframe切片_python pandas dataframe 行列选择,切片操作方法

SQL中的select是根据列的名称来选取&#xff1b;Pandas则更为灵活&#xff0c;不但可根据列名称选取&#xff0c;还可以根据列所在的position&#xff08;数字&#xff0c;在第几行第几列&#xff0c;注意pandas行列的position是从0开始&#xff09;选取。相关函数如下&#xf…

php根据设备判断访问,PHP判断设备访问来源

/*** 判断用户请求设备是否是移动设备* return bool*/function isMobile() {//如果有HTTP_X_WAP_PROFILE则一定是移动设备if (isset($_SERVER[HTTP_X_WAP_PROFILE])) {return true;}//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息if (isset($_SERVER[HTTP_VIA])…

机器学习 深度学习 ai_如何学习机器学习和人工智能?

机器学习 深度学习 aiSTRATEGY 战略 Learn theory practical aspects. 学习理论和实践方面的知识。 (At first get an overview of what you are going to learn). (首先获得要学习的内容的概述)。 Gain a good hold/insight on each concept. 掌握/理解每个概念。 If you …

linux常用命令和配置

2019独角兽企业重金招聘Python工程师标准>>> 启动php&#xff1a; /etc/init.d/php-fpm restart 查看PHP运行目录&#xff1a; which php /usr/bin/php 查看php-fpm进程数&#xff1a; ps aux | grep -c php-fpm 查看运行内存 /usr/bin/php -i|grep mem iptables如…

centos7时间同步_centos 8.x系统配置chrony时间同步服务

centos 8.x系统配置chrony时间同步服务CentOS 7.x默认使用的时间同步服务为ntp服务&#xff0c;但是CentOS 8开始在官方的仓库中移除了ntp软件&#xff0c;换成默认的chrony进行时间同步的服务&#xff0c;chrony既可以作为客户端向其他时间服务器发送时间同步请求&#xff0c;…

php可以用scanf,C/C++中 使用scanf和printf如何读入输出double型数据。

黄舟2017-04-17 13:47:232楼注意scanf函数和printf函数是不同寻常的函数&#xff0c;因为它们都没有将函数的参数限制为固定数量。scanf函数和printf函数又可变长度的参数列表。当调用带可变长度参数列表的函数时&#xff0c;编译器会安排float参数自动转换成为double类型&…

ICWAI和ICWA的完整形式是什么?

ICWAI / ICWA&#xff1a;印度成本与工程会计师协会/印度儿童福利法 (ICWAI / ICWA: Institute of Cost and Works Accountants of India / Indian Child Welfare Act) 1)ICWAI&#xff1a;印度成本与工程会计师协会 (1) ICWAI: Institute of Cost and Works Accountants of In…

深入研究java.lang.Runtime类【转】

转自&#xff1a;http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 RuntimegetRuntimeexitaddShutdownHookremoveShutdownHookhaltrunFinalizersOnExitexecexecexecexecexecexecavailableProcessorsfreeMemorytotalMemorymaxMemorygcrunFina…

java队列实现限流,java中应对高并发的两种策略

目的&#xff1a;提高可用性通过ExecutorService实现队列泄洪//含有20个线程的线程池private ExecutorService executorService Executors.newFixedThreadPool(20);将有并发压力的下游代码放入到线程池的submit方法中&#xff0c;如下&#xff1a;//同步调用线程池的submit方法…

crontab 日志_liunx 中定时清理过期日志文件

问题描述经常遇到日志文件过多&#xff0c;占用大量磁盘空间&#xff0c;需要定期删除过期日志。问题涉及方面删除过期日志的脚本。定时任务删除任务脚本先查询到过期的日志文件&#xff0c;然后删除。语法find path -option [ -print ] [ -exec -ok command ] …