创建调整后的图像缓存,但prevent滥用

编程入门 行业动态 更新时间:2024-10-24 04:46:16
本文介绍了创建调整后的图像缓存,但prevent滥用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好了,所以我对我怎么想的发球和缓存我的图片的想法。 我不知道这是正确的方式做到这一点,但如果是这样,我想知道如何去preventing滥用。

Okay so I have an idea on how I want to serve and cache my images. I don't know if this is the right way to do it but if it is, I'd like to know how to go about preventing abuse.

情况:

的index.php

<img src="images/cache/200x150-picture_001.jpg" />

图片/缓存/的.htaccess

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ images/image.php?f=$1 [L]

以上检查如果图像存在,如果不是会被改写为image.php

The above checks if the image exists, if not it will be rewritten to image.php

image.php 伪code

得到的文件名的高度和宽度

get height and width from filename

调整大小和保存图像的缓存文件夹

resize and save image to cache folder

服务与内容型和ReadFile的形象。

serve the image with content-type and readfile.

这个办法,我试图减少HTTP请求和PHP负载readfiles。 浏览器获得了为image / jpeg,如果它存在,如果没有它,将产生

This way I'm trying to reduce HTTP requests and PHP load with readfiles. The browser gets the image/jpeg if it exists and if not it will be generated.

最后,所有的图像将被缓存,并在合适的高度和宽度担任所以浏览器不会被下载过大的图像。

Eventually all images will be cached and served in the right height and width so browsers won't be downloading oversized images.

,唯一的缺点..如果你改变图像的URL不同的尺寸,你可以填满服务器。

The only catch.. if you change the image url to different dimensions, you can fill up the server.

我这样做是正确的,什么是正确的方式去做。有没有发生停止该任何机会?

Am I doing it right, what's the right way to do it. Is there any chance of stopping this from happening?

我知道这整个缓存的概念已经细化了一百万次,请赐教。

I know this whole concept of caching has been refined a million times, please enlighten me.

推荐答案

一些方法:

  • 维护允许的分辨率数组,并检查要求的分辨率是否在该数组。缺点:你不能快速增加的决议没有编辑阵列

  • Maintain an array of allowed resolutions, and check whether the requested resolution is in that array. Downside: you can't quickly add a resolution without editing the array.

如果这是一个CMS环境:允许创建的新的图像(不在缓存中还)只能通过身份验证的用户;否则拒绝该请求。当一个身份验证的用户增加了一个形象的CMS,他们preVIEW它,这样做产生的调整后的图像。缺点:不完全是容易实现

If this is in a CMS context: allow the creation of new images (that are not in the cache yet) only by authenticated users; refuse the request otherwise. When an authenticated user adds an image in the CMS, they preview it, and doing that generates the resized image. Downside: not entirely easy to implement.

更多推荐

创建调整后的图像缓存,但prevent滥用

本文发布于:2023-11-01 02:24:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   图像   调整后   prevent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!