class ImageController extends FbController { private $loginError = 0; public function __construct($do) { parent::__construct($do); } public function index() { $this->listAll(); } public function add() { $this->assign('title', i18n('add_image')); $this->edit(); } public function edit() { $inputErrors = array(); $inputData = array(); $array = array(); if ($this->do == 'edit') { try { $id = fb_url_part(2, FB_INT, 0); $image = Image::getById($id); $inputData = $image->getData(); foreach ($image->getTags() AS $tag) { $array[] = $tag->tag; } $inputData['tags'] = implode(",", $array); $inputData['categories'] = array(); foreach ($image->getCategories() AS $cat) { $inputData['categories'][$cat->getId()] = 1; } $this->assign('image', $image); } catch (Exception $e) { $this->displayError($e->getMessage()); } } if (is_post_request()) { extract($inputData = fb_post_array(array( 'title' => array(FB_STRING | FB_NO_BREAKS | FB_TRIM, '', null, 30), 'description' => array(FB_STRING | FB_NO_BREAKS | FB_TRIM, ''), 'tags' => array(FB_STRING, ''), 'categories' => array(FB_INT | FB_ARRAY, array()), 'published' => array(FB_BOOL, ''), ))); $currentUser = User::getCurrent(); $inputData['title'] = $title; $inputData['description'] = $description; if ($title == '') { $inputErrors['title'] = i18n('field_empty_error'); } if ($this->do == 'add') { if ($_FILES['file']['size'] == null) { $inputErrors['file'] = i18n('field_empty_error'); } } if (sizeof($inputErrors) == 0) { $make = true; if ($this->do == 'edit') { $newImageId = $id; $dbArray = $inputData; unset($dbArray['tags']);; unset($dbArray['categories']); $image->update($dbArray); } else { $image = new fileUpload(); $ImageInfo = $image->fileUpload('upload', $currentUser->getId()); $dbArray = $inputData; unset($dbArray['tags']);; unset($dbArray['categories']); if ($ImageInfo['success'] !== true) { $this->assign('error', $_FILES['file']['name'] . ' ' . $ImageInfo); $inputErrors['file'] = i18n('please_choose_another_file'); $make = false; } else { $dbArray['filename'] = $ImageInfo['filename']; $dbArray['mimeType'] = $ImageInfo['mimeType']; $dbArray['width'] = $ImageInfo['width']; $dbArray['height'] = $ImageInfo['height']; $dbArray['userId'] = $currentUser->getId(); $newImageId = Image::insert($dbArray); //Image-Handling gem. settings //Displaypicture erstellen self::createThumbnail($newImageId, $currentUser->getId(), 'display', fb_setting('maxDisplayWidth'), fb_setting('maxDisplayHeight'), fb_setting('displayQuality')); //Thumb erstellen self::createThumbnail($newImageId, $currentUser->getId(), 'thumb', fb_setting('thumbnailWidth'), fb_setting('thumbnailHeight'), fb_setting('thumbnailQuality')); } } if ($make) { $image = Image::getById($newImageId); // assign new tags to image $tagArray = explode(",", $tags); $tagArray = array_map('trim', $tagArray); $tagArray = array_diff($tagArray, array('')); // little nice function to remove empty elements from array $image->setTags($tagArray); //*******Category Handling******* $image->setCategories(array_keys($categories)); $this->assign('message', i18n('saved')); $this->assign('ImageInfo' , i18n('image_saved')); $this->redirect('image'); } } } $this->assign('inputData', $inputData); $this->assign('inputErrors', $inputErrors); $this->assign('title', $this->do == 'add' ? i18n('add_image') : i18n('edit_image')); $this->output('image/edit'); } public function delete() { try { $id = fb_url_part(2, FB_INT, 0); $image = Image::getById($id); $this->assign('image', $image); if (is_post_request()) { $confirm = fb_post('confirm', FB_BOOL, 0); if ($confirm) { try { $name = $image->title; $foto = new fileUpload(); $currentUser = User::getCurrent(); //Update CategoryCount on delete image //$toDeleteCategory = ImageCategory::getAll("imageId = $id"); /*if(sizeof($toDeleteCategory) >= 1) { foreach($toDeleteCategory as $category => $value) { ImageCategory::delete('categoryId = ' . $value . ' && imageId = ' . $id, $value); } }*/ //TODO: delete image cache files ImageCache::getByImageId($image->getId(), 'display')->delete(); ImageCache::getByImageId($image->getId(), 'thumb')->delete(); $ImageInfo = $foto->fileDelete($image->filename, $currentUser->getId()); $image->delete(); $this->assign('message', i18n('image_x_has_been_deleted', $name)); $this->redirect('image'); } catch (Exception $e) { $this->displayError($e->getMessage()); } } else { $this->redirect('image'); } } } catch (Exception $e) { $this->displayError($e->getMessage()); } $this->assign('title', i18n('delete_image')); $this->output('image/delete'); } /** * exifData function. * * @access public * */ public function exifData($image = '') { $inputErrors = array(); $inputData = array(); $id = fb_url_part(2, FB_INT, 0); $this->assign('exifDataAvailable', false); if (is_post_request()) { //wenn post-daten vorhanden, dann speichere sie $exif = Exif::getByImageId($id); $image = Image::getById($id); //Postdaten holen $inputData = fb_post_array(array( 'creationDate' => array(FB_STRING | FB_NO_BREAKS | FB_TRIM, '', null, 30), 'cameraModel' => array(FB_STRING | FB_NO_BREAKS | FB_TRIM, ''), 'camManufactor' => array(FB_STRING | FB_NO_BREAKS | FB_TRIM, ''), 'focalLength' => array(FB_STRING, '', null, 5), 'aperture' => array(FB_STRING, '', null, 5), 'exposureTime' => array(FB_STRING, '', null, 5), 'flash' => array(FB_STRING, '', null, 5), 'filmspeed' => array(FB_STRING, '', null, 5), )); $inputData['imageId'] = $id; if(!$exif) { //wenn noch keine Daten in DB vorhanden, INSERT Datensatz Exif::insert($inputData); } else { //sonst UPDATE Datensatz Exif::update($inputData, $id); } if (is_ajax_request()) { $this->outputJSONStatus(i18n('exif_data_saved_for_x', htmlspecialchars($image->title))); } else { $this->assign('message', i18n('exif_data_saved_for_x', htmlspecialchars($image->title))); $this->redirect('image/listAll'); } } else { try { $image = Image::getById($id); $imagePath = UPLOAD_PATH . $image->userId . '/' . $image->filename; $exif = Exif::getByImageId($id); if (Exif::getByImageId($id) !== null) { //Abfrage ob Daten in DB vorhanden -> !!!funktioniert!!! $inputData = $exif->getData(); $this->assign('exifDataAvailable', true); } else { //wenn keine Daten in DB vorhanden, dann lese aus -> !!!funktioniert!!! $inputData = Exif::readOutExif($imagePath); if(sizeof($inputData) > 0){ // wenn keine Daten in Bild vorhanden, gib Info-Message aus $this->assign('exifDataAvailable', true); } } } catch (Exception $e) { // Exif does not exist, so show an error page $this->displayError($e->getMessage()); } } if (false) { // TODO: cleanup here $imagePath = UPLOAD_PATH . $image->userID . '/' . $image->filename; $exifData = new Exif(); $exifData1 = $exifData->readOutExif(SERVER_PATH . $imagePath); $inputData = $exifData1; $this->assign('exifData', $exifData1); $this->assign('imagePath', $imagePath); $this->assign('image', $image); } $this->assign('inputData', $inputData); $this->assign('inputErrors', $inputErrors); $this->assign('image', $image); $this->output('image/exifData', 'raw'); //$this->output('image/exifData'); } /** * @return void */ public function exifAutoComplete() { $field = fb_url_part(2, FB_STRING | FB_NO_BREAKS, ''); $term = fb_get('term', FB_STRING | FB_NO_BREAKS, ''); $result = array(); if (in_array($field, Exif::getFields()) && $field != 'imageId') { if (!empty($term)) { $exifValues = Exif::getByQuery($field . ' like "%' . SQL::instance()->escape($term) . '%"', $field . ' ASC'); } else { $exifValues = Exif::getByQuery('', $field . ' ASC'); } foreach ($exifValues as $record) { $result[] = $record->$field; } } $this->assign('result', $result); $this->output('image/exifAutoComplete', 'json'); } /** * listAll function. * * @access public * */ public function listAll() { // assign page title $this->assign('title', __('images')); // assigning filter data $filter = array( 'query' => '', 'categories' => array(), 'tags' => array() ); // assigning category list $categories = array(); foreach (Category::getByQuery('count > 0') as $cat) { //$filter['categories'][$cat->getId()] = 1; $categories[$cat->getId()] = $cat->name; } $this->assign('categories', $categories); // assigning tag list $tags = array(); foreach (Tag::getByQuery('count > 0') as $tag) { //$filter['tags'][$tag->getId()] = 1; $tags[$tag->getId()] = $tag->tag; } $this->assign('tags', $tags); $conditions = array(); $filter = fb_get_array(array( 'q' => array(FB_STRING | FB_TRIM, ''), 'categories' => array(FB_ARRAY | FB_INT, array()), 'tags' => array(FB_ARRAY | FB_INT, array()), 'published' => array(FB_INT, -1, -1, 1), 'exif' => array(FB_INT, -1, -1, 1) )); if (!empty($filter['q'])) { $conditions[] = 'title LIKE "%' . SQL::escape($filter['q']) . '%"'; } if ($filter['published'] != -1) { $conditions[] = 'published = ' . $filter['published']; } $filterCategoryId = $filter['categories']; $filterTagId = $filter['tags']; // get images from database $images = Image::getByQuery(implode(' AND ', $conditions), 'id DESC', '', $filterCategoryId, $filterTagId); // assign images and filter $this->assign('images', $images); $this->assign('filter', $filter); if (fb_post('getResultCount', FB_BOOL, false) === true) { echo json_encode(array('imageCount' => sizeof($images))); } else { $this->output('image/listAll'); } } /** * listAllImage function. * * @access public * */ public function listAllImage() { try { $id = fb_url_part(2, FB_INT, 0); $image = Image::getById($id); $inputData = $image->getData(); } catch (Exception $e) { // Image does not exist, so show an error page $this->displayError($e->getMessage()); } $thumb = FbThumbFactory::create($image->getServerPath()); $thumb->resize(50, 50); $thumb->show(); } /** * CreateThumbnails function. * @return void * @access public * */ private function createThumbnail($newImageId, $currentUser, $type, $newWidth, $newHeight, $imageQuality, $coords=null) { $options['jpegQuality'] = $imageQuality; $newImage = Image::getById($newImageId); $thumb = FbThumbFactory::create($newImage->getServerPath()); $filename = $currentUser . '_' . $newImageId . '_' . $type . '_' . $newWidth . 'x' . $newHeight . '.jpg'; if($type == "thumb") { $dbArray['type'] = 'thumb'; $thumb->adaptiveResize($newWidth, $newHeight); $thumb->setOptions($options); } else if ($type == "display") { $dbArray['type'] = 'display'; $thumb->resize($newWidth, $newHeight)->setOptions($options); } $thumb->save(CACHE_PATH . $filename); //Thumb in DB table ImageCache schreiben $resizedDimensions = $thumb->getCurrentDimensions(); $dbArray['imageId'] = $newImageId; $dbArray['width'] = $resizedDimensions['width']; $dbArray['height'] = $resizedDimensions['height']; $dbArray['cacheFile'] = $filename; ImageCache::insert($dbArray); } /** * editThumbnail function. * * @access public * */ public function editThumbnail() { try { $id = fb_url_part(2, FB_INT, 0); $image = Image::getById($id); $inputData = $image->getData(); $imageCache = ImageCache::getByImageId($id, 'thumb'); $currentUser = User::getCurrent(); if (is_post_request()) { // generate new thumbnail $coords = fb_post_array(array( 'x1' => array(FB_FLOAT, 0, 0, 1), 'y1' => array(FB_FLOAT, 0, 0, 1), 'x2' => array(FB_FLOAT, 0, 0, 1), 'y2' => array(FB_FLOAT, 0, 0, 1) )); $startX = intval(round($coords['x1'] * $image->width)); $startY = intval(round($coords['y1'] * $image->height)); $newWidth = intval(round($coords['x2'] * $image->width))-$startX; $newHeight = intval(round($coords['y2'] * $image->height))-$startY; $filename = CACHE_PATH . $currentUser->getId() . '_' . $image->getId() . '_thumb_' . fb_setting('thumbnailWidth') . 'x' . fb_setting('thumbnailHeight') . '.jpg'; $thumb = FbThumbFactory::create($image->getServerPath()); $thumb->crop($startX, $startY, $newWidth, $newHeight); $thumb->resize(fb_setting('thumbnailWidth'), fb_setting('thumbnailWidth')); $thumb->save($filename, 'jpg'); //save coords in db $imageCache->update($coords); $this->assign('newThumb', $image->getSize('thumb')); $this->outputJSONStatus(i18n('thumbnail_saved')); } } catch (Exception $e) { // Image does not exist, so show an error page $this->displayError($e->getMessage()); } $thumbnailDimensions = array( // original image dimensions 'width' => $image->width, 'height' => $image->height, // current thumbnail dimensions, please replace with data from database 'x1' => $imageCache->x1, 'y1' => $imageCache->y1, 'x2' => $imageCache->x2, 'y2' => $imageCache->y2, ); /* we could think about delivering crop images as data URIs $thumb = FbThumbFactory::create($image->getServerPath()); $thumb->resize(600, 400); $imageDimensions = $thumb->getCurrentDimensions(); $this->assign('imageData', $thumb->getImageAsString()); $this->assign('imageWidth', $imageDimensions['width']); $this->assign('imageHeight', $imageDimensions['height']); */ $this->assign('thumbnailDimensions', $thumbnailDimensions); $this->assign('image', $image); $this->output('image/editThumbnail', 'raw'); } /** * editThumbnailImage function. * * @access public * */ public function editThumbnailImage() { try { $id = fb_url_part(2, FB_INT, 0); $image = Image::getById($id); $inputData = $image->getData(); } catch (Exception $e) { // Image does not exist, so show an error page $this->displayError($e->getMessage()); } $thumb = FbThumbFactory::create($image->getServerPath()); $thumb->resize(600, 400); $thumb->setOptions(array('jpegQuality' => 50)); $thumb->show(); } public function togglePublished() { try { $id = fb_url_part(2, FB_INT, 0); $image = Image::getById($id); } catch (Exception $e) { // Image does not exist, so show an error page $this->outputJSONStatus($e->getMessage(), parent::JSON_ERROR); } $image->published = $image->published == 1 ? 0 : 1; $this->assign('published', $image->published == 1 ? true : false); $this->outputJSONStatus($image->published == 1 ? i18n('x_has_been_published', htmlspecialchars($image->title)) : i18n('x_has_been_unpublished', htmlspecialchars($image->title))); } }