使用PHP生成多个图像

时间:2013-01-30 14:58:30

标签: php image-processing gd

代码

$files = scandir("images");
$exclude = array(".", "..");

$images = array_diff($files, $exclude);

foreach($images as $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);
}

问题

代码的调整大小部分工作正常,但它只输出第一个调整大小的图像。我怎样才能使它转储所有已调整大小的图像?

2 个答案:

答案 0 :(得分:2)

你不能这样做。 imagejpeg“打印”图像的字节(这就是为什么,结合正确的标题,您在浏览器中看到该图片)。但是,打印多个图像(以及尝试更改标题)将无法正常工作,因为您正在“修改”第一个图像的字节,而不是将新图像附加到输出。如果你想使用这样的解决方案,你可以:

  1. 保存服务器中的每个图像,然后以其他方式访问这些图像(通过打印指向图片的<img>标签或投放某种列出图像的结构)。

    < / LI>
  2. 如果您不想保存它们,您始终可以使用base64对字节进行编码,然后将其用作<img src>

    ob_start ();
    imagejpeg ($new_image);
    $image_data = ob_get_contents ();
    ob_end_clean ();
    $image_data_base64 = base64_encode ($image_data);
    echo '<img src="data:image/jpg;base64,'.$image_data_base64.'" />';
    
  3. 你应该每次都这样做。

    非常重要:在php中输出(或完成你的工作)图像对象后总是使用imagedestroy($new_image):它会释放资源,所以你不会收到“内存”筋疲力尽“错误很容易。

答案 1 :(得分:1)

该行:

header("Content-Type: image/jpeg");

让您的浏览器了解您的内容是 图片。

如果您在文件夹中选择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();

}
相关问题