addImage() public method

Add an image to this gallery.
public addImage ( array $image_data, boolean $default = false ) : integer
$image_data array The image to add. Keys include:
  image_filename   - The filename of the image [REQUIRED].
  data             - The binary image data [REQUIRED]
  image_caption    - The caption/description. Defaults to filename.
  image_type       - The MIME type of the image. Attempts to detect.
'image_caption', and 'data'. Optional keys include 'image_filename' and 'image_type'
$default boolean Make this image the new default tile image.
return integer The id of the new image.
Exemplo n.º 1
0
 /**
  * Handle extracting images from uploaded zip files.
  *
  * @param string $filename  The local path to the zip file.
  *
  * @return array  An array of the resulting image_ids.
  * @throws Ansel_Exception
  */
 private function _handleZip($filename)
 {
     $image_ids = array();
     /* See if we can use the zip extension for reading the file. */
     if (Horde_Util::extensionExists('zip')) {
         $zip = new ZipArchive();
         if ($zip->open($filename) !== true) {
             throw new Ansel_Exception(_("Could not open zip archive."));
         }
         /* Iterate the archive */
         for ($z = 0; $z < $zip->numFiles; $z++) {
             $zinfo = $zip->statIndex($z);
             if ($this->_isMetaFile($zinfo['name'])) {
                 continue;
             }
             /* Extract the image */
             $stream = $zip->getStream($zinfo['name']);
             $zdata = stream_get_contents($stream);
             if (!strlen($zdata)) {
                 throw new Ansel_Exception(_("Could not extract image data from zip archive."));
             }
             /* Save the image */
             $image_id = $this->_gallery->addImage(array('image_filename' => $zinfo['name'], 'image_caption' => '', 'data' => $zdata));
             $image_ids[] = $image_id;
             unset($zdata);
         }
         $zip->close();
         unset($zip);
     } else {
         /* Receiving zip data, but extension not loaded */
         $data = file_get_contents($filename);
         /* Get list of images */
         try {
             $zip = Horde_Compress::factory('zip');
             $files = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_LIST));
         } catch (Horde_Compress_Exception $e) {
             throw new Ansel_Exception($e);
             continue;
         }
         /* Iterate the archive */
         foreach ($files as $key => $zinfo) {
             if ($this->_isMetaFile($zinfo['name'])) {
                 continue;
             }
             /* Extract the image */
             try {
                 $zdata = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_DATA, 'info' => $files, 'key' => $key));
             } catch (Horde_Compress_Exception $e) {
                 throw new Ansel_Exception($e);
             }
             /* Add the image */
             $image_id = $this->_gallery->addImage(array('image_filename' => $zinfo['name'], 'image_caption' => '', 'data' => $zdata));
             $image_ids[] = $image_id;
             unset($zdata);
         }
         unset($zip);
         unset($data);
     }
     return $image_ids;
 }
Exemplo n.º 2
0
 /**
  * Copy image and related data to specified gallery.
  *
  * @param array $images           An array of image ids.
  * @param Ansel_Gallery $gallery  The gallery to copy images to.
  *
  * @return integer The number of images copied
  * @throws Ansel_Exception
  */
 public function copyImagesTo(array $images, Ansel_Gallery $gallery)
 {
     if (!$gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
         throw new Horde_Exception_PermissionDenied(_("Access denied copying photos to this gallery."));
     }
     $imgCnt = 0;
     foreach ($images as $imageId) {
         $img = $this->getImage($imageId);
         // Note that we don't pass the tags when adding the image..see below
         $newId = $gallery->addImage(array('image_caption' => $img->caption, 'data' => $img->raw(), 'image_filename' => $img->filename, 'image_type' => $img->getType(), 'image_uploaded_date' => $img->uploaded));
         /* Copy any tags */
         $tags = $img->getTags();
         $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($newId, $tags, $gallery->get('owner'), 'image');
         // Check that new image_id doesn't have existing attributes,
         // throw exception if it does.
         $newAttributes = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImageAttributes($newId);
         if (count($newAttributes)) {
             throw new Ansel_Exception(_("Image already has existing attributes."));
         }
         $exif = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImageAttributes($imageId);
         if (is_array($exif) && count($exif) > 0) {
             foreach ($exif as $name => $value) {
                 $GLOBALS['injector']->getInstance('Ansel_Storage')->saveImageAttribute($newId, $name, $value);
             }
         }
         ++$imgCnt;
     }
     return $imgCnt;
 }