如何在不将图像存储在文件夹中的情况下调整图像大小


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;
相关文章: