该行:
header("Content-Type: image/jpeg");
让您的浏览器了解您的内容是图像。
这就是如果您在一个文件夹中选择 2 个图像,右键单击并打开它们。您的图像不会像只有一张一样显示在一起。
完成工作的一种方法是创建一个执行 2 件事的页面:
- 如果有参数则显示图像
<img>
如果没有参数,则将所有图像显示为标签
这是一个例子。
// The argument is set: we display one image as png
if (array_key_exists('img', $_GET)) {
$image = $_GET['img'];
// security: prevents access to unauthorized directories (by giving "../../file.jpg" as file)
$goodPath = realpath('images');
$wantedImage = realpath("images/{$image}");
if (strncmp($wantedImage, $goodPath, strlen($goodPath)) != 0) {
die();
}
// if user wants an image that does not exists, we prevent GD errors
if (!is_file($wantedImage))
{
die();
}
// and your code here, to display only ONE image
$original_image = imagecreatefromjpeg("images/{$image}");
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);
$new_width = 180;
$new_height = floor($original_height * ($new_width/$original_width));
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
header("Content-Type: image/jpeg");
imagejpeg($new_image);
die();
// No argument: we display all images as image tags
} else {
// security: prevents from xss exploits
$self = htmlentities($_SERVER['PHP_SELF']);
// glob will get all files that matches your pattern
$files = glob("images/*.[jJ][pP][gG]");
// display image tags
foreach ($files as $image) {
$image = htmlentities($image);
echo "<img src='{$self}?img={$image}' />";
}
die();
}