0

そのため、サーバーからのすべての画像リクエストをリダイレクトして、カスタム PHP スクリプトを使用しようとしています。PHP スクリプトは、 http://www.domain.com/images/imageNameHere.jpgからアクセスできるフレームワークに組み込まれています 。ユーザーは、画像が提供されていることを明らかに知りません。問題は、このメソッドが現在トップレベルで機能していることですが、バックエンドでは多くの問題を引き起こしています。たとえば、この画像で getimagesize() などの別の PHP 関数を使用しようとすると、ストリームを開けないというエラーが返されます。

さらに詳しく調べてみると、これらの画像のすべてのヘッダーが 404 not found エラーを生成していることに気付きました。(ヘッダー検出ツールをオンラインで使用し、error_log で、ロードされたすべてのイメージに 404 not found エラーが表示されることを確認して、これを見つけました) 言うまでもなく、これを使用することはできません。すべての要求を 200 にする必要があります。これを行うことで、問題の解決に役立つと思います。

これは私の現在の .htaccess ファイルです。これらの画像をシステム経由で渡し、ファイルとしてではなく URI として扱うように設定する方法が正確にはわかりません。

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

RewriteCond %{HTTP_HOST} ^i.tinyuploadz.com
RewriteRule (.*) http://www.tinyuploadz.com/images%{REQUEST_URI} [NC,L,NS]

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Image rediects?
#RewriteRule /images/(.+)\.jpg /images/$1.jpg [PT]

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

# Fix the CSS an d JS
#RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]

# Do not let people browse our directories
Options -Indexes

現在、PHP関数を設定する方法は次のとおりです(これはコントローラーによって生成されたビューです)

$day = date("d", $image['upload_time']);
$month = date("m", $image['upload_time']);
$year = date("Y", $image['upload_time']);
$url = IMAGE_URL .  $year . '/' . $month . '/' . $day . '/' . $image['image_id'] . '.' . $image['extension'];

header('Content-type: ' . $image['mime']);
switch ($image['extension'])
{
    case 'png':
        $img = @imagecreatefrompng($url);
        imagepng($img);
        imagedestroy($img);
        break;
    case 'jpg':
    case 'jpeg':
        $img = @imagecreatefromjpeg($url);
        imagejpeg($img);
        imagedestroy($img);
        break;
    case 'gif':
        if (is_ani($url))
        {
            readfile($url);
            break;
        }
        $img = @imagecreatefromgif($url);
        imagegif($img);
        imagedestroy($img);
        break;
    case 'bmp':
        // Seems like we can't do anything special for bmps
        readfile($url);
        break;
    default:
        readfile($url);
        break;
}
//readfile($url . $image['image_id'] . '.' . $image['extension']);

// We don't want Kohana to break anything else with the header change
exit();

function is_ani($filename) 
{
    if(!($fh = @fopen($filename, 'rb')))
        return false;
    $count = 0;
    //an animated gif contains multiple "frames", with each frame having a 
    //header made up of:
    // * a static 4-byte sequence (\x00\x21\xF9\x04)
    // * 4 variable bytes
    // * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)

    // We read through the file til we reach the end of the file, or we've found 
    // at least 2 frame headers
    while(!feof($fh) && $count < 2) 
    {
        $chunk = fread($fh, 1024 * 100); //read 100kb at a time
        $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
    }

    fclose($fh);
    return $count > 1;
}

問題について助けてくれてありがとう:)

4

2 に答える 2

0

HTTP リクエストは問題ではありません... getimagesize が画像を取得するときは、別のブラウザーと同じであるためです。URL を urlencode にフォーマットしていることを確認してください。

問題を解決するために、ライブ URL を取得する可能性はありますか?

于 2012-03-06T20:43:35.010 に答える
0

これを変更してみてください:

#RewriteRule /images/(.+)\.jpg /images/$1.jpg [PT]

これに:

RewriteRule /images/(.+)\.jpg /images/$1.jpg [PT,L]

私が疑うのは、Apache は書き換えルールを解析しますが、それをフィルター処理してRewriteRule .* index.php/$0 [PT]行に戻すだけなので、最終的にすべてを index.php に渡しているということです。

于 2012-03-06T09:39:32.100 に答える