用PHP将16个图像合并为1个大图像 - php

我有16张图像,每个图像的大小为512,我需要使用PHP 4行4列(4X4)组合它们,然后保存新图像。

我已经尝试过了,但是似乎没有用!

<?php
 $stars = imagecreatefrompng("images/1.jpg");
 $gradient = imagecreatefrompng("images/2.jpg");
 imagecopymerge($stars, $gradient, 0, 0, 0, 0, 256, 256, 60);
 header('Content-type: image/png');
 imagepng($stars);
 imagedestroy($stars);
 imagedestroy($gradient);
?>

我怎样才能做到这一点的脚本?

参考方案

步骤1:取得包含原始图片位置的阵列
此步骤对于任何人都是不同的,但是以最简单的形式,您将定义如下内容:

$srcImagePaths = Array('https://diceattack.files.wordpress.com/2011/01/tile_e_o_fh.png',
'https://diceattack.files.wordpress.com/2011/01/tile_e_g_fh.png',
'https://diceattack.files.wordpress.com/2011/01/tile_e_b_fh.png',
'https://diceattack.files.wordpress.com/2011/01/tile_b_q_fh.png');

第2步:定义一些措施并初始化空白的“背景”图片
在这里,我们使用第一个GD函数:imagecreatetruecolor()创建通用的“基本”图像,imagecolorallocate()定义RGB颜色,imagefill()用该颜色填充通用图像。

$tileWidth = $tileHeight = 28;
$numberOfTiles = 12;
$pxBetweenTiles = 1;

$mapWidth = $mapHeight = ($tileWidth + $pxBetweenTiles) * $numberOfTiles;

$mapImage = imagecreatetruecolor($mapWidth, $mapHeight);
$bgColor = imagecolorallocate($mapImage, 50, 40, 0);
imagefill($mapImage, 0, 0, $bgColor);

第三步:
想想您要使源图像结束的坐标
有几种不同的方法可以指定此方法,但是如果要处理许多相同大小的图像,则可以编写一个小的函数来将(数组)索引映射到一组X,Y坐标,这是有意义的。这是我将它们全部排列成12×12正方形网格的地方:

function indexToCoords($index)
{
 global $tileWidth, $pxBetweenTiles, $leftOffSet, $topOffSet, $numberOfTiles;

 $x = ($index % $numberOfTiles) * ($tileWidth + $pxBetweenTiles) + $leftOffSet;
 $y = floor($index / $numberOfTiles) * ($tileWidth + $pxBetweenTiles) + $topOffSet;
 return Array($x, $y);
}

步骤4:遍历源图像并将其复制到基础图像上
我们使用函数imagecopy()来执行此操作,如下所示:

/ *
*将图像复制到地图
* /

foreach ($srcImagePaths as $index => $srcImagePath)
{
 list ($x, $y) = indexToCoords($index);
 $tileImg = imagecreatefrompng($srcImagePath);

 imagecopy($mapImage, $tileImg, $x, $y, 0, 0, $tileWidth, $tileHeight);
 imagedestroy($tileImg);
}

注意我们在那里如何使用indexToCoords()函数–当然,我们不希望所有> source图像都位于同一位置。

第5步(间奏):使用PHP调整图像大小
我们用来将源图像放在基础图像上的同一imagecopy()函数也可以用于调整图像大小。如果要自动生成缩略图,则非常方便!您可以按照以下方法进行操作:

/ *
*转为缩略图格式
* /

$thumbSize = 200;
$thumbImage = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumbImage, $mapImage, 0, 0, 0, 0, $thumbSize, $thumbSize, $mapWidth, $mapWidth);

最后一步:设置标头以告知浏览器即将出现图像,然后输出最终图像

/ *
*输出缩略图图像
* /

header ("Content-type: image/png");
imagepng($thumbImage); //change argument to $mapImage to output the original size image

就是这样!请注意,您可能不需要均匀填充的背景,而是想要真实的背景图像–您可以在步骤2中使用imagecreatefrompng()轻松地做到这一点。

为了方便起见,这里再次提供所有代码。

<?php

//Source image paths (DISCLAIMER: this is just to demonstrate, to generate a real TT map you need 144 of these)
<pre>$srcImagePaths = Array('https://diceattack.files.wordpress.com/2011/01/tile_e_o_fh.png',
'https://diceattack.files.wordpress.com/2011/01/tile_e_g_fh.png',
'https://diceattack.files.wordpress.com/2011/01/tile_e_b_fh.png',
'https://diceattack.files.wordpress.com/2011/01/tile_b_q_fh.png');
</pre>
/*
 * INIT BASE IMAGE FILLED WITH BACKGROUND COLOR
 */

$tileWidth = $tileHeight = 28;
$numberOfTiles = 12;
$pxBetweenTiles = 1;
$leftOffSet = $topOffSet = 1;

$mapWidth = $mapHeight = ($tileWidth + $pxBetweenTiles) * $numberOfTiles;

$mapImage = imagecreatetruecolor($mapWidth, $mapHeight);
$bgColor = imagecolorallocate($mapImage, 50, 40, 0);
imagefill($mapImage, 0, 0, $bgColor);

/*
 *  PUT SRC IMAGES ON BASE IMAGE
 */

function indexToCoords($index)
{
 global $tileWidth, $pxBetweenTiles, $leftOffSet, $topOffSet, $numberOfTiles;

 $x = ($index % 12) * ($tileWidth + $pxBetweenTiles) + $leftOffSet;
 $y = floor($index / 12) * ($tileWidth + $pxBetweenTiles) + $topOffSet;
 return Array($x, $y);
}

foreach ($srcImagePaths as $index => $srcImagePath)
{
 list ($x, $y) = indexToCoords($index);
 $tileImg = imagecreatefrompng($srcImagePath);

 imagecopy($mapImage, $tileImg, $x, $y, 0, 0, $tileWidth, $tileHeight);
 imagedestroy($tileImg);
}

/*
 * RESCALE TO THUMB FORMAT
 */
$thumbSize = 200;
$thumbImage = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumbImage, $mapImage, 0, 0, 0, 0, $thumbSize, $thumbSize, $mapWidth, $mapWidth);

header ("Content-type: image/png");
imagepng($thumbImage);

?>

Source

它肯定会工作。用了我几次:)

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

php getdate()-小时不正确 - php

我在iframe中将php用于计数器。我正在使用getdate();功能。我的当地时间不是下午12:16,如果我使用getdate();从php获取现在的本地时间,小时显示为13。我从php getdate()回答的值应该不是12吗?$d1=getdate(); $hournew=$d1['hours']; echo $hournew .…

php:是否有充分的理由引用所有数组键/索引? - php

我正在遍历别人的代码,他们总是避免转义其数组键。例如:$ row_rsCatalogsItems [名称]代替$ row_rsCatalogsItems ['名称']因此,我不断地对自己接触的所有事物进行微小的更改,以应对这些惰性。但是现在我想知道这样做是否有很多好处。我得到它会在默认为字符串之前检查常量(我在处理常量时会讨厌php中的行为,因为即使未定义,…

PHP:将字符串拆分为字母和数字部分的最佳方法 - php

我有几个格式的字符串AA11 AAAAAA1111111 AA1111111 分离字符串的字母和数字部分的最佳方法(最有效)? 参考方案 如果它们都是一系列字母,然后是一系列数字,并且没有非字母数字字符,那么sscanf()可能比regexp更有效$example = 'AAA11111'; list($alpha,$numeric) =…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…