我有一个包含 5 张图像的文件夹,我想通过 PHP 将其编译成垂直图像精灵。我遍历了所有获取数据的项目并将其保存到一个数组中:
Array
(
[0] => Array
(
[location] => uploads/test1/android.png
[height] => 175
[width] => 175
[vertical] => 0
)
[1] => Array
(
[location] => uploads/test1/autocad.png
[height] => 225
[width] => 225
[vertical] => 175
)
[2] => Array
(
[location] => uploads/test1/betting.png
[height] => 512
[width] => 512
[vertical] => 400
)
[3] => Array
(
[location] => uploads/test1/calculator.png
[height] => 200
[width] => 200
[vertical] => 912
)
[4] => Array
(
[location] => uploads/test1/ccleaner.png
[height] => 256
[width] => 256
[vertical] => 1112
)
)
垂直是该图像的起始 y 坐标。这是我当前的PHP:
$background = imagecreatetruecolor($horizontal, $vertical);
$newimg = "sprite.jpg";
foreach($images as $i)
{
$tmp = imagecreatefromjpeg($i['location']);
imagecopy($background, $tmp, 0, $i['vertical'], 0, 0, $i['width'], $i['height']);
imagedestroy($tmp);
}
imagejpeg($background, $newimg);
$images 是上面的数组。当我创建数组时,我计算水平和垂直变量(我已经验证它们是正确的)。我已经用 PHP 尝试了一些东西,并且在某一时刻设法创建了正确大小 (512x1368) 的黑色图像,但这与我来的时候差不多。任何想法为什么这不起作用?
这是更新的代码:
$background = imagecreatetruecolor($horizontal, $vertical);
$newimg = "sprite.png";
foreach($images as $i)
{
$tmp = imagecreatefrompng($i['location']);
imagecopy($background, $tmp, 0, $i['vertical'], 0, 0, $i['width'], $i['height']);
imagedestroy($tmp);
}
imagepng($background, $newimg);
我什至尝试注释掉“imagedestroy”,看看这是否是罪魁祸首。该代码不会生成任何东西。零。文件夹和图像权限都是 777。在这一点上,我什至不确定下一步该尝试哪个方向。有什么想法吗?
还尝试了“imagecreatefromstring”,它也没有产生任何结果(实际上没有创建文件)。
$frame = imagecreatefromstring(file_get_contents($i['location']));
imagecopy($background, $frame, 0, $i['vertical'], 0, 0, $i['width'], $i['height']);
imagedestroy($frame);