久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP實現將幾張照片拼接到一起的合成圖片功能【便于整體打印輸出】

這篇文章主要介紹了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("\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:該代碼應用于命令行模式,且需要注意圖片文件夾路徑。

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要介紹了PHP定義字符串的四種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
下面小編就為大家分享一篇php 替換文章中的圖片路徑,下載圖片到本地服務器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP給源代碼加密的幾種方法匯總(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇php打開本地exe程序,js打開本地exe應用程序,并傳遞相關參數方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP類的反射來實現依賴注入過程以及相關知識點分享,對此有興趣的朋友跟著小編學習下吧。
php遍歷一個文件夾內的所有文件和文件夾,并刪除所有文件夾和子文件夾下的所有文件的代碼,通過遞歸方式實現達到清空一個目錄的效果。本文給大家分享實例代碼,需要的朋友參考
主站蜘蛛池模板: 国产成人精品视频在线观看 | 欧美成人精品一区二区男人看 | 农村妇女毛片精品久久久 | 一级黄色片免费在线观看 | 日韩男人天堂 | 国产在线视频网 | 91 久久| 伊人天堂网| 中文字幕国产视频 | 激情黄色在线观看 | 亚洲成人高清 | 久久人 | 久久99国产精一区二区三区 | 国产精品91视频 | 国产精品精品视频一区二区三区 | 国产欧美日韩 | 视频一区二区在线 | 伊人网99 | 热99在线| 91久久国产综合久久 | 日本人爽p大片免费看 | 99精品欧美| 亚洲va欧美va人人爽午夜 | 四虎最新视频 | 欧美日韩三级在线观看 | www.蜜桃av.com | 中文区中文字幕免费看 | 精品一区二区三区在线播放 | 亚洲高清一区二区三区 | 色香婷婷 | 日韩欧美三区 | 97中文视频 | 精品国产乱码 | 亚洲国产一区二区三区在线观看 | 午夜电影在线播放 | 中文字幕综合在线 | 91视频网址| 欧美激情综合 | 日韩国产精品一区二区三区 | 久久亚洲春色中文字幕久久久 | 国产精品影视在线观看 |