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

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

rush: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,'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,2=>array( 'x' => 0 + $g_border + ($g_width - 3*$g_border)/2 + $g_border,4=>array( 0=>array( 'x' => 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,2=>array( 'x' => 0 + $g_border,'y' => 0 + $g_border + ($g_height-3*$g_border)/2 + $g_border,3=>array( 'x' => 0 + $g_border + ($g_width-3*$g_border)/2 + $g_border,); // 排版 $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); imagefill($image_all,$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'],$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,$source_x,$source_y,$cropped_width,$cropped_height); // 缩放 imagecopyresampled($target_image,$cropped_image,$target_height,$cropped_height); return $target_image; }

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

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《PHP数学运算技巧总结》

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用