开发者

PHP + Imagick = Premature end of script headers?

I have the following .htaccess file in my images directory:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.jpg /images/image.php?%{REQUEST_FILENAME}

And the script, that processes the request:

<?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_ima开发者_开发技巧ge->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 */

My site is running on an Apache web server and lately it produces the following:

The site locks up, I'm not able to load any pages for 5-8 minutes. Sometimes it generates Error 500 or Error 503, sometimes not. The error log contains "Premature end of script headers" lines in the problematic time periods.

This happens once like every 3-4 days, the time of the day varies though, it happened on sunday morning and on weekday afternoon too.

My sincere question is: Is there a serious problem / flaw / hole in this code that can cause my problems? (I don't have any reason to think that the problem is caused by the script, no evidence, but I'm desperate and out of ideas.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜