开发者

Efficient way to store images to DB from PHP GD image object

I need to generate and store a lot of small (1-10KB) PNG images (>10 millions) to the database. The only thing I care is images/s throughput. For now I know two ways of storing GD image object to the database:

Use output buffer:

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

Use temporary file (tmpfs/ramfs):

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

Update. There is a 3rd method: Us开发者_如何学JAVAe PHP memory stream:

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

My question is are there any other ways to write image to DB? Any pros/cons of each method. Maybe it is worth to write a custom stream, which writes data to DB directly?

Note: Storing images to filesystem is NOT an option.

Benchmark results:

Efficient way to store images to DB from PHP GD image object


You could try letting imagepng() writing to a memory stream instead of a file.

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

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

Although I'm not sure the imagepng() function can handle I/O streams, if it does, it could be a nice alternative.


I don't think there is any other way: imagepng() doesn't have the option of returning the data stream (a design error in my eyes, but what are you going to do).

Which of the abovementioned ways is better for you, you should benchmark; writing a stream wrapper might be worth it, because it saves the step of writing the file.


Writing image to temporary file would only increase disk I/O, so using memory buffer is a good choice. You can then upload them into DB with prepared statement's send_long_data function. (But you probably already know that.)


Both are unlikely to influence your total runtime since openreadclose'ing a cached file on linux these days takes about 20 microseconds. Generating and compressing the images is going to be the time-eater (you should optimize first the part that takes the most time). If you really want to save 1% on your total time, use the ob_() functions.

Anyway, if you want to store those in a database, examine your database connection library performance with BLOBs, some are pretty awful, others are fast.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜