我有 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);
?>
Run Code Online (Sandbox Code Playgroud)
我怎样才能制作那样的脚本?
小智 5
第 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');
Run Code Online (Sandbox Code Playgroud)
第 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);
Run Code Online (Sandbox Code Playgroud)
第 3 步:
考虑您希望源图像在哪个坐标处结束 有一些不同的方法可以指定这一点,但是如果您正在处理许多相同大小的图像,那么编写一个映射(数组)的小函数是有意义的索引到一组 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);
}
Run Code Online (Sandbox Code Playgroud)
第 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);
}
Run Code Online (Sandbox Code Playgroud)
请注意我们在那里如何使用 indexToCoords() 函数——当然,我们不希望所有 > 源图像都在同一位置。
第 5 步(插曲):使用 PHP 调整图像大小 我们用于将源图像放在基础图像上的相同 imagecopy() 函数也可用于调整图像大小。如果你想自动生成缩略图很方便!您可以这样做:
/* * 重新调整为拇指格式 */
$thumbSize = 200;
$thumbImage = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumbImage, $mapImage, 0, 0, 0, 0, $thumbSize, $thumbSize, $mapWidth, $mapWidth);
Run Code Online (Sandbox Code Playgroud)
最后一步:设置 header 告诉浏览器有图像来了,并输出最终图像
/* * 输出缩略图图像 */
header ("Content-type: image/png");
imagepng($thumbImage); //change argument to $mapImage to output the original size image
Run Code Online (Sandbox Code Playgroud)
就是这样!请注意,您可能不想要均匀填充的背景,而是想要真实的背景图像——您可以通过在步骤 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);
?>
Run Code Online (Sandbox Code Playgroud)
它肯定会起作用。多次使用我自己:)