1

我正在尝试动态生成缩略图。有简化的例子。用户请求 /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 

我将其重写为真实路径 /images/999/400x500.jpg 但此图像不存在,我想即时生成缩略图。

4

3 回答 3

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);
?>

现在将图像路径从 eg 更改/images/frontpage/head.jpg/imgresize/300x500/images/frontpage/head.jpg(300=width, 500=height)。如果图像已经调整大小,则根本不会调用 PHP 脚本(RewriteCond %{REQUEST_FILENAME} !-f在 .htaccess 中),只会将图像提供给用户。如果调整大小的图像不存在,它将被创建,另存为/imgresize/300x500/images/frontpage/head.jpg(因此来自第一个请求的虚拟路径将成为所有后续请求的真实路径)并发送给用户。

于 2013-11-08T10:08:49.630 回答
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]
于 2013-11-08T11:14:04.093 回答
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]

因此,如果找不到真实图像,脚本将生成它!

于 2013-11-08T11:18:54.463 回答