Hi there!
I’d like to share with this pretty simple script with you, hope it will save you few minutes.
You guys who familiar with this Symfony plugin for assets management probably know that it uses sfThumbnail plugin for making thumbnails of image assets.
sfThumbnail has configuration in app.yml and it looks like:
all: sfAssetsLibrary: upload_dir: media # Asset library root, under the web/ dir check_type: false # Set to true if you want to restrict the type of assets types: ['image'] # Accepted asset types if check_type is true thumbnail_dir: thumbnail # Where the image thumbnails are stored use_ImageMagick: false # Set to true if you have the convert command thumbnails: # Name and size (in pixels) of the thumbnails created at upload tiny: # Displayed in the list page width: 70 height: 70 small: # Displayed in the list page width: 100 height: 100 shave: false # Cut strips to constraint the image size middle: width: 200 height: 200 large: # Displayed in the details page width: 450 height: 450 original: # Displayed in the details page width: 800 height: 800 search_pager_size: 20 # Number of resuts per page mass_upload_size: 5 # Number of file upload controls displayed in the mass upload form
This is cool but when you have your project up and running and decided to add new type of thumbnail (for instance name it ‘big’)
big: width: 400 height: 400
“This is simple science” – you need to get all assets objects and resize them in the following way:
1. Create batch script in you symfony batch folder:
$ symfony init-batch default dev mybatchname
Also make sure that it users ‘backend’ application (or application that uses sfAssetAdmin module)
2. Place content from the following code block into your batch script
<?php /* sfAssets resizer batch script
* * !!! BEFORE RUN THIS SCRIPT BACKUP YOUR ASSETS FOLDER !!! * * This batch script resizes all image assets stored by sfAssetsLibraryPlugin plugin * If you changed thumbnails' dimentions or added new you'd love this script. * Just make changes to your app.yml and run this script. * This script doesn't deletes any assets from disk - it's overwrites. * * @package alexfilatov * @subpackage batch * @author Alex Filatov http://www.alexfilatov.com */ define('SF_ROOT_DIR', realpath(dirname(__file__).'/..')); define('SF_APP', 'backend'); define('SF_ENVIRONMENT', 'dev'); define('SF_DEBUG', 1); require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); // initialize database manager $databaseManager = new sfDatabaseManager(); $databaseManager->initialize(); // batch process starts here // If you have a lot of records you should refine this using hydration $c = new Criteria(); $c->add(sfAssetPeer::TYPE, 'image'); $assets = sfAssetPeer::doSelect($c); foreach ($assets as $asset) { echo 'Processing ' . SF_ROOT_DIR.DIRECTORY_SEPARATOR.'/web/'.$asset->getFolderPath() . ':' . $asset->getFilename() . '...'; sfAssetsLibraryTools::createThumbnails($asset->getFolderPath(), $asset->getFilename()); echo "done!\n"; } // batch process ends here ?>
3. Run it
php mybatchname.php
Important notice: if you have a lot of image assets (thousands) you need to refine this script using object hydration.
I’ll do it later, just to let you know.
Peace! 🙂