从 mysql 存储的图像创建缩略图

发布于 2024-12-31 22:37:35 字数 1611 浏览 2 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静 2025-01-07 22:37:35

这就是为什么在数据库中存储文件是一个坏主意的众多原因之一......

getimagesize() 适用于文件,而不是字符串。既然你已经在字符串中获得了原始图像,请使用 [imagecreatefromstring()][1],然后使用 imagesx() 和 imagesy() 函数来获取尺寸:

while( $row = mysql_fetch_array( $result ) ) {
    $uploaded_image = base64_decode( $row['image'] );
    $image = imagecreatefromstring( $uploaded_image );
    $uploaded_x = imagesx($image); // X dimension
    $uploaded_y = imagesy($image); // Y dimension
    etc....

请注意,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:

while( $row = mysql_fetch_array( $result ) ) {
    $uploaded_image = base64_decode( $row['image'] );
    $image = imagecreatefromstring( $uploaded_image );
    $uploaded_x = imagesx($image); // X dimension
    $uploaded_y = imagesy($image); // Y dimension
    etc....

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

海之角 2025-01-07 22:37:35

您走在正确的轨道上,问题是 $uploaded_image 是图像的内容而不是文件路径。 getimagesize 需要文件名作为第一个参数。

从概念上讲,这是需要进行的更改:

  • 将 $uploaded_image 的内容写入临时位置
  • 在该临时位置上调用 getimagesize

以下是一些示例代码(未经测试):

$uploaded_image = base64_decode( $row['image'] );  //this is from your code above, should work fine
$uploaded_temp_file = tempnam(sys_get_temp_dir(), 'user_image');    //generate a temporary file name in your OS's temp folder
$uploaded_temp_res = fopen($uploaded_temp_file, 'wb');    //open the file for binary write
fwrite($uploaded_temp_res, $uploaded_image);   //commit binary data to the file
fclose($uploaded_temp_res);     //close the file handle
$size = getimagesize($uploaded_temp_file);      //finally, get the image size

请注意,您将需要添加错误处理以及不添加的内容。

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:

  • write the content of $uploaded_image to a temporary location
  • call getimagesize on that temporary location

Here's some example code (untested):

$uploaded_image = base64_decode( $row['image'] );  //this is from your code above, should work fine
$uploaded_temp_file = tempnam(sys_get_temp_dir(), 'user_image');    //generate a temporary file name in your OS's temp folder
$uploaded_temp_res = fopen($uploaded_temp_file, 'wb');    //open the file for binary write
fwrite($uploaded_temp_res, $uploaded_image);   //commit binary data to the file
fclose($uploaded_temp_res);     //close the file handle
$size = getimagesize($uploaded_temp_file);      //finally, get the image size

Note that you'll want to add error handling and what not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文