How can I display thumbnails from Blob data

对着背影说爱祢 提交于 2020-01-01 19:56:54

问题


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:

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

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
?>

回答1:


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.

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.

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).

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.



来源:https://stackoverflow.com/questions/22962042/how-can-i-display-thumbnails-from-blob-data

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