简体   繁体   中英

can't do imagejpeg() for multiple images

I have this code and I need to make this work:

         if ($handle = opendir("images/")) { $i=0;
                   while (false !== ($file = readdir($handle))){
                    if ($file != "." && $file != ".." && $file != "Recursive Dir_Renfiles_dirname-filename.php") {
                        $filename[$i]=$file;
                        $i++;
                    }}}
                    //print_r($filename);

foreach($filename as $filename){
    $percent = 0.5;

    // Content type
    header('Content-Type: image/jpeg');

    // Get new dimensions
    list($width, $height) = getimagesize($filename);
    $new_width = $width * $percent;
    $new_height = $height * $percent;

    // Resample    
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    // Output
    imagejpeg($image_p, null, 100);    
}

I there a way to make this work? Or is there another way to do this?

If you want multiple images on one page, you should use this PHP file to produce one image, and then use the PHP file as the source in an HTML image tag, like this:

<img src="picture.php?img_id=1" />
<img src="picture.php?img_id=2" />


NOTE:

When getting the filename via $_GET , it would be a good idea to watch out for unexpected characters, especially slashes. Someone could call the URL like this: picture.php?filename=../badImage.jpg , which may display a file in a different folder than you want them to have access to. So perhaps do something like $_GET["filename"] = stripslashes($_GET["filename"]); or something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM