如何使用 Kohana 的图像模块加速图像大小调整
我有一个简单的脚本,可以动态地从 Cloudfront 存储桶获取图像,调整它们的大小,将它们转换为有效的数据 URL 并将它们显示在页面上。
我遇到的问题是加载时间。下面的脚本需要约 12 秒来加载(每个图像约 1 - 1.5 秒)
是否有任何建议的方法来加快速度?
$mango = Mango::factory('illustration')->load(9)->as_array();
$images = array();
foreach($mango as $data)
{
$image = Image::factory('cloudfrontbucket' . urlencode($data->illustration), 'imagick');
$image = $this->data_uri($image->resize(200), 'image/png');
$images[$data->id]['image'] = $image;
$images[$data->id]['id'] = $data->id;
}
提前致谢。
I have a simple script that dynamically takes images from a Cloudfront bucket, resizes them, converts them to a valid data URL and displays them on a page.
The problem I am having is load time. The below script takes ~12 seconds to load (about ~ 1 - 1.5 seconds per image)
Is there any suggested ways to speed this up?
$mango = Mango::factory('illustration')->load(9)->as_array();
$images = array();
foreach($mango as $data)
{
$image = Image::factory('cloudfrontbucket' . urlencode($data->illustration), 'imagick');
$image = $this->data_uri($image->resize(200), 'image/png');
$images[$data->id]['image'] = $image;
$images[$data->id]['id'] = $data->id;
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您无法加快图像调整大小过程本身。该模块使用 GD 或 ImageMagick,它们是核心 php 扩展,并且运行得尽可能快。唯一的开销是常规的 Kohana 东西。如果你想加快这个过程,你应该寻找一种加速 Kohana 本身的方法,例如 @ThePixelDeveloper 指出的 Gearman。
如果您无法充分减少负载,您可以解决此问题以限制一次处理的图片。下次脚本运行时(通过 cronjob,或者如果您无权访问 crontab,则使用 poormans cron),只需检查哪些图片尚未处理并拍摄一些即可。
You cannot, basically, speed up the image resizing process itself. The module uses GD or ImageMagick, which are core php extensions and run as fast as they can. The only overhead is the regular Kohana stuff. If you want to speed up the process you should look into a way to speed up Kohana itself, for instance Gearman as @ThePixelDeveloper noted.
If you cannot reduce the load enough you could workaround this problem to limit the pictures you process in one time. The next time the script runs (through a cronjob or if you do not have access to the crontab a poormans cron), just check which pictures are not processed yet and take a few.