images ディレクトリに次の .htaccess ファイルがあります。
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.jpg /images/image.php?%{REQUEST_FILENAME}
そして、リクエストを処理するスクリプト:
<?php
/*
* Watermark module
* Uses ImageMagick lib
*
*/
// The name of the watermark image. Should be in the same directory
// as the image.php
define('WATERMARK_IMAGE', 'watermark.png');
// Watermark images with larger width than this value (pixel)
// Set to 0 (Zero) to watermark all images
define('WATERMARK_THRESHOLD_WIDTH', 218);
$filename = $_SERVER['QUERY_STRING'];
// If the requested file doesn't exist, return HTTP 404
// Should not happen, as the htaccess handles that
if (file_exists($filename))
{
// Get the last modified property of the source file for caching
$file_last_modified = filemtime($filename);
// Expirese in two months
$expires = time() + (60 * 24 * 60 * 60);
// Checking last modified date, if it's the same, then return 304 "Not Modified"
// No body response is generated.
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
&& strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $file_last_modified)
{
header("HTTP/1.1 304 Not Modified");
}
else
{
$requested_image = new Imagick($filename);
// If the marker image doesn't exist, then return the original image
if (file_exists(WATERMARK_IMAGE))
{
$watermark = new Imagick(WATERMARK_IMAGE);
// Get original image's dimensions
$requested_image_width = $requested_image->getImageWidth();
$requested_image_height = $requested_image->getImageHeight();
// Get watermark image's dimensions
$watermark_width = $watermark->getImageWidth();
$watermark_height = $watermark->getImageHeight();
// Calculate watermark position
// Current position: center - center
$position_x = ($requested_image_width - $watermark_width)/2;
$position_y = ($requested_image_height - $watermark_height)/2;
// Only watermark images larger than the threshold
if ($requested_image_width > WATERMARK_THRESHOLD_WIDTH)
{
$requested_image->compositeImage($watermark, imagick::COMPOSITE_OVERLAY, $position_x, $position_y);
}
// Destroy the marker image
$watermark->destroy();
}
// Set the headers
header("Pragma: public");
header("Content-type: image/jpeg");
header("Expires: " . gmdate("D, d M Y H:i:s", $expires) . " GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $file_last_modified) . " GMT");
// Return the response image
echo $requested_image;
// Destroy the temporary image
$requested_image->destroy();
}
}
else
{
header('HTTP/1.1 404 Not Found');
}
/* End of file image.php */
/* Location: ./images/image.php */
私のサイトは Apache Web サーバーで実行されており、最近では次のように生成されます。
サイトがロックされ、5 ~ 8 分間ページを読み込めません。エラー 500 またはエラー 503 が生成される場合と生成されない場合があります。エラー ログには、問題のある期間の「スクリプト ヘッダーの早期終了」行が含まれています。
これは 3 ~ 4 日に 1 回発生し、時間帯は異なりますが、日曜日の朝と平日の午後にも発生しました。
私の心からの質問は次のとおりです。このコードには、私の問題を引き起こす可能性のある重大な問題/欠陥/穴がありますか? (問題がスクリプトによって引き起こされていると考える理由はありません。証拠はありませんが、必死でアイデアがありません。)