php拼接图片,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(""); 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("");}echo "ok".nl2br("");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 $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程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值