如何调整图像大小而不将其存储在文件夹中 [英] How to resize image without storing it on a folder

查看:137
本文介绍了如何调整图像大小而不将其存储在文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经坚持了几个小时。我已经厌倦了谷歌任何解决方案,但没有找到解决方案。所以任何帮助都非常感谢。

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.

我的问题:

我正在使用表格上传多张图片。由于数据在多个服务器上备份,我必须将它们存储在数据库中。 将图像存储在文件夹上不是解决方案。我认为我可以将图像保存到数据库中,但我需要创建所有正在上传的图像的缩略图。所以这是我的问题,我如何从这些tmp文件创建缩略图。我创建使用 imagecreate 它不起作用,我无法获取该缩略图的内容并将其保存到数据库中。

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.

请提供建议。

感谢您的帮助。

干杯

推荐答案

这是你的大问题:

return $dst;

$ dst 是图片资源,不是图像数据。

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

您应该使用 imagejpeg() imagepng()改为发送图像数据。

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.

所以,

return $dst;

替换为:

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

这篇关于如何调整图像大小而不将其存储在文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆