Generate multiple images with PHP

好久不见. 提交于 2019-12-12 01:44:57

问题


Code

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

Problem

The resizing part of the code works just fine, but it only outputs the first resized image. How can I make it so that it dumps all of the resized images?


回答1:


You can't do such the way you are doing it. imagejpeg "prints" the bytes of the image (which is why, combined with the proper header, you see that picture in your browser). However, printing multiple images (plus trying to change the header) won't work, as you are "modifying" the bytes of the first image, and not appending new images to the output. If you want to use a solution like that, you could:

  1. Save every image in your server and then access those images in another way (either by printing <img> tags pointing to the pictures or throwing some kind of structure listing the images).

  2. If you don't want to save them, you can always encode the bytes using base64 and then using that as an <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.'" />';
    

You should do so every time.

And very important: always use imagedestroy($new_image) after outputting (or finishing your work with) an image object in php: it will release resources, so you won't receive a "Memory exhausted" error that easy.




回答2:


The line :

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

make your browser understand that your content is an image.

That's about if you select 2 images in a folder, right-click and open them. Your images will not be displayed together as if they were only one.

One way to do your job is to create a page that does 2 things :

  • display an image if it has arguments
  • display all images as <img> tags if it has no argument

Here is an example.

// 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();

}


来源:https://stackoverflow.com/questions/14606941/generate-multiple-images-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!