如何显示Blob数据中的缩略图 [英] How can I display thumbnails from Blob data

查看:207
本文介绍了如何显示Blob数据中的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手,我正试图建立一个图片网站.我设法使上传功能正常工作(作为Blob发送到数据库并使用自定义url显示).现在,我想检索此Blob数据并自动为其创建缩略图.不幸的是,我只能看到带有指定宽度和高度的黑色背景的画布格式.有人可以指出我正确的方向吗?

I'm new to php and I'm trying to build a photo website. I've managed to get the upload function working properly (send as blob to database and displaying it using custom url). Now, I want to retrieve this blob data and automaticly make a thumbnail for it. Unfortunately I only see the canvas format with a black background with the width and height I specified. Can someone point me in the right direction?

get_thumb_function.php:

get_thumb_function.php:

// db_connect
include 'databaseconnection.php';

// get id
$id = $_GET['id'];

// select image from id
$query = mysqli_query($db,"SELECT * FROM images WHERE id='$id'");

$rij = mysqli_fetch_array($query);

// content image
$content = $rij['image']; 

// get content type
header('Content-type: image/jpg');

$filename = $content;

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = 100;
$new_height = 100;

// 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
$thumb = imagejpeg($image_p, null, 100);
echo $thumb;

这是我尝试在其中检索缩略图的脚本. index.php

This is the script in which I try to retrieve the thumbnail image. index.php

// Alle images uit de database
$query = mysqli_query($db, "SELECT * FROM images LIMIT 0, 9"); // Maximaal 9 foto's

//$id = $row['id'];
while ($row = mysqli_fetch_assoc($query)) {?>
      <a href="image.php?id=<?php echo $row['id']; ?>"><img src="get_thumb_function.php?id=<?php echo $row['id']; ?>"></a>

<?php
}//endwhile
?>

推荐答案

首先,将图像存储在数据库中是一个坏主意.文件系统在处理文件方面更好(因为它是一个 file 系统).服务也更快,因为Web服务器不必处理PHP请求,但可以直接输出文件的内容.它称为服务静态文件",请在Google上对其进行阅读.只需将它们存储在文件系统上,然后将文件名保存在数据库中即可.然后可以将其用作img标签的src参数的图像URL的基础.

First of all, it's a bad idea to store images in the database. The file system is way better at dealing with files (as it's a file system). It's also faster to serve, as the web server doesn't have to process a PHP request, but can output the contents of the file directly. It's called "serving a static file", please Google it and read up on it. Just store them on the file system and save the file name in the database. That can then be used as a base for the image URL for a src parameter of an img tag.

第二,在每次请求时生成缩略图都是一个坏主意.生成一次,存储结果(如前所述,最好在文件系统上)并在需要时提供服务,效率更高.在服务器加载和页面加载方面要好得多(记住,服务于静态文件比服务于PHP请求要快得多.)

Secondly, it's a bad idea to generate a thumbnail every time it's requested. It's much more efficient to generate it once, store the outcome (as said, preferable on the file system) and serve that when needed. Much better in terms of server load and page loading (it's faster to serve a static file than to serve a PHP request, remember).

由于这两个建议,我很犹豫回答这个问题.但是,我觉得给了您这些建议后,您便可以自己决定是否继续追求这条路线.

Because of those two suggestions I'm quite hesitant to answer this question. However, I feel that having given you those suggestions you can decide for yourself if still pursuing this route is the right choice.

您需要阅读 imagejpeg() 函数.它不返回图像数据.输入null作为第二个参数时,将直接回显该文件.这就是您想要的,因此删除最后一行(echo $thumb;)就足以解决此问题.然后,可以丢弃$thumb变量,除非您要检查函数调用是否成功(实际上,这对于调试脚本可能很有用).

You need to read up on the imagejpeg() function. It doesn't return the image data. As you give null as the second argument the file will be echoed directly. It's what you want, so deleting the last line (echo $thumb;) is enough to solve this problem. The $thumb variable can then be disposed of, unless you want to check if the function call was sucessful or not (which, in fact, may be useful in debugging the script).

关于未显示调整大小的图像的原因,您需要确保检索到的图像数据有效(在header()调用后直接在同一脚本中使用echo $content;,紧随其后退出) JPEG图像.全部排序后,请给出结果,因为没有这些信息就无法确定脚本是否正确.

As for why the resized image is not shown, you need to make sure that the retrieved image data is valid (echo $content; in the same script directly after the header() call, exiting directly after it) and that it's a JPEG image. When that's all sorted, give the results of that, as it's impossible to determine if the script is correct without that information.

最后,请不要忽略我的前两个建议,因为这是公认的良好做法.

Finally, please don't ignore my two first suggestions, as those are generally accepted as being good practices.

这篇关于如何显示Blob数据中的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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