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.pswp.cn/news/542570.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542570.shtml
英文地址,請注明出處:http://en.pswp.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 ] …