php - 图像版本控制 - 最佳方法

标签 php codeigniter hash versioning

我正在用 php (codeigniter) 开发一个站点框架,并希望在图像上传上引入图像版本控制,以便我可以利用图像缓存。最简单的方法就是对图像进行 md5 并将其用作文件名,但我不喜欢这种方法,原因如下:

1)Not SEO friendly on the image names

2)md5 hashes seem unnecessarily long - and therefore larger database field required.

所以我正在考虑使用如下方法:

文件名以输入的图像名称开头,并使用下划线而不是空格,然后添加一个随机生成的整数,例如 8 位数字长。这意味着我必须检查该名称的现有图像,然后重新生成整数(如果存在)(尽管不太可能)。

大概我还必须为每个图像大小使用唯一的文件名,所以我想这里的解决方案是添加表示文件大小的前缀。

现在我想第一次就把这个做对,因为一旦部署框架,改变就会很痛苦,所以我真的只是在寻找输入

a)Whether my concerns are justified (particularly does the filename do anything for SEO and does the length of a random string of numbers affect it)

b)Whether there is anything else I should be concerned about or check for with my proposed approach.

c)Is there an easier approach, perhaps a hashing algorithm which produces much shorter results.

d) Is there already a ci lib out there that does this?

感谢您的意见和建议

最佳答案

这回答了您的一些问题:

用下划线替换空格不足以获得干净的文件名,因为您需要检查更多奇怪的字符,但您可以使用 CI 安全库中的 sanitize_filename() 方法: http://ellislab.com/codeigniter/user-guide/libraries/security.html

如果您确实想保留原始文件名,那么您的方法对我来说听起来不错。不过,通过简单的增量循环检查该结尾的文件是否存在,可以将文件名末尾的 8 位整数替换为“-1”、“-2”、“-3”。

文件上传库是您可以查看的东西 - http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html 。它很灵活,可以配置为保留原始文件名。从安全库获取 sanitize_filename() 来工作应该完全符合您的需要。

在我的所有 CI 应用程序中,我总是使用加密的文件名(此可选功能由 CI 文件上传类提供)。同时,我可以通过向库添加数字(如果未使用加密)或仅为其提供另一个加密名称(当加密选项打开时)来将库配置为不覆盖现有文件。我确实喜欢这种方式,因为它可以保持文件名一致干净(虽然很长并且不利于 SEO,但是 ALT 标记使其更多地暴露在搜索引擎中)。

关于php - 图像版本控制 - 最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915466/

相关文章:

perl - 为什么一个空的Perl哈希有一个键?

php - 如何修复 "Warning: illegal string offset"

php - 在 WooCommerce 订阅续订订单中设置送货方式

php - 根据codeigniter中的外键从两个表中获取json编码数据

php - 无法正确使用查询

java - 加密和压缩文件名

php - Satis http basic auth - pass credentials

php - 将变量传递给 PhpUnit

javascript - 根据数据库时间播放视频

PHP - 计算两个字符串之间相似度的最佳哈希算法是什么?