/** * Deal with returning the 'image' field to contain a string, and not * an sfValidatedFile object, which Doctrine turns its nose up at. */ public function updateObject($values = null) { // if a new file is uploaded if ($file = $this->getValue('cropped_image')) { $object = parent::updateObject(); list($width, $height, $type, $attr) = getimagesize($file->getTempName()); $cache = sfImagePoolCache::getInstance($object->getImage(), array(), array('width' => $width, 'height' => $height, 'scale' => false)); $file->save($cache->getDestination()); // now set the object's other columns, which weren't part of the upload form $object['width'] = $width; $object['height'] = $height; $object['is_crop'] = true; $object['location'] = $cache::CROP_IDENTIFIER; $this->new_file = $cache->commit(false); } else { $object = parent::updateObject(); } return $object; }
/** * Generate, cache and display the given image */ public function executeImage(sfWebRequest $request) { $sf_pool_image = $this->getRoute()->getObject(); $thumb_method = $request->getParameter('method'); $width = $request->getParameter('width'); $height = $request->getParameter('height'); try { // check file exists on the filesystem if (!file_exists($sf_pool_image->getPathToOriginalFile())) { throw new sfImagePoolException(sprintf('%s does not exist', $sf_pool_image->getPathToOriginalFile())); } // create thumbnail $resizer = new sfImagePoolResizer($sf_pool_image, $thumb_method, $width, $height); $cache = sfImagePoolCache::getInstance($sf_pool_image, array(), $resizer->getParams()); $thumb = $resizer->save($cache->getDestination()); // get thumbnail data and spit out $image_data = $thumb->toString(); $response = $this->getResponse(); // set headers so when image is requested again, if it exists // on the filesystem it'll just be fetched from the browser cache. if ($cache->sendCachingHttpHeaders()) { $response->setContentType($thumb->getMime()); $response->addCacheControlHttpHeader('public'); $response->addCacheControlHttpHeader('max_age', $cache->getLifetime()); $response->setHttpHeader('Last-Modified', date('D, j M Y, H:i:s')); $response->setHttpHeader('Expires', date('D, j M Y, H:i:s', strtotime(sprintf('+ %u second', $cache->getLifetime())))); $response->setHttpHeader('Content-Length', strlen($image_data)); } $response->setHttpHeader('X-Is-Cached', 'no'); sfConfig::set('sf_web_debug', false); $cache->commit(); return $this->renderText($image_data); } catch (sfImagePoolException $e) { if (sfConfig::get('app_sf_image_pool_placeholders', false)) { $dest = sfConfig::get('app_sf_image_pool_use_placeholdit', false) ? sprintf('http://placehold.it/%ux%u&text=%s', $width, $height, urlencode(sfConfig::get('app_sf_image_pool_placeholdit_text', ' '))) : sprintf('@image?width=%s&height=%s&filename=%s&method=%s', $width, $height, sfImagePoolImage::DEFAULT_FILENAME, $thumb_method); $this->logMessage($e->getMessage()); $this->redirect($dest, 302); } else { throw $e; } } }
/** * Call sfImagePoolCache implementation to take care of deleting files * This handles all caching implementations */ public function postDelete($event) { $crop = $event->getInvoker(); $cache = sfImagePoolCache::getInstance($crop->getImage()); $cache->delete($crop); }
require_once realpath(dirname(__FILE__) . '/../../bootstrap/unit.php'); $t = new lime_test(7, new lime_output_color()); $adapter_options = sfConfig::get('app_sf_image_pool_cache'); // Test will fail if no config options // Rackspace do not have testing credentials available for the API so this test requires Rackspace account details if (isset($adapter_options['options']) && !empty($adapter_options['options'])) { // Create an image $image = new sfImagePoolImage(); $image->original_filename = 'test.png'; $image->filename = 'test.png'; $image->mime_type = 'image/png'; $image->save(); $t->isa_ok($image, 'sfImagePoolImage', 'Image created'); $thumbnail_options = array('width' => 300, 'height' => 300, 'scale' => false); // Create cache $cache = sfImagePoolCache::getInstance($image, $adapter_options, $thumbnail_options); $t->isa_ok($cache, 'sfImagePoolRackspaceCloudFilesCache', 'Cache class created'); $container = $cache->getContainer(); $t->is($container->name, $adapter_options['options']['container'], 'Container created or already exists'); // copy test file in place copy($_test_dir . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'test.png', $cache->getDestination()); $url = $cache->commit(false); $imageCrop = sfImagePoolCropTable::getInstance()->findCrop($image, $thumbnail_options['width'], $thumbnail_options['height'], !$thumbnail_options['scale'], $cache::CROP_IDENTIFIER); $t->isa_ok($imageCrop, 'sfImagePoolCrop', 'Crop created'); $objectName = $cache->getCloudName(); $object = $container->get_object($objectName); $t->isa_ok($object, 'CF_Object', 'Image created on Rackspace cloud'); $image->delete(); $image = sfImagePoolImageTable::getInstance()->findOneByFilename('test.png'); $t->is($image, false, 'Image deleted from database'); try {