Php 通过图像数组运行imagecreatefrompng,但数组仅返回1个图像
我试图截取从远程站点获取的图像的底部。 让它也与以下代码一起工作:Php 通过图像数组运行imagecreatefrompng,但数组仅返回1个图像,php,image,crop,php-gd,Php,Image,Crop,Php Gd,我试图截取从远程站点获取的图像的底部。 让它也与以下代码一起工作: $u = $xmlString->xpath('//*[contains(@u, "/fds/")]'); foreach($u as $result) { $itemLinks = 'http://exampleurl/'.$result['u'].'.png'; $in_filename = $itemLinks; list($width, $height) = getimagesize($
$u = $xmlString->xpath('//*[contains(@u, "/fds/")]');
foreach($u as $result) {
$itemLinks = 'http://exampleurl/'.$result['u'].'.png';
$in_filename = $itemLinks;
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 264;
$new_width = $width;
$image = imagecreatefrompng($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparentindex = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefill($new_image, 0, 0, $transparentindex);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header("Content-Type: image/png");
imagepng($new_image);
}
此代码的唯一问题是:
我从一个远程XML文件中获取图像路径,我用xpath过滤了该文件。因此,我所有完成的图像url都存储在一个数组中。但我的代码只是生成了一个图像,其中包含了我需要的完美大小
发生这种情况是因为它最终只产生1个img。也可能是因为它只返回一个名为img的图像
问题:有人知道为什么它不会返回所有图像吗
例如:
数组包含15个图像链接。
我在数组中运行我的foreach循环。
Foreach循环只返回1个图像。
您的问题是由最后两行引起的:
header("Content-Type: image/png");
imagepng($new_image);
这与在浏览器中打开单个图像文件(如.PNG)的效果相同。不能同时查看多个图像文件,除非它们嵌入到HTML页面中
如果要在浏览器中同时显示所有15个图像,则需要在处理图像时保存每个图像,然后输出一个HTML文件,如下所示:
$images = '';
foreach($u as $result) {
// your existing code...
imagepng($new_image, './'.$result['u'].'.png');
$images .= '<img src="'.$result['u'].'.png">';
}
// wrap this in valid HTML syntax (<head>, <body>, etc.)
echo $images;