所以我试图重定向来自我的服务器的所有图像请求以使用自定义 PHP 脚本。PHP 脚本内置在一个框架中,可以通过http://www.domain.com/images/imageNameHere.jpg访问该框架 。用户显然不知道图像正在提供给他们。所以现在的问题是这个方法目前在它的顶层工作,但是对于后端它会引起很多问题。例如,如果我尝试在此图像上使用另一个 PHP 函数,例如 getimagesize(),则会返回一个错误,即它无法打开流。
进一步研究此事,我注意到这些图像的所有标题都生成 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;
}
感谢您对此事的任何帮助:)