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());
}