/**
  * Fetch images from pool according to any default tag(s) option specified 
  * in the global schema.yml.
  * Override added to specify per tag in MooEditable
  * 
  * @return sfDoctrinePager
  */
 public function getPager($per_page = 12, $page = 1, $tagged_object = null, $tag = null)
 {
     $pager = new sfDoctrinePager($this->getClassnameToReturn(), $per_page);
     $im = new sfImagePoolImage();
     if (!$im->option('tagging')) {
         $tagged_object = $tag = null;
     }
     $im->free(true);
     if (isset($tagged_object) && ($tag = $tagged_object->getTagRestriction())) {
         $query = TagTable::getObjectTaggedWithQuery($this->getClassnameToReturn(), $tag, $pager->getQuery(), array('nb_common_tags' => 1));
         $pager->setQuery($query);
     } else {
         if (!empty($tag)) {
             // If only tags we have no taggable object
             // So get images tagged with this query and do a whereIn on the ids
             $tags = explode(',', $tag);
             $images = TagTable::getObjectTaggedWith($tags, array('model' => 'sfImagePoolImage', 'nb_common_tags' => 1));
             $image_ids = array();
             foreach ($images as $image) {
                 $image_ids[] = $image->id;
             }
             if (!empty($image_ids)) {
                 $pager->getQuery()->whereIn('sfImagePoolImage.id', $image_ids);
             } else {
                 $pager->getQuery()->where('false');
             }
             // we have no images tagged
         }
     }
     $pager->getQuery()->orderBy('updated_at DESC');
     $pager->setPage($page);
     $pager->init();
     return $pager;
 }
 protected function checkTaggingField($fields, $tagging_field)
 {
     $im = new sfImagePoolImage();
     $k = array_search($tagging_field, $fields);
     if ($k !== false && !$im->option('tagging')) {
         unset($fields[$k]);
     }
     return $fields;
 }
 /**
  * 
  */
 public function generateImages($nb)
 {
     for ($i = 0; $i < $nb; $i++) {
         $data = array('original_filename' => mt_rand() . '.jpg', 'filename' => md5(microtime()) . '.jpg', 'is_published' => rand(0, 1), 'mime_type' => 'image/jpg');
         $image = new sfImagePoolImage();
         $image->fromArray($data);
         $image->save();
         unset($image, $data);
     }
 }
 public function setup()
 {
     parent::setup();
     $image = new sfImagePoolImage();
     if ($image->option('tagging')) {
         $query = Doctrine_Core::getTable('Tag')->createQuery('t')->where('id IN(SELECT tag_id FROM tagging WHERE taggable_model = "sfImagePoolImage")');
         $this->widgetSchema['tag'] = new sfWidgetFormDoctrineChoice(array('model' => 'tag', 'multiple' => true, 'expanded' => true, 'query' => $query));
         $this->validatorSchema['tag'] = new sfValidatorDoctrineChoice(array('model' => 'tag', 'multiple' => true, 'required' => false));
     }
     $image->free(true);
 }
 /**
  * Construct a new Resizer instance
  *
  * @param sfImagePoolImage $image 
  * @param string $method "crop" or "scale" 
  * @param integer $width 
  * @param integer $height 
  */
 public function __construct(sfImagePoolImage $image, $method, $width, $height)
 {
     $this->checkMethod($method);
     // expected values for method are 'scale' and 'crop', see plugin routing.yml
     // for default values.
     $this->image = $image;
     $this->method = $method;
     $this->scale = $method == 'scale' ? true : false;
     $default_options = sfConfig::get('app_sf_image_pool_adapter_options', array());
     $options = $this->scale ? array() : array('method' => 'shave_all');
     $this->options = array_merge($default_options, $options);
     $this->width = $width;
     $this->height = $height;
     // This is a tad clunky - we just want to return the params passed to
     // sfThumbnail, but we can't use call_user_func_array to call a
     // constructor. Reflection to the rescue!
     // http://www.php.net/manual/en/function.call-user-func-array.php#74427
     $this->params = array($this->width, $this->height, $this->scale, sfConfig::get('app_sf_image_pool_inflate', true), sfConfig::get('app_sf_image_pool_jpeg_quality', 90), sfConfig::get('app_image_pool_adapter', 'ImagePoolImageMagickAdapter'), $this->options);
     $reflectionObj = new ReflectionClass('sfThumbnail');
     $this->thumb = $reflectionObj->newInstanceArgs($this->params);
     $this->thumb->loadFile($this->image->getPathToOriginalFile());
 }
 /**
  * Take array of data for a single upload and create an image, assigning
  * content of $tags as tag (CSV string or single tag or array).
  * 
  * This common logic is abstracted out so it can easily be used by other plugins
  * that require image upload but need to handle the actual upload logic themselves.
  * 
  * @param array $upload
  * @param mixed $tags
  * @return sfImagePoolImage
  */
 public static function createImageFromUpload($upload, $tags = null)
 {
     // upload was ok, mime type ok and file isn't too big so let's move it into the image pool
     // location and then create a new object for it.
     $file = new sfValidatedFile($upload['name'], $upload['type'], $upload['tmp_name'], $upload['size'], sfImagePoolPluginConfiguration::getBaseDir());
     // this will generate the unique filename
     $new_filename = $file->save();
     $image_data = array('original_filename' => $file->getOriginalName(), 'filename' => $new_filename, 'mime_type' => $file->getType());
     $image = new sfImagePoolImage();
     $image->fromArray($image_data);
     // now set tags if they've been supplied
     if ($tags) {
         $image->addTag($tags);
     }
     $image->save();
     return $image;
 }
$t = new lime_test(6, new lime_output_color());
// Create a new random images to work with
$images = array();
for ($i = 0; $i < 5; $i++) {
    $im = new sfImagePoolImage();
    $im->setFilename(uniqid());
    $im->setOriginalFilename(uniqid());
    $images[] = $im;
}
// Create a new image-poolable object
$obj = new ImageArticle();
$obj->setImages($images);
$t->is(count($obj->getPoolImages()->getInsertDiff()), 5, "5 images flagged for insert");
$t->is(count($obj->getPoolImages()->getDeleteDiff()), 0, "No images flagged to delete");
$obj->save();
$t->is(count($obj->getPoolImages()->getInsertDiff()), 0, "No images flagged for insert");
$t->is(count($obj->getPoolImages()->getDeleteDiff()), 0, "No images flagged to delete");
$im = new sfImagePoolImage();
$im->setFilename(uniqid());
$im->setOriginalFilename(uniqid());
$obj->getPoolImages()->add($im);
$t->is(count($obj->getPoolImages()->getInsertDiff()), 1, "One new image to insert");
$obj->save();
$obj->getPoolImages()->remove(1);
$t->is(count($obj->getPoolImages()->getDeleteDiff()), 1, "One old image to delete");
$obj->save();
// Clean up
$obj->delete();
foreach ($images as $im) {
    $im->delete();
}
<?php

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');