public function thumbAction() { $id = (int) $this->_getParam('id'); $images = Yadda_Model_Image::all(array('deal' => $id)); $imageId = $images[0]['id']; // create a temporary directory $tmp = uniqid('/tmp/'); @mkdir($tmp); if (!is_dir($tmp)) { throw new Data_Exception_Error('Error creating tmp dir'); } // fetch the original try { Yadda_Store::getInstance()->get('/deal/' . $id . '/image/' . $imageId . '/original.jpg', $tmp . '/original.jpg'); } catch (Yadda_Store_Exception $e) { // use default @copy(APPLICATION_BASE . '/pub-data/img/' . $this->_getParam('filename'), $tmp . '/original.jpg'); } // resize try { Yadda_Image::resize($tmp . '/original.jpg', $tmp . '/' . $this->_getParam('filename'), array('width' => $this->_getParam('width'), 'height' => $this->_getParam('height'), 'crop' => $this->_getParam('crop')), Yadda_Image::JPEG); } catch (Yadda_Image_Exception $e) { @system('rm -rf ' . $tmp); throw new Data_Exception_Error('Error resizing image'); } // prepare to serve the jpg file $this->getResponse()->setHeader('Content-Type', 'image/jpeg'); $this->getResponse()->setBody(@file_get_contents($tmp . '/' . $this->_getParam('filename'))); $this->getResponse()->setHeader('Cache-Control', 'public, max-age=' . 7 * 24 * 60 * 60); // cleanup @system('rm -rf ' . $tmp); $this->getResponse()->sendResponse(); die; }
<?php include_once '../bootstrap.php'; set_time_limit(0); $logger = Yadda_Log::getInstance(); // get all images that need to be fetched $imageDb = Yadda_Db_Table::getInstance('image'); $select = $imageDb->select()->from('image')->where('status = ?', 'new')->where('source LIKE ?', 'http%')->where('created >= ?', date('Y-m-d H:i:s', strtotime('-1 day'))); $images = $imageDb->fetchAll($select); foreach ($images as $image) { // mark the image as 'fetching' -- if the update doesn't return > 0 then // either the row has been deleted or it is already being fetched by // another process $rows = $imageDb->update(array('status' => 'fetching', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id)); // we need to process this image if ($rows > 0) { $logger->log('Fetching image #' . $image->id, Zend_Log::INFO); $logger->log('Source: ' . $image->source, Zend_log::DEBUG); try { Yadda_Model_Image::fetch($image); // mark the image as 'active' $image->getTable()->update(array('status' => 'active', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id)); } catch (Zend_Exception $e) { $logger->log('Exception: ' . (string) $e, Zend_Log::ERR); // reset the image to 'new' so that it can be fetched next time $image->getTable()->update(array('status' => 'new', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id)); } sleep(rand(0, 10)); } }