簡體   English   中英

不能對多個圖像執行imagejpeg()

[英]can't do imagejpeg() for multiple images

我有以下代碼,需要進行以下工作:

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

我有辦法使這項工作嗎? 還是有其他方法可以做到這一點?

如果要在一頁上顯示多個圖像,則應使用此PHP文件生成一個圖像,然后將PHP文件用作HTML圖像標記中的源,如下所示:

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


注意:

通過$_GET獲取文件名時,最好注意不要出現意外字符,尤其是斜杠。 有人可以這樣調用URL: picture.php?filename=../badImage.jpg ,它可能會在與您希望他們訪問的文件夾不同的文件夾中顯示文件。 因此,也許可以執行類似$_GET["filename"] = stripslashes($_GET["filename"]); 或者其他的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM