私は最近、この問題だけを解決する必要があり、この単純なキャッシュ ソリューションを実装しました。
<?php
function send($name, $ext) {
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/$ext");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
}
error_reporting(E_ALL);
ini_set('display_errors', 'On');
if (isset($_REQUEST['fp'])) {
$ext = pathinfo($_REQUEST['fp'], PATHINFO_EXTENSION);
$allowedExt = array('png', 'jpg', 'jpeg');
if (!in_array($ext, $allowedExt)) {
echo 'fail';
}
if (!isset($_REQUEST['w']) && !isset($_REQUEST['h'])) {
send($_REQUEST['fp']);
}
else {
$w = $_REQUEST['w'];
$h = $_REQUEST['h'];
//use height, width, modification time and path to generate a hash
//that will become the file name
$filePath = realpath($_REQUEST['fp']);
$cachePath = md5($filePath.filemtime($filePath).$h.$w);
if (!file_exists("tmp/$cachePath")) {
exec("gm convert -quality 80% -colorspace RGB -resize " .$w .'x' . $h . " $filePath tmp/$cachePath");
}
send("tmp/$cachePath", $ext);
}
}
?>
私が気づいたいくつかのこと:
- cuda 処理で変換をテストしていませんが、Graphicsmagick は imagemagick よりもはるかに高速に変換されます。
- 最終製品では、言語のネイティブ グラフィック ライブラリを使用して、このコードを ASP に再実装しました。これもはるかに高速でしたが、メモリ不足エラーが発生すると壊れてしまいました (私のワークステーションでは問題なく動作しましたが、4GB RAM サーバーでは動作しませんでした)。