簡體   English   中英

如何調整圖像大小而不將其存儲在文件夾中

[英]How to resize image without storing it on a folder

所以,我已經堅持了幾個小時。 我已經厭倦了為此搜索任何解決方案,但沒有找到解決方案。 因此,非常感謝任何幫助。

我的問題:

我正在使用表單上傳多張圖片。 由於數據在多台服務器上備份,我必須將它們存儲在數據庫中。 將圖像存儲在文件夾中不是解決方案。 我想我可以將圖像保存到數據庫中,但我需要為正在上傳的所有圖像創建縮略圖。 所以這就是我的問題,我如何從這些 tmp 文件創建縮略圖。 我創建使用imagecreate它不起作用,我無法獲取該縮略圖的內容並將其保存到數據庫中。

這是我用來調整不返回內容的圖像大小的代碼。

 function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = abs(ceil($width-($width*abs($r-$w/$h))));
        } else {
            $height = abs(ceil($height-($height*abs($r-$w/$h))));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}

這是我在表單發布后使用的代碼:現在它只顯示所有上傳的文件。

 for($i=0;$i< count($_FILES['upload_file']['tmp_name']);$i++)
 {

        $size = getimagesize($_FILES['upload_file']['tmp_name'][$i]);
        $name = $_FILES['upload_file']['name'][$i];
        $type = $size['mime'];          
        $file_size_bits = $size['bits'];
        $file_size_width = $size[0];
        $file_size_height = $size[1];
        $name = $_FILES['upload_file']['name'][$i];
        $image_size = $size[3];
        $uploadedfile = $_FILES['upload_file']['tmp_name'][$i];
        $tmpName  = base64_encode(file_get_contents($_FILES['upload_file']['tmp_name'][$i]));
        $sImage_ = "data:" . $size["mime"] . ";base64," . $tmpName;
        echo '<p>Preview from the data stored on to the database</p><img src="' . $sImage_ . '" alt="Your Image" />';


    }

我需要創建正在上傳的那些文件的縮略圖。 我如何做到這一點。

請指教。

謝謝你的幫助。

干杯

這是你的大問題:

return $dst;

$dst是圖片資源,不是圖片數據。

您應該使用imagejpeg()imagepng()來發送回圖像數據。

因為這些函數將數據流輸出到瀏覽器,所以我們使用一些輸出緩沖函數來捕獲輸出的圖像數據,而不是將其發送到瀏覽器。

所以,

return $dst;

用。。。來代替:

ob_start();
imagejpeg( $dst, NULL, 100); // or imagepng( $dst, NULL, 0 );
$final_image = ob_get_contents();
ob_end_clean();
return $final_image;

暫無
暫無

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

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