public function commit($redirect = true)
 {
     // save to cloud
     $object_name = $this->getCloudName();
     $this->object = $this->container->create_object($object_name);
     $this->object->load_from_filename($this->getDestination());
     // clean up temp file
     unlink($this->getDestination());
     // check if crop exists
     $imageCrop = sfImagePoolCropTable::getInstance()->findCrop($this->image, $this->resizer_options['width'], $this->resizer_options['height'], !$this->resizer_options['scale'], self::CROP_IDENTIFIER);
     if (!$imageCrop) {
         // create image crop
         $imageCrop = new sfImagePoolCrop();
     }
     // add/ update details
     $imageCrop->Image = $this->image;
     $imageCrop->width = $this->resizer_options['width'];
     $imageCrop->height = $this->resizer_options['height'];
     $imageCrop->location = self::CROP_IDENTIFIER;
     $imageCrop->is_crop = !$this->resizer_options['scale'];
     // controller redirect 301 to cdn
     // If we are on a secure page we want to use the ssl option to avoid security warnings
     $ssl = sfContext::getInstance()->getRequest()->isSecure();
     $off_site_index = $ssl ? 'off_site_ssl_uri' : 'off_site_uri';
     $url = $this->options[$off_site_index] . DIRECTORY_SEPARATOR . $object_name;
     // There's a chance that save() will fail because the crop already exists
     // in the database (race condition). So, if it does fail, let's try and grab it. If it
     // failed and it doesn't exist, then re-throw the exception
     try {
         $imageCrop->save();
     } catch (Doctrine_Connection_Exception $e) {
         if ($e->getPortableCode() != Doctrine_Core::ERR_ALREADY_EXISTS) {
             throw $e;
         }
     }
     if ($redirect) {
         sfContext::getInstance()->getController()->redirect($url, 0, 301);
     } else {
         return $url;
     }
 }
Exemplo n.º 2
0
function pool_image_uri($image, $dimensions = 200, $method = 'crop', $absolute = false)
{
    // remove Symfony escaping if applied
    if ($image instanceof sfOutputEscaper) {
        $image = $image->getRawValue();
    }
    $offsite = false;
    if (is_array($dimensions)) {
        $width = $dimensions[0];
        $height = $dimensions[1];
    } else {
        if (strpos(strtolower($dimensions), 'x') !== false) {
            list($width, $height) = explode('x', $dimensions);
        } else {
            $height = $width = $dimensions;
        }
    }
    $cache_options = sfConfig::get('app_sf_image_pool_cache');
    $class = $cache_options['class'];
    // If we are on a secure page we want to use the ssl option to avoid security warnings
    $ssl = sfContext::getInstance()->getRequest()->isSecure();
    $off_site_index = $ssl ? 'off_site_ssl_uri' : 'off_site_uri';
    // If remote and remote uri set, plus image exists
    if ($class::IS_REMOTE && !empty($cache_options[$off_site_index]) && $image) {
        // check whether crop exists - if it doesn't business as usual
        $is_crop = 'crop' == $method;
        $crop = sfImagePoolCropTable::getInstance()->findCrop($image, $width, $height, $is_crop, $class::CROP_IDENTIFIER);
        if ($crop) {
            $absolute = false;
            $offsite = true;
        }
    }
    if (!function_exists('url_for')) {
        sfApplicationConfiguration::getActive()->loadHelpers(array('Url'));
    }
    // If we have an empty sfImagePool instance (ie. no image) then output a placeholder if set in config to do so
    if (!$image['filename']) {
        if (sfConfig::get('app_sf_image_pool_placeholders', false)) {
            if (sfConfig::get('app_sf_image_pool_use_placeholdit', false)) {
                $url = sprintf('http://placehold.it/%ux%u&text=%s', $width, $height, urlencode(sfConfig::get('app_sf_image_pool_placeholdit_text', ' ')));
            } else {
                // If offsite then should have cached placeholder too - check whether created as crop too
                if ($class::IS_REMOTE && !empty($cache_options[$off_site_index])) {
                    $is_crop = 'crop' == $method;
                    $crop = sfImagePoolCropTable::getInstance()->findCrop(sfImagePoolImage::DEFAULT_FILENAME, $width, $height, $is_crop, $class::CROP_IDENTIFIER);
                    if ($crop) {
                        $absolute = false;
                        $offsite = true;
                    }
                }
                $url = url_for(sprintf('@image?width=%s&height=%s&filename=%s&method=%s', $width, $height, sfImagePoolImage::DEFAULT_FILENAME, $method), $absolute);
            }
        } else {
            return false;
        }
        // No image, no placeholder
    } else {
        $url = url_for(sprintf('@image?width=%s&height=%s&filename=%s&method=%s', $width, $height, $image['filename'], $method), $absolute);
    }
    // Do we want to remove the controller filename? It's good to have this option independent of the global Symfony
    // setting as we may be generating URLs to insert into the db and which should not have any controller referenced.
    if (!sfConfig::get('app_sf_image_pool_use_script_name', false) || $offsite) {
        $url = preg_replace('%\\w+(_dev)?\\.php/%', '', $url);
    }
    // If offsite - then replace local image-pool folder with offsite URL (naming convention for offsite should mirror
    // folder structure for local)
    if ($offsite) {
        $url = str_replace(sfImagePoolPluginConfiguration::getBaseUrl(), $cache_options[$off_site_index], $url);
    }
    return $url;
}
    $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 {
        $object2 = $container->get_object($objectName);
        $t->fail('Object not deleted from cloud');
    } catch (NoSuchObjectException $e) {
        $t->pass('Object deleted from cloud');
    }
} else {
    $t->fail('Please ensure you have set up your Rackspace cache options - use the rackspace:initialise task to accomplish this or see the README');