0

我使用 REST API 从商店获取产品,大约有 3k 条记录。不幸的是,我需要自己创建产品的缩略图。为此,我使用了适用于 PHP 的Imagine库。
在下一个任务中将产品插入/更新到数据库后,我从数据库中选择所有记录并尝试为每个产品创建缩略图。
它有效......但我能够在 120 秒内创建 12 个缩略图(这是我的执行时间脚本)。12 个缩略图对我来说太少了,我想加快这个过程,但我该怎么做呢?
所有缩略图均为 240x360px,每个缩略图的大小约为 12KB。

这是我用于生成缩略图的代码:

public function generateThumbnails($products) 
{
    $imagine = new Imagine();
    $resize = 240/360;

    foreach($products as $product)
    {
        if(!file_exists('data/thumbs/'.$product['productId'].'.jpg')){

            $img = $imagine->open($product['productImage']);
            $size = $img->getSize();

            $width = $size->getWidth();
            $height = $size->getHeight();

            $newWidth = floor($height*$resize);

            $cordX = $width/2-$newWidth/2;

            if($cordX < 0)
                $cordX = 0;


            $img->crop(new Point($cordX, 0), new Box($newWidth, $height))->resize(new Box(240, 360))->save('data/thumbs/'.$product['productId'].'.jpg');
        }
    }

}

我在 localhost (Windows 7) 上运行这个脚本,它是基于 Zend Framework 2 + Doctrine 2 的应用程序的一部分

4

1 回答 1

1

当再次需要缩略图时,您可以使用缓存或文件缓存。

于 2013-10-19T16:47:33.870 回答