Ansel_Api::saveImage PHP Method

saveImage() public method

Stores an image in a gallery and returns gallery and image data.
public saveImage ( integer $gallery_id, array $image, array $params = [] ) : array
$gallery_id integer The gallery id to add the image to.
$image array Image data array. This can either be the return from Horde_Form_Type_image:: or an array with the following four fields: 'filename', 'description', 'data', 'type' and optionally 'tags'
$params array An array of additional parameters:
  (string)slug         If set, use this as the gallery slug
                       (ignores $gallery_id)
  (string)scope        The scope to use, if not the default.
  (boolean)default     Set this as the key gallery image.
  (array)gallery_data  Any gallery parameters to change at this time.
  (string)encoding     The encoding type for the image data (base64 or binhex)
  (string)compression  The compression type for image data (gzip,lzf)
  (boolean)skiphook    Don't call the postupload hook(s).
return array An array of image/gallery data
    public function saveImage($gallery_id, array $image, array $params = array())
    {
        // Set application scope
        if (!empty($params['scope'])) {
            $GLOBALS['injector']->getInstance('Ansel_Config')->set('scope', $params['scope']);
        }
        // Build image upload structure
        if (isset($image['filename']) && isset($image['description']) && isset($image['data']) && isset($image['type'])) {
            Horde::log(sprintf("Receiving image %s in saveImage() with a raw filesize of %i", $image['filename'], strlen($image['data'])), 'DEBUG');
            $image_data = array('image_filename' => $image['filename'], 'image_caption' => $image['description'], 'image_type' => $image['type'], 'data' => $this->_getImageData($image['data'], empty($params['encoding']) ? 'none' : $params['encoding'], empty($params['compression']) ? 'none' : $params['compression'], true));
        }
        // Validate the image data and other requirements
        if (empty($image_data) && getimagesize($image['file']) === false) {
            throw new InvalidArgumentException(_("The file you uploaded does not appear to be a valid photo."));
        }
        if (empty($params['slug']) && empty($gallery_id)) {
            throw new InvalidArgumentException(_("A gallery to add this photo to is required."));
        }
        if (!empty($params['slug'])) {
            $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGalleryBySlug($params['slug']);
        } elseif ($GLOBALS['injector']->getInstance('Ansel_Storage')->galleryExists($gallery_id)) {
            $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($gallery_id);
        }
        // Check perms for requested gallery
        if (!$gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
            throw new Horde_Exception_PermissionDenied(_("Access denied adding photos to this gallery."));
        }
        // Changing any values while we are at it?
        if (!empty($params['gallery_data'])) {
            foreach ($params['gallery_data'] as $key => $value) {
                $gallery->set($key, $value);
            }
            $gallery->save();
        }
        if (empty($image_data)) {
            $image_data = array('image_filename' => $image['name'], 'image_caption' => $image['name'], 'image_type' => $image['name']['type'], 'data' => file_get_contents($image['file']));
        }
        if (isset($image['tags']) && is_array($image['tags']) && count($image['tags'])) {
            $image_data['tags'] = $image['tags'];
        }
        $image_id = $gallery->addImage($image_data, !empty($params['default']));
        // Call the postupload hook if needed
        if (empty($params['skiphook'])) {
            $this->postBatchUpload($image_id);
        }
        return array('image_id' => (int) $image_id, 'gallery_id' => (int) $gallery->id, 'gallery_slug' => $gallery->get('slug'), 'image_count' => (int) $gallery->countImages());
    }