从 mysql 存储的图像创建缩略图
我有一个名为 users 的表,每行(用户)都有一个图像。
使用以下代码将图像作为 BLOB 存储在 mysql 数据库中:
$filename = $_FILES['image']['tmp_name'];
$size = getimagesize($filename);
$handle = fopen( $filename , "rb" );
$content = fread( $handle , filesize( $filename ) );
fclose( $handle );
unlink($filename);
$image = base64_encode( $content );
// .... send query to database ....
这一开始是有效的,但是随着越来越多的图像必须在每个页面上显示,它会被真正加载,所以现在我想创建缩略图,保存在服务器的文件系统,使用 php GD。
我有以下代码,但我不知道该怎么做才能从数据库读取图像,以便创建其缩略图。
$query = "SELECT * FROM users;";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) ) {
$uploaded_image = base64_decode( $row['image'] );
$size = getimagesize($uploaded_image); <--------------------
$dimension_x = 73;
$dimension_y = 73;
$directory = 'views/images/generated/people/';
do{
$filename = random_32();
$filename = $directory.$filename.'.jpg';
} while( file_exists($filename) );
$image = imagecreatefromjpeg( $uploaded_image );
$thumb = imagecreatetruecolor( $dimension_x , $dimension_y );
$size = getimagesize( $uploaded_image );
imagecopyresampled( $thumb , $image , 0 , 0 , 0 , 0 , $dimension_x , $dimension_y , $size['0'], $size['1']);
imagejpeg( $thumb , $filename , 100);
}
我试图找到应该在箭头所在的位置放置什么以使脚本正常工作。
它现在所做的就是输出
Warning: getimagesize(ÿØÿà): failed to open stream: No such file or directory in /var/www/play/ns4/models/create_thumbs_people.php on line 12
数据库中的每个条目。
---- 编辑 ----
忘记提及,random_32() 是一个生成 32 个字符长的随机字符串以便命名图像的函数。
I have a table named users and each row (user) has an image.
The image is stored as a BLOB in a mysql database using this code:
$filename = $_FILES['image']['tmp_name'];
$size = getimagesize($filename);
$handle = fopen( $filename , "rb" );
$content = fread( $handle , filesize( $filename ) );
fclose( $handle );
unlink($filename);
$image = base64_encode( $content );
// .... send query to database ....
This worked at first, but as more and more images have to be shown at each page, it gets really loaded, so now I want to create thumbnails, saved in the server's filesystem, using php GD.
I have the following code but I have no idea what to do in order to read the image from the database so I can create its thumbnail.
$query = "SELECT * FROM users;";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) ) {
$uploaded_image = base64_decode( $row['image'] );
$size = getimagesize($uploaded_image); <--------------------
$dimension_x = 73;
$dimension_y = 73;
$directory = 'views/images/generated/people/';
do{
$filename = random_32();
$filename = $directory.$filename.'.jpg';
} while( file_exists($filename) );
$image = imagecreatefromjpeg( $uploaded_image );
$thumb = imagecreatetruecolor( $dimension_x , $dimension_y );
$size = getimagesize( $uploaded_image );
imagecopyresampled( $thumb , $image , 0 , 0 , 0 , 0 , $dimension_x , $dimension_y , $size['0'], $size['1']);
imagejpeg( $thumb , $filename , 100);
}
I am trying to find what should I put where the arrow is to make the script work.
All it does now is output
Warning: getimagesize(ÿØÿà): failed to open stream: No such file or directory in /var/www/play/ns4/models/create_thumbs_people.php on line 12
For every entry in the database.
---- EDIT ----
Forgot to mention, random_32() is a function that generates a 32 characters long random string so that the images are named.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是为什么在数据库中存储文件是一个坏主意的众多原因之一......
getimagesize() 适用于文件,而不是字符串。既然你已经在字符串中获得了原始图像,请使用 [imagecreatefromstring()][1],然后使用 imagesx() 和 imagesy() 函数来获取尺寸:
请注意,imagecreatefromstring 实际上有一点聪明,可以计算出图像是什么类型,与相当愚蠢的 createfromgif/jpg/png/etc... 函数不同,这些函数仅适用于那些特定的文件类型。
[1]: https://www.php.net/imagecreatefromstring
And here's one of the many reasons why storing files in the database is a bad idea...
getimagesize() works on files, not strings. Since you've got the raw image in a string, use [imagecreatefromstring()][1], then the imagesx() and imagesy() functions to get the dimensions:
Note that imagecreatefromstring actually has a bit of smarts and can figure out what type the image is, unlike the quite moronic createfromgif/jpg/png/etc... functions, which only work on those particular file types.
[1]: https://www.php.net/imagecreatefromstring
您走在正确的轨道上,问题是 $uploaded_image 是图像的内容而不是文件路径。 getimagesize 需要文件名作为第一个参数。
从概念上讲,这是需要进行的更改:
以下是一些示例代码(未经测试):
请注意,您将需要添加错误处理以及不添加的内容。
You're on the right track, the issue is that $uploaded_image is the content of the image and not a file path. getimagesize requires a file name as the first argument.
Conceptually, here's the change that needs to be made:
Here's some example code (untested):
Note that you'll want to add error handling and what not.