php - 将图像从 PHP GD 图像对象存储到数据库的有效方法

标签 php gd

我需要生成大量小 (1-10KB) PNG 图像(>1000 万)并将其存储到数据库中。 我唯一关心的是图像/s 吞吐量。现在我知道两种将 GD 图像对象存储到数据库的方法:

使用输出缓冲区:

ob_start();
imagepng($image);
$imageData = ob_get_contents();
ob_end_clean();

使用临时文件(tmpfs/ramfs):

$tmpFilePath = '/dev/shm/file_000.png';
imagepng($image, $tmpFilePath);
$imageData = file_get_contents($thumbnail);

更新。还有第三种方法:使用 PHP 内存流:

// PHP streams are NOT supported
$tmpFilePath = 'php://memory';
imagepng($image, $tmpFilePath);
$imageData = file_get_contents($tmpFilePath);

我的问题是还有其他方法可以将图像写入数据库吗?每种方法的任何优点/缺点。 也许值得编写一个自定义流,将数据直接写入数据库?

注意:将图像存储到文件系统不是一种选择。

基准测试结果:

Generating and saving 10k PNG images

最佳答案

您可以尝试让 imagepng() 写入 memory stream而不是文件。

$tmpFilePath = 'php://memory/file_000.png';

imagepng($image, $tmpFilePath);
$imageData = file_get_contents($tmpFilePath);

虽然我不确定 imagepng() 函数是否可以处理 I/O 流,但如果可以,它可能是一个不错的选择。

关于php - 将图像从 PHP GD 图像对象存储到数据库的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004894/

相关文章:

javascript - 如何在 codeigniter 中使用同名的多个输入插入数据

PHP 类扩展不起作用,为什么这是正确扩展类的方法?

javascript - 使用 AJAX 为 plupload 回显值

php - 如何使用 PHP 找到图像的 "majority colors"?

php - 两个不寻常的 PHP 运算符一起用于获取图像像素颜色,请解释

php - 图像 GD 和 png mask

php - 从php中的ttf-font获取单个字符的宽度?

Javascript 函数操作

php - Laravel 5.0 - 共享主机 - Artisan 无法正常工作

PHP - 将背景颜色更改为透明