简体   繁体   English

不能对多个图像执行imagejpeg()

[英]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: 如果要在一页上显示多个图像,则应使用此PHP文件生成一个图像,然后将PHP文件用作HTML图像标记中的源,如下所示:

<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. 通过$_GET获取文件名时,最好注意不要出现意外字符,尤其是斜杠。 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. 有人可以这样调用URL: picture.php?filename=../badImage.jpg ,它可能会在与您希望他们访问的文件夹不同的文件夹中显示文件。 So perhaps do something like $_GET["filename"] = stripslashes($_GET["filename"]); 因此,也许可以执行类似$_GET["filename"] = stripslashes($_GET["filename"]); or something. 或者其他的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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