我正在尝试动态生成缩略图。有简化的例子。 用户请求 /fakeimagespath/image_name.400x400.jpg 其中400x400是缩略图大小。 如果此图像存在于文件夹/realpath/image_name.400x400.jpg中 - 我们通过.htaccess内部重定向显示它。
如果此图片不存在,我想要内部重定向 /generator.php?name=image_name&size=400x400 这个php文件将从源文件生成它并使用jpeg mime类型输出。
请帮助修改mod_revrite规则。
#index.php prints $_SERVER for debug purposes.
#cup.jpg, girl.jpg - existing files, 404.jpg - not exists
RewriteEngine on
RewriteCond $1 ^fakeimage
RewriteRule ^(fakeimage\-(cup|girl|404)\.jpg)$ /realpath/$2.jpg
RewriteCond $1 -f
RewriteRule ^(.*)$ /index.php?found=$1 [L]
RewriteCond $1 !-f
RewriteRule ^(.*)$ /index.php?notfound=$1 [L]
更新(示例):
/shop/some-user-friendly-alias.999.400x500.jpg
我把它重写为REAL路径/images/999/400x500.jpg 但是这个图像不存在,我想在运行中生成缩略图。
答案 0 :(得分:2)
以下是我使用的脚本。您可以根据自己的需要进行调整。
使用以下代码将.htaccess放入/
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^imgresize/(.+)$ imgresize/imgresize.php
以下代码位于/imgresize/imgresize.php
<?php
// no image requested
if(!isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI'])) {
header('HTTP/1.0 404 Not Found');
exit;
}
// thumbnail already exists (should never be called as the .htaccess is handling this)
if(file_exists('../'.$_SERVER['REQUEST_URI'])) {
header('Content-type: image');
readfile('../'.$_SERVER['REQUEST_URI']);
exit;
}
// extract new width, new height and filename from request
preg_match_all('%/imgresize/([0-9]+)x([0-9]+)/(.+)$%isU', $_SERVER['REQUEST_URI'], $matches);
$new_width = $matches[1][0];
$new_height = $matches[2][0];
$filename = $matches[3][0];
// file doesn't exist
if(!file_exists('../'.$filename)) {
header('HTTP/1.0 404 Not Found');
exit;
}
// get width, height and file format from the original image
list($ori_width, $ori_height, $type, $attr) = getimagesize('../'.$filename);
// create new image
if($type == IMAGETYPE_JPEG) {
$ori_image = imagecreatefromjpeg('../'.$filename);
}
elseif( $type == IMAGETYPE_GIF ) {
$ori_image = imagecreatefromgif('../'.$filename);
}
elseif( $type == IMAGETYPE_PNG ) {
$ori_image = imagecreatefrompng('../'.$filename);
}
// calculate new image ratio
$src_x = $src_y = 0;
if($ori_height/$new_height > $ori_width/$new_width) {
$old_height = $ori_height;
$ori_height = $ori_width/($new_width/$new_height);
$src_y = $old_height/2 - $ori_height/2;
}
else {
$old_width = $ori_width;
$ori_width = $ori_height/($new_height/$new_width);
$src_x = $old_width/2 - $ori_width/2;
}
// resize original image
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $ori_image, 0, 0, $src_x, $src_y, $new_width, $new_height, $ori_width, $ori_height);
// create path
$new = $new_width.'x'.$new_height.'/'.$filename;
$parts = explode('/', $new);
$path = '';
for($i=0;$i<sizeof($parts)-1;$i++) {
$path .= $parts[$i].'/';
if(!file_exists($parts[$i])) {
mkdir($path);
}
}
// save the created image
imagejpeg($new_image, $new, 90);
// sent the created image to the user
header('Content-Type: image');
imagejpeg($new_image, null, 90);
?>
现在更改图像路径,例如/images/frontpage/head.jpg
到/imgresize/300x500/images/frontpage/head.jpg
(300 =宽度,500 =高度)。如果图像已经调整大小,则根本不会调用PHP脚本(.htaccess中的RewriteCond %{REQUEST_FILENAME} !-f
),只会将图像提供给用户。如果调整大小的图像不存在,它将被创建,保存为/imgresize/300x500/images/frontpage/head.jpg
(因此第一个请求的虚拟路径将成为所有后续请求的真实路径)并发送给用户。
答案 1 :(得分:1)
这样的事情对你有用:
RewriteEngine On
# images exists in /realpath/
RewriteCond %{DOCUMENT_ROOT}/realpath/$1 -f
RewriteRule ^fakeimages/([^.]+\.[^.]+\.jpg)$ /realpath/$1 [L,NC]
# images doesn't exist in /realpath/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^fakeimages/([^.]+)\.([^.]+)\.jpg$ /generator.php?name=$1&size=$2 [L,NC,QSA]
答案 2 :(得分:0)
解决。很抱歉给您带来不便。 由两个.htaccess文件完成。 首先 - 内部根目录: /.htaccess
RewriteEngine on
RewriteCond $1 ^fooimage
RewriteRule ^(fooimage\-(cup|girl|404)\.jpg)$ /realpath/$2.jpg [L]
第二个内部真实路径: /realpath/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^generate\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ generate.php?notfound=$1 [L]
因此,如果未找到真实图像,脚本将生成它!