public static function generateThumbs($photo, $sizes = array())
 {
     $wa = wa();
     $photo_path = self::getPhotoPath($photo);
     $apply_sharp = wa('photos')->getConfig()->getOption('sharpen');
     $main_thumbnail_size = photosPhoto::getBigPhotoSize();
     $main_thumbnail_path = self::getPhotoThumbPath($photo, $main_thumbnail_size);
     foreach ((array) $sizes as $size) {
         if ($size == $main_thumbnail_size) {
             continue;
         }
         $image = self::generateThumb(array('path' => $main_thumbnail_path, 'size' => $main_thumbnail_size), $photo_path, $size, $apply_sharp);
         if (!$image) {
             continue;
         }
         $image->save(self::getPhotoThumbPath($photo, $size));
     }
     // sharp for mail thumbnail
     if ($apply_sharp) {
         $image = waImage::factory($main_thumbnail_path);
         $image->sharpen(self::SHARP_AMOUNT);
         $image->save();
     }
     clearstatcache();
 }
 private function workup($photo)
 {
     $photo['edit_rights'] = $this->photo_rights_model->checkRights($photo, true);
     $photo['private_url'] = photosPhotoModel::getPrivateUrl($photo);
     $photo['thumb_big'] = photosPhoto::getThumbInfo($photo, photosPhoto::getBigPhotoSize());
     $photo['thumb_middle'] = photosPhoto::getThumbInfo($photo, photosPhoto::getMiddlePhotoSize());
     $photo['thumb_crop'] = photosPhoto::getThumbInfo($photo, photosPhoto::getCropPhotoSize());
     $photo['thumb'] = photosPhoto::getThumbInfo($photo, photosPhoto::getThumbPhotoSize());
     return $photo;
 }
 public function execute()
 {
     $url = waRequest::param('url');
     $album = waRequest::param('album');
     $this->hash = waRequest::param('hash');
     if (!$url) {
         throw new waException(_w('Page not found', 404));
     }
     $this->photo_model = new photosPhotoModel();
     $photo = $this->getPhoto($url);
     if (!$photo) {
         throw new waException(_w('Page not found'), 404);
     }
     if (!$this->private_hash && !$this->inCollection($photo, $this->hash)) {
         throw new waException(_w('Page not found'), 404);
     }
     $photo = photosPhoto::escapeFields($photo);
     $size = waRequest::get('size', null, waRequest::TYPE_STRING);
     $is_mini = waRequest::get('mini', 0, waRequest::TYPE_INT);
     if ($is_mini) {
         $size = $size ? $size : photosPhoto::getBigPhotoSize();
         // mini version of loading photo (in albums loading photo in stack)
         $photo['thumb_custom'] = photosPhoto::getThumbInfo($photo, $size);
         $this->response['photo'] = $photo;
         return;
     }
     // delegate work to special render helper
     $render_helper = new photosPhotoRenderHelper($photo, $this->private_hash);
     $result = $render_helper->workUp(array('album' => $album, 'hash' => $this->hash, 'need_photo_stream' => false));
     if ($size) {
         $result['photo']['thumb_custom'] = photosPhoto::getThumbInfo($result['photo'], $size);
     }
     // pull out result of working up
     $this->response['photo'] = $result['photo'];
     $this->response['tags'] = $result['blocks']['tags'];
     $this->response['exif'] = $result['blocks']['exif'];
     $this->response['albums'] = $result['blocks']['albums'];
     $this->response['author'] = $result['blocks']['author'];
     $this->response['stack_nav'] = $result['blocks']['stack_nav'];
     /**
      * Add extra widgets to photo page
      * @event frontend_photo
      * @param string[array]mixed $photo photo data
      * @return array[string][string]string $return[%plugin_id%]['bottom'] In bottom, under photo - any widget
      */
     $this->response['frontend_photo'] = wa()->event('frontend_photo', $photo);
 }
 public function photoUpload(waImage $photo)
 {
     $settings = $this->getSettings();
     $opacity = $settings['opacity'];
     $result = null;
     if ($opacity && !empty($settings['text'])) {
         $font_path = realpath(dirname(__FILE__) . '/config/data/arial.ttf');
         $photo->watermark(array('watermark' => $settings['text'], 'opacity' => $opacity, 'font_file' => $font_path, 'font_size' => $settings['text_size'] * max($photo->width, $photo->height) / photosPhoto::getBigPhotoSize(), 'font_color' => $settings['text_color'], 'text_orientation' => $this->_orientation($settings['text_orientation']), 'align' => $this->_align($settings['text_align'])));
         $result = true;
     }
     if ($opacity && !empty($settings['image'])) {
         $watermark_path = wa()->getDataPath('data/', true) . $settings['image'];
         $watermark = waImage::factory($watermark_path);
         $photo->watermark(array('watermark' => $watermark, 'opacity' => $opacity, 'align' => $this->_align($settings['image_align'])));
         $result = true;
     }
     return $result;
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't rotate photo");
     }
     $direction = waRequest::post('direction', 'left', waRequest::TYPE_STRING_TRIM);
     $photo_model = new photosPhotoModel();
     $photo_model->rotate($id, $direction == 'right');
     $photo = $photo_model->getById($id);
     $photo['thumb'] = photosPhoto::getThumbInfo($photo, photosPhoto::getThumbPhotoSize());
     $photo['thumb_big'] = photosPhoto::getThumbInfo($photo, photosPhoto::getBigPhotoSize());
     $photo['thumb_middle'] = photosPhoto::getThumbInfo($photo, photosPhoto::getMiddlePhotoSize());
     $original_photo_path = photosPhoto::getOriginalPhotoPath($photo);
     if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
         $photo['original_exists'] = true;
     } else {
         $photo['original_exists'] = false;
     }
     $this->log('photo_edit', 1);
     $this->response['photo'] = $photo;
 }
Ejemplo n.º 6
0
waSystem::getInstance(null, $config);
$request_file = wa()->getConfig()->getRequestUrl(true, true);
if (preg_match("@^thumb.php/(.+)@", $request_file, $matches)) {
    $request_file = $matches[1];
}
$public_path = $path . '/wa-data/public/photos/';
$protected_path = $path . '/wa-data/protected/photos/';
$main_thumb_file = false;
$file = false;
$size = false;
// app settings
/**
 * @var photosConfig $app_config
 */
$app_config = wa('photos')->getConfig();
$main_thumbnail_size = photosPhoto::getBigPhotoSize();
$enable_2x = false;
if (preg_match('#((?:\\d{2}/){2}([0-9]+)(?:\\.[0-9a-f]+)?)/\\2\\.(\\d+(?:x\\d+)?)(@2x)?\\.([a-z]{3,4})#i', $request_file, $matches)) {
    $file = $matches[1] . '.' . $matches[5];
    $main_thumb_file = $matches[1] . '/' . $matches[2] . '.' . $main_thumbnail_size . '.' . $matches[5];
    $size = $matches[3];
    if ($file && !$app_config->getOption('thumbs_on_demand')) {
        $thumbnail_sizes = $app_config->getSizes();
        $thumbnail_sizes[] = '192x192';
        // for album covers
        if (in_array($size, $thumbnail_sizes) === false) {
            $file = false;
        }
    }
    if ($matches[4] && $app_config->getOption('enable_2x')) {
        $enable_2x = true;
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't rotate photo");
     }
     $direction = waRequest::post('direction', 'left', waRequest::TYPE_STRING_TRIM);
     if (isset($this->derection_angles[$direction])) {
         $photo_model = new photosPhotoModel();
         $photo_rights_model = new photosPhotoRightsModel();
         $photo = $photo_model->getById($id);
         if (!$photo_rights_model->checkRights($photo, true)) {
             throw new waException(_w("You don't have sufficient access rights"));
         }
         $photo_path = photosPhoto::getPhotoPath($photo);
         $paths = array();
         try {
             $image = new photosImage($photo_path);
             $result_photo_path = preg_replace('/(\\.[^\\.]+)$/', '.result$1', $photo_path);
             $backup_photo_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $photo_path);
             $paths[] = $result_photo_path;
             $result = $image->rotate($this->derection_angles[$direction])->save($result_photo_path);
             if ($result) {
                 $count = 0;
                 while (!file_exists($result_photo_path) && ++$count < 5) {
                     sleep(1);
                 }
                 if (!file_exists($result_photo_path)) {
                     throw new waException("Error while rotate. I/O error");
                 }
                 $paths[] = $backup_photo_path;
                 if (waFiles::move($photo_path, $backup_photo_path)) {
                     if (!waFiles::move($result_photo_path, $photo_path)) {
                         if (!waFiles::move($backup_photo_path, $photo_path)) {
                             throw new waException("Error while rotate. Original file corupted but backuped");
                         }
                         throw new waException("Error while rotate. Operation canceled");
                     } else {
                         $edit_datetime = date('Y-m-d H:i:s');
                         $data = array('edit_datetime' => $edit_datetime, 'width' => $photo['height'], 'height' => $photo['width']);
                         $photo_model->updateById($id, $data);
                         $photo = array_merge($photo, $data);
                         $thumb_dir = photosPhoto::getPhotoThumbDir($photo);
                         $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
                         $paths[] = $back_thumb_dir;
                         waFiles::delete($back_thumb_dir);
                         if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) {
                             throw new waException("Error while rebuild thumbnails");
                         }
                     }
                     $photo['thumb'] = photosPhoto::getThumbInfo($photo, photosPhoto::getThumbPhotoSize());
                     $photo['thumb_big'] = photosPhoto::getThumbInfo($photo, photosPhoto::getBigPhotoSize());
                     $photo['thumb_middle'] = photosPhoto::getThumbInfo($photo, photosPhoto::getMiddlePhotoSize());
                     $original_photo_path = photosPhoto::getOriginalPhotoPath($photo);
                     if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
                         $photo['original_exists'] = true;
                     } else {
                         $photo['original_exists'] = false;
                     }
                     $this->response['photo'] = $photo;
                     $this->log('photo_edit', 1);
                     $obligatory_sizes = $this->getConfig()->getSizes();
                     try {
                         photosPhoto::generateThumbs($photo, $obligatory_sizes);
                     } catch (Exception $e) {
                         waLog::log($e->getMessage());
                     }
                 } else {
                     throw new waException("Error while rotate. Operation canceled");
                 }
             }
             foreach ($paths as $path) {
                 waFiles::delete($path);
             }
         } catch (Exception $e) {
             foreach ($paths as $path) {
                 waFiles::delete($path);
             }
             throw $e;
         }
     }
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't restore photo");
     }
     $photo_model = new photosPhotoModel();
     $photo_rights_model = new photosPhotoRightsModel();
     $photo = $photo_model->getById($id);
     if (!$photo_rights_model->checkRights($photo, true)) {
         throw new waException("You don't have sufficient access rights");
     }
     $original_photo_path = photosPhoto::getOriginalPhotoPath($photo);
     if (!wa('photos')->getConfig()->getOption('save_original') || !file_exists($original_photo_path)) {
         throw new waException("Can't restore photo. Original photo doesn't exist");
     }
     $paths = array();
     try {
         $photo_path = photosPhoto::getPhotoPath($photo);
         $backup_photo_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $photo_path);
         if (waFiles::move($photo_path, $backup_photo_path)) {
             if (!waFiles::move($original_photo_path, $photo_path)) {
                 if (!waFiles::move($backup_photo_path, $photo_path)) {
                     throw new waException("Error while restore. Current file corupted but backuped");
                 }
                 $paths[] = $backup_photo_path;
                 throw new waException("Error while restore. Operation canceled");
             } else {
                 $image = new photosImage($photo_path);
                 $edit_datetime = date('Y-m-d H:i:s');
                 $data = array('edit_datetime' => $edit_datetime, 'width' => $image->width, 'height' => $image->height);
                 $photo_model->updateById($id, $data);
                 $photo = array_merge($photo, $data);
                 $thumb_dir = photosPhoto::getPhotoThumbDir($photo);
                 $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
                 $paths[] = $back_thumb_dir;
                 waFiles::delete($back_thumb_dir);
                 // old backups
                 if (!waFiles::move($thumb_dir, $back_thumb_dir) && !waFiles::delete($thumb_dir)) {
                     throw new waException("Error while rebuild thumbnails");
                 }
                 $photo['original_exists'] = false;
                 $photo['thumb'] = photosPhoto::getThumbInfo($photo, photosPhoto::getThumbPhotoSize());
                 $photo['thumb_big'] = photosPhoto::getThumbInfo($photo, photosPhoto::getBigPhotoSize());
                 $photo['thumb_middle'] = photosPhoto::getThumbInfo($photo, photosPhoto::getMiddlePhotoSize());
                 $sizes = $this->getConfig()->getSizes();
                 try {
                     photosPhoto::generateThumbs($photo, $sizes);
                 } catch (Exception $e) {
                     waLog::log($e->getMessage());
                 }
                 $this->response['photo'] = $photo;
                 $this->log('photo_reverttooriginal', 1);
             }
         } else {
             throw new waException("Error while restore. Operation canceled");
         }
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
     } catch (Exception $e) {
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
         throw $e;
     }
 }
Ejemplo n.º 9
0
 public function getStack($id, $options = array())
 {
     $parent_id = $this->getStackParentId($id);
     if ($parent_id) {
         $stack = $this->getByParent($parent_id);
         if (!empty($options)) {
             $need_tags = isset($options['tags']) && $options['tags'];
             if ($need_tags) {
                 $photo_tags_model = new photosPhotoTagsModel();
             }
             foreach ($stack as &$s) {
                 if ($need_tags) {
                     $s['tags'] = $photo_tags_model->getTags($s['id']);
                 }
                 $s['thumb'] = photosPhoto::getThumbInfo($s, photosPhoto::getThumbPhotoSize());
                 $s['thumb_crop'] = photosPhoto::getThumbInfo($s, photosPhoto::getCropPhotoSize());
                 $s['thumb_big'] = photosPhoto::getThumbInfo($s, photosPhoto::getBigPhotoSize());
                 $s['thumb_middle'] = photosPhoto::getThumbInfo($s, photosPhoto::getMiddlePhotoSize());
             }
             unset($s);
         }
         return $stack;
     }
     return null;
 }
 /**
  * Returns photos in this collection.
  *
  * @param string|array $fields
  * @param int $offset
  * @param int $limit
  * @param bool $escape
  * @return array [photo_id][field] = field value in appropriate field format
  * @throws waException
  */
 public function getPhotos($fields = "*,thumb,tags", $offset = 0, $limit = 50, $escape = true)
 {
     $sql = $this->getSQL();
     $sql = "SELECT " . ($this->joins ? 'DISTINCT ' : '') . $this->getFields($fields) . " " . $sql;
     //$sql .= $this->getGroupBy();
     $sql .= $this->getOrderBy();
     $sql .= " LIMIT " . ($offset ? $offset . ',' : '') . (int) $limit;
     $data = $this->getModel()->query($sql)->fetchAll('id');
     if (!$data) {
         return array();
     }
     if ($this->post_fields) {
         $ids = array_keys($data);
         foreach ($this->post_fields as $table => $fields) {
             if ($table == '_internal') {
                 foreach ($fields as $i => $f) {
                     if ($f == 'thumb' || substr($f, 0, 6) == 'thumb_') {
                         if ($f == 'thumb') {
                             $size = photosPhoto::getThumbPhotoSize();
                         } else {
                             $size = substr($f, 6);
                             switch ($size) {
                                 case 'crop':
                                     $size = photosPhoto::getCropPhotoSize();
                                     break;
                                 case 'middle':
                                     $size = photosPhoto::getMiddlePhotoSize();
                                     break;
                                 case 'big':
                                     $size = photosPhoto::getBigPhotoSize();
                                     break;
                                 case 'mobile':
                                     $size = photosPhoto::getMobilePhotoSize();
                                     break;
                             }
                         }
                         foreach ($data as $id => &$v) {
                             $v[$f] = photosPhoto::getThumbInfo($v, $size);
                         }
                         unset($v);
                     }
                     if ($f == 'frontend_link') {
                         foreach ($data as $id => &$v) {
                             $v['frontend_link'] = photosFrontendPhoto::getLink(array('url' => $this->frontend_base_url ? $this->frontend_base_url . '/' . $v['url'] : $v['url']));
                         }
                         unset($v);
                     }
                     if ($f == 'edit_rights') {
                         $photo_model_rights = new photosPhotoRightsModel();
                         $photo_ids = array();
                         foreach ($data as $id => &$v) {
                             $photo_ids[] = $id;
                             $v['edit_rights'] = false;
                         }
                         unset($v);
                         foreach ($photo_model_rights->filterAllowedPhotoIds($photo_ids, true) as $photo_id) {
                             $data[$photo_id]['edit_rights'] = true;
                         }
                     }
                 }
             } elseif ($table == 'tags') {
                 $model = $this->getModel('photo_tags');
                 $tags = $model->getTags($ids);
                 foreach ($data as $id => &$v) {
                     $v['tags'] = isset($tags[$id]) ? $tags[$id] : array();
                 }
                 unset($v);
             }
         }
     }
     if ($escape) {
         self::escapePhotoFields($data);
     }
     return $data;
 }
 public function execute()
 {
     $id = waRequest::post('id', 0, waRequest::TYPE_INT);
     $in_stack = waRequest::post('in_stack', 0, waRequest::TYPE_INT);
     $hash = waRequest::post('hash', null, waRequest::TYPE_STRING_TRIM);
     $hash = urldecode($hash);
     // get photo
     $this->photo_model = new photosPhotoModel();
     $this->photo = $this->photo_model->getById($id);
     if (!$this->photo) {
         throw new waException(_w("Photo doesn't exists"), 404);
     }
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$photo_rights_model->checkRights($this->photo)) {
         throw new waRightsException(_w("You don't have sufficient access rights"));
     }
     $this->photo['name_not_escaped'] = $this->photo['name'];
     $this->photo = photosPhoto::escapeFields($this->photo);
     $this->photo['upload_datetime_formatted'] = waDateTime::format('humandate', $this->photo['upload_datetime']);
     $this->photo['upload_timestamp'] = strtotime($this->photo['upload_datetime']);
     $this->photo['edit_rights'] = $photo_rights_model->checkRights($this->photo, true);
     $this->photo['private_url'] = photosPhotoModel::getPrivateUrl($this->photo);
     $this->photo['thumb'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getThumbPhotoSize());
     $this->photo['thumb_big'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getBigPhotoSize());
     $this->photo['thumb_middle'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getMiddlePhotoSize());
     $original_photo_path = photosPhoto::getOriginalPhotoPath($this->photo);
     if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
         $this->photo['original_exists'] = true;
     } else {
         $this->photo['original_exists'] = false;
     }
     $photo_tags_model = new photosPhotoTagsModel();
     $tags = $photo_tags_model->getTags($id);
     $this->photo['tags'] = $tags;
     $this->response['photo'] = $this->photo;
     // get stack if it's possible
     if (!$in_stack && ($stack = $this->photo_model->getStack($id, array('thumb' => true, 'thumb_crop' => true, 'thumb_big' => true, 'thumb_middle' => true)))) {
         $this->response['stack'] = $stack;
     }
     // get albums
     $album_photos_model = new photosAlbumPhotosModel();
     $albums = $album_photos_model->getAlbums($id, array('id', 'name'));
     $this->response['albums'] = isset($albums[$id]) ? array_values($albums[$id]) : array();
     // exif info
     $exif_model = new photosPhotoExifModel();
     $exif = $exif_model->getByPhoto($this->photo['id']);
     if (isset($exif['DateTimeOriginal'])) {
         $exif['DateTimeOriginal'] = waDateTime::format('humandatetime', $exif['DateTimeOriginal'], date_default_timezone_get());
     }
     $this->response['exif'] = $exif;
     // get author
     $contact = new waContact($this->photo['contact_id']);
     $this->response['author'] = array('id' => $contact['id'], 'name' => photosPhoto::escape($contact['name']), 'photo_url' => $contact->getPhoto(photosPhoto::AUTHOR_PHOTO_SIZE), 'backend_url' => $this->getConfig()->getBackendUrl(true) . 'contacts/#/contact/' . $contact['id']);
     // for making inline-editable widget
     $this->response['frontend_link_template'] = photosFrontendPhoto::getLink(array('url' => '%url%'));
     $hooks = array();
     $parent_id = $this->photo_model->getStackParentId($this->photo);
     $photo_id = $parent_id ? $parent_id : $id;
     /**
      * Extend photo page
      * Add extra widget(s)
      * @event backend_photo
      * @return array[string][string]string $return[%plugin_id%]['bottom'] In bottom, under photo any widget
      */
     $hooks['backend_photo'] = wa()->event('backend_photo', $photo_id);
     $this->response['hooks'] = $hooks;
     if ($hash !== null) {
         $collection = new photosCollection($hash);
         if (strstr($hash, 'rate>0') !== false) {
             $collection->orderBy('p.rate DESC, p.id');
         }
         $this->response['photo_stream'] = $this->getPhotoStream($collection);
         if ($collection->getAlbum()) {
             $this->response['album'] = $collection->getAlbum();
         }
     }
 }