gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:44:34 25 4
gpt4 key购买 nike

我需要生成大量小 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com