Reputation: 623
Im using REST API to get products from store, there is around ~3k records. Unfortunately I need create thumbnails of products myself. To do this I use Imagine library for PHP.
After I insert/update products to database in next task I select from database all records and trying to create thumbnail for each of product.
It works... but I'm able to create 12 thumbnails in 120s(thats my execution time script). 12 thumbnails it's far too low for me, I would like to speed up this process, but how can I do this?
All thumbnails are 240x360px, size of each thumbnail is around 12KB.
Here is code which I use for generating thumbnails:
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');
}
}
}
Im running this script on localhost (Windows 7), it's part of application based on Zend Framework 2 + Doctrine 2
Upvotes: 0
Views: 186
Reputation: 12592
You can use a cache or a file cache when the thumbnail is needed again.
Upvotes: 1