<?php
/** * @param array $files文件組 * @param int $xNum列數量 * @param int $yNum行數量 * @param int $xDistance列間距 * @param int $yDistance行間距 */ function dealHbPic ($files = [], $xNum = 1, $yNum = 3, $xDistance = 2, $yDistance = 2) { // -----------------------給圖片分組,生成$xNum列,$yNum行的組------------------------------------------------------- $group = []; $groupNum = $yNum; $nowGroup = 0; foreach ($files as $key => $item) { $group[$nowGroup][] = $item; if (($key+1) % $xNum === 0) { $nowGroup++; } } //--------------將第一個圖片的寬高當作一個元素的標準,先繪畫一個背景圖用來存放圖片---------------------------------- $img = new Imagick($files[0]); $width = $img->getImageWidth(); $height= $img->getImageHeight(); //首先進行一個圖片繪畫 $newImg = new Imagick(); $newImg->newImage($width * $xNum + ($xNum - 1) * $xDistance, $height * $yNum + ($yNum - 1) * $yDistance, '#AAAAAA', 'jpg'); $newImg->compositeImage($img, Imagick::COMPOSITE_OVER, 0, 0 ); //----------------------------按組將圖片覆蓋到對應的區域------------------------------------------------------------ foreach ($group as $key => $val) { //以每一行進行處理 foreach ($val as $sKey => $sVal) { $copyImg = new Imagick($sVal); //根據標準元素處理每一個圖片 $copyImg->resizeImage($width, $height,Imagick::FILTER_LANCZOS,1); //好了這裏開始繪畫處理 $nowWidth = $width * $sKey;//根據列計算當前圖片應該在X軸的位置 if ($sKey > 0) { $nowWidth += $xDistance;//如果超過第一列,則增加對應列間隔 } $nowHeight= $height * $key;//根據行計算當前圖片應該在Y軸的位置 if ($key > 0) { $nowHeight += $yDistance * $key;//如果超過第一行,則增加對應行間隔 } $newImg->compositeImage($copyImg, Imagick::COMPOSITE_OVER, $nowWidth, $nowHeight ); } } $file=".\jpg\img.jpg"; $newImg->writeImage($file); }
$jpgFileNameArray = [ '.\jpg\1513145518_thump.jpg', '.\jpg\1513145539_thump.jpg', '.\jpg\1513145706_thump.jpg', ]; dealHbPic($jpgFileNameArray);PS:可根據對應參數處理成對應的圖片合集
效果圖:(紅框內爲對應行間隔)