gpt4 book ai didi

php - 用php将16张图片合并成1张大图片

转载 作者:行者123 更新时间:2023-12-01 20:16:48 25 4
gpt4 key购买 nike

我有 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);
?>

我如何制作可以做到这一点的脚本?

最佳答案

Step 1: get an array with source image locations This step is different for anybody, but in its easiest form you would define something like this:

$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');

Step 2: define some measures and initialize a blank ‘background’ image Here we use the first GD functions: imagecreatetruecolor() creates a generic ‘base’ image, imagecolorallocate() to define an RGB color and imagefill() to fill our generic image with that color.

$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);

Step 3:
think at which coordinates you want your source images to end up There are some different ways to specify this, but if you are dealing with many images of the same size it makes sense to write a small function that maps an (array) index to a set of X,Y coordinates. Here is mine which arranges them all in a 12×12 square grid:

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);
}

Step 4: loop over the source images and copy them on the base image We use function imagecopy() to do this, like this:

/** 将源图像复制到 map */

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

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

Note how we used the indexToCoords() function in there – we do not want all the >source images on the same position of course.

Step 5 (intermezzo): resizing an image with PHP The same imagecopy() function we used to put our source images on the base image can also be used to resize images. Handy if you want to generate thumbnails automatically! Here’s how you can do that:

/* * 重新调整为拇指格式 */

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

Final step: set header to tell the browser there’s an image coming, and output the final image

/** 输出缩略图*/

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 - 用php将16张图片合并成1张大图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com