Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // -------------------------------------------------------------------------------
- // Bootstrap a Zend Framework application
- // -------------------------------------------------------------------------------
- /**
- * Define application constants
- */
- define('DS', DIRECTORY_SEPARATOR);
- define('PS', PATH_SEPARATOR);
- defined('BASE_PATH')
- or define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
- defined('APP_PATH')
- or define('APP_PATH', realpath(dirname(__FILE__) . '/../application'));
- defined('APP_ENV')
- or define('APP_ENV', 'development');
- $paths[] = BASE_PATH . DS . 'library';
- $paths[] = APP_PATH . DS . 'models';
- set_include_path(implode(PS, $paths) . PS . get_include_path());
- // register Pheanstalk class loader
- require_once('Pheanstalk/pheanstalk_init.php');
- require_once('Thumbnail.php');
- require_once 'Ash/Application.php';
- $application = new Ash_Application(APP_ENV, APP_PATH . DS . 'configs' . DS . 'application.ini');
- $application->bootstrap('config')
- ->bootstrap('log');
- /**
- * Setup auth - perform the tasks as if we were an Member
- */
- $auth = Zend_Auth::getInstance();
- $auth->getStorage()->write(array('role' => 'Member'));
- // -------------------------------------------------------------------------------
- // Now get to work!
- // -------------------------------------------------------------------------------
- $host = isset(Ash_Ash::getConfig()->beanstalk->host)
- ? Ash_Ash::getConfig()->beanstalk->host
- : '127.0.0.1';
- $tube = isset(Ash_Ash::getConfig()->beanstalk->thumbnailTask->tube)
- ? Ash_Ash::getConfig()->beanstalk->thumbnailTask->tube
- : 'thumbnails';
- // create a new Pheanstalk client that should be listening to the same Beanstalkd host
- $beanstalk = new Pheanstalk($host);
- /**
- * only watch the tube dedicated towards thumb nailing and ignore the 'default' tube
- */
- if ($tube == 'default') {
- $beanstalk->watch($tube);
- } else {
- $beanstalk->watch($tube)->ignore('default');
- }
- /**
- * Define the size of the thumbnails to be produced (assuming square images)
- */
- $thumbnailSizes = array(40, 60, 30, 25, 180);
- /**
- * start processing jobs!
- */
- while ($job = $beanstalk->reserve()) {
- Ash_Ash::log("New WorkerThumbnail Job started for {$job->getData()}");
- $filename = $job->getData();
- if (!empty($filename)) {
- // if the file exists then do the thumbnailing
- if (file_exists(Ash_Ash::getConfig()->appSettings->uploadPath . $filename)) {
- // Start Vars
- $cacheDir = Ash_Ash::getConfig()->appSettings->cachePath . $filename . '/';
- $cacheUrl = '/media/cache/' . $filename . '/';
- switch (true) {
- // if the cached dir exists but is not writable
- case (file_exists($cacheDir) && !is_writable($cacheDir)):
- chmod($cacheDir, 0777);
- break;
- // Check for image's cache folder, make one if none exists
- case (!file_exists($cacheDir)):
- mkdir($cacheDir, 0777, true);
- break;
- }
- // create thumbnails for each of the sizes
- foreach ($thumbnailSizes as $size) {
- $thumbFilename = $size . 'x' . $size . '_' . $filename;
- // Check if this size exists, if not, resize and crop it
- if (!file_exists($cacheDir . $thumbFilename)) {
- $thumb = new Thumbnail(Ash_Ash::getConfig()->appSettings->uploadPath . $filename);
- $thumb->resize(null, null, $size);
- $thumb->cropFromCenter($size);
- $thumb->save($cacheDir . $thumbFilename, 100);
- }
- }
- /**
- * Done with the job so delete it!
- */
- Ash_Ash::log("Deleting WorkerThumbnail Job for {$job->getData()}");
- $beanstalk->delete($job);
- } else {
- // bury the job and make a log entry
- $beanstalk->bury($job);
- Ash_Ash::log("WorkerThumbnail was forced to BURY the job for {$job->getData()} "
- . "because the file didn't exist. Job details: \n" . print_r($job,true));
- }
- } else {
- // bury the job and make a log entry
- $beanstalk->bury($job);
- Ash_Ash::log("WorkerThumbnail was forced to BURY the job for {$job->getData()} "
- . "because the filepath was EMPTY! Job details: \n" . print_r($job,true));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement