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; }
/** * Fetches an image from its source URL, and puts it into the asset store. * * @param Zend_Db_Table_Row $image * @throws Yadda_Model_Exception * @return void */ public static function fetch($image) { // create temporary directory $tmp = uniqid('/tmp/'); @mkdir($tmp); if (!is_dir($tmp)) { throw new Yadda_Model_Exception('Error creating temp directory'); } // tidy up the URL $uri = str_replace(' ', '%20', $image->source); // download image into temporary directory $config = Zend_Registry::get('config'); $client = new Zend_Http_Client($uri, array('timeout' => 60, 'useragent' => $config->userAgent, 'output_stream' => $tmp . '/original')); $client->setHeaders('Accept-encoding', 'identity'); try { $client->request(); } catch (Zend_Exception $e) { @system('rm -rf ' . $tmp); throw new Yadda_Model_Exception('Error downloading image'); } // convert image try { Yadda_Image::convert($tmp . '/original', $tmp . '/converted.jpg', Yadda_Image::JPEG); } catch (Yadda_Image_Exception $e) { @system('rm -rf ' . $tmp); throw new Yadda_Model_Exception('Error converting image to JPEG'); } // upload converted image to asset store try { Yadda_Store::getInstance()->put($tmp . '/converted.jpg', '/deal/' . $image->deal_id . '/image/' . $image->id . '/original.jpg'); } catch (Yadda_Store_Exception $e) { @system('rm -rf ' . $tmp); throw new Yadda_Model_Exception('Error uploading image to store'); } // done! @system('rm -rf ' . $tmp); }