How to resize image without storing it on a folder

淺唱寂寞╮ 提交于 2019-12-10 12:33:07

问题


So, i have been stuck on this for couple of hours. I have tired to google any solutions for this but didn't find solutions. So any help is greatly appreciated.

My Issue:

I am using form to upload multiple images. Due to data being backup on multiple servers i have to stored them on database. And storing the images on folder is not a solution. I thinks I can save the image onto the database but my I need to create a thumbnail of all the images which are being uploaded. So that's my problem, how do i create a thumbnail from those tmp files. I created to use imagecreate its not working, I am not able to fetch the content of that thumbnail and save it onto the database.

This is a code i used to resize that image which doesn't return the contents.

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

This is my code which is used after form is posted: Right now it only displays all the files that are uploaded.

 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" />';


    }

I need to create a thumbnail of those files that are being uploaded. How do i achieve that.

Please advise.

Thanks for your help.

Cheers


回答1:


This is your big problem:

return $dst;

$dst is the image resource, not the image data.

You should use imagejpeg() or imagepng() to send the image data back instead.

Because those functions output the data stream to browser, we use some output buffer functions to capture the outputted image data instead of send it to the browser.

So,

return $dst;

replace with:

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


来源:https://stackoverflow.com/questions/36270233/how-to-resize-image-without-storing-it-on-a-folder

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