【问题标题】:serving images? htaccess redirect the images folder?提供图像? htaccess 重定向图像文件夹?
【发布时间】:2012-09-17 12:42:22
【问题描述】:

我有一个 php 脚本,它可以上传图像,将其重命名为随机散列,根据散列创建文件夹,将原始图像存储在最后创建的文件夹中,最后为它创建 2 个其他缩略图...

常规图片...images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg

缩略图...images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b_t.jpg

较小的缩略图...images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b_sm.jpg

对于每张上传的照片,它都会在数据库中存储一条新记录,其中包含照片 ID 和此信息

/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b

(请注意我没有存储“.jpg”或“_t.jpg”等)

现在为了让我提供图像(我不知道这是否是一个好习惯),目前我有这个返回图像的 php 函数......

function get_image($image_id,$type = '')
{
    $sql = "SELECT * FROM photos where photo_id = {$image_id}";
    $query = $this->db->query($sql);
    $r = $query->row();

    $image = base_url().'images/'.$r->dir_1 . '/' . $r->dir_2 . '/' . $r->dir_3 . '/'.$image_id.'_'.$r->img_hash;

    if($type == 'small')
        return $image.'_sm.jpg';
    if($type == 'reg_thumb')
        return $image.'_t.jpg';
    if($type == '' || $type == 'original')
        return $image.'.jpg';

}

基本上,我想做一些像推特这样的图片网址看起来像这样的事情

https://twimg0-a.akamaihd.net/profile_images/2392722244/blah.jpg

问题...

  1. 如何实现这种类型的 url 结构?访问?
  2. 我是否以正确的方式返回图像?
  3. 我在做什么是安全、聪明和好的做法吗?如果没有,有更好的方法吗?

谢谢!!

【问题讨论】:

    标签: php image .htaccess hash serving


    【解决方案1】:

    国际海事组织, 您确实可以通过 url rewrite 做到这一点。 在 /etc/apache2/sites-available/yoursite 中,

        <Directory />
    
                RewriteEngine On
                RewriteCond %{HTTP_REFERER}          profile_images/(\w+)/(\w+|.)+$
    
    
                RewriteRule ^/profile_images/(\w+)/         index.php?image_id=$1 [L,QSA]
    
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
    
                Order allow,deny
                allow from all
        </Directory>
    

    尝试解决这个问题。我认为它与 htaccess 中的指令非常相似。

    Am I returning the images the right way?
    

    你这是什么意思?你的“回报”会起作用。我会使用 switch/case 语句而不是 ifs。

    is what im doing safe, smart, and a good practice? if not is there a better method?
    

    安全:您应该转义您的输入。但是,如果您仅重定向图像名称为字母数字的图像,那应该不是问题

    我不知道你为什么分解成这么多的目录。每个图像+缩略图一个目录就足够了。不用担心使用 b-tree 或类似的东西进行索引,仅在您的数据库中使用索引就足够了。

    最后但同样重要的是,如果我不需要在用户和图像之间建立链接,我只会使用 apache 的重写规则来完成。

    重写规则 ^/profile_images/(\w+)(_\w+)?.(jpg|png)/images/$1/$1$2.$3

    不涉及php,也不涉及db,一行就搞定了。

    https://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-as

    http://httpd.apache.org/docs/current/mod/mod_rewrite.html

    我不确定这是否有帮助,但您可能已经学到了一些东西。至少,你有另一个观点:)

    【讨论】:

    • 感谢您的回答,我忘了提及.. 我之所以使用目录数量,是因为我正在构建一个大型 Web 应用程序......我们正在查看数百万张照片上传,所以我需要图像均匀分布。我要进行 HTaccess 并更改该功能以切换大小写。再次感谢!
    • 对于上面的 htaccess,我把它放在哪里?在站点的主根目录还是在图像文件夹中?对不起,我没有 htaccess 的技能
    • 修改 /etc/apache2/sites-available/yoursite 文件并重新加载 apache。 (尽管这些行可能是 .htaccess 的一部分)
    • 均匀分布是什么意思?它在不同的硬盘上吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    相关资源
    最近更新 更多