public function moveTemporaryPhoto($tmpId, $albumId, $desc, $tag = NULL, $angle = 0, $uploadKey = null, $status = null)
 {
     $tmp = $this->photoTemporaryDao->findById($tmpId);
     $album = PHOTO_BOL_PhotoAlbumService::getInstance()->findAlbumById($albumId);
     if (!$tmp || !$album) {
         return FALSE;
     }
     $previewTmp = $this->photoTemporaryDao->getTemporaryPhotoPath($tmp->id, 1);
     $mainTmp = $this->photoTemporaryDao->getTemporaryPhotoPath($tmp->id, 2);
     $originalTmp = $this->photoTemporaryDao->getTemporaryPhotoPath($tmp->id, 3);
     $smallTmp = $this->photoTemporaryDao->getTemporaryPhotoPath($tmp->id, 4);
     $fullscreenTmp = $this->photoTemporaryDao->getTemporaryPhotoPath($tmp->id, 5);
     $privacy = OW::getEventManager()->call('plugin.privacy.get_privacy', array('ownerId' => $album->userId, 'action' => 'photo_view_album'));
     $photoService = PHOTO_BOL_PhotoService::getInstance();
     $photo = new PHOTO_BOL_Photo();
     $photo->description = htmlspecialchars(trim($desc));
     $photo->albumId = $albumId;
     $photo->addDatetime = time();
     $photo->status = empty($status) ? "approved" : $status;
     $photo->hasFullsize = (int) $tmp->hasFullsize;
     $photo->privacy = !empty($privacy) ? $privacy : 'everybody';
     $photo->hash = uniqid();
     $photo->uploadKey = empty($uploadKey) ? $photoService->getPhotoUploadKey($albumId) : $uploadKey;
     PHOTO_BOL_PhotoDao::getInstance()->save($photo);
     try {
         $storage = OW::getStorage();
         $dimension = array();
         if ((int) $angle !== 0) {
             $tmpImage = $tmp->hasFullsize ? (bool) OW::getConfig()->getValue('photo', 'store_fullsize') ? $originalTmp : $fullscreenTmp : $mainTmp;
             $smallImg = new UTIL_Image($tmpImage);
             $smallImg->resizeImage(PHOTO_BOL_PhotoService::DIM_SMALL_WIDTH, PHOTO_BOL_PhotoService::DIM_SMALL_HEIGHT, TRUE)->rotate($angle)->saveImage($smallTmp);
             $storage->copyFile($smallTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_SMALL));
             $dimension[PHOTO_BOL_PhotoService::TYPE_SMALL] = array($smallImg->getWidth(), $smallImg->getHeight());
             $smallImg->destroy();
             $previewImage = new UTIL_Image($tmpImage);
             $previewImage->resizeImage(PHOTO_BOL_PhotoService::DIM_PREVIEW_WIDTH, PHOTO_BOL_PhotoService::DIM_PREVIEW_HEIGHT)->rotate($angle)->saveImage($previewTmp);
             $storage->copyFile($previewTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_PREVIEW));
             $dimension[PHOTO_BOL_PhotoService::TYPE_PREVIEW] = array($previewImage->getWidth(), $previewImage->getHeight());
             $previewImage->destroy();
             $main = new UTIL_Image($tmpImage);
             $main->resizeImage(PHOTO_BOL_PhotoService::DIM_MAIN_WIDTH, PHOTO_BOL_PhotoService::DIM_MAIN_HEIGHT)->rotate($angle)->saveImage($mainTmp);
             $storage->copyFile($mainTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_MAIN));
             $dimension[PHOTO_BOL_PhotoService::TYPE_MAIN] = array($main->getWidth(), $main->getHeight());
             $main->destroy();
             $originalImage = new UTIL_Image($tmpImage);
             $originalImage->resizeImage(PHOTO_BOL_PhotoService::DIM_ORIGINAL_WIDTH, PHOTO_BOL_PhotoService::DIM_ORIGINAL_HEIGHT)->rotate($angle)->saveImage($originalTmp);
             $storage->copyFile($originalTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_ORIGINAL));
             $dimension[PHOTO_BOL_PhotoService::TYPE_ORIGINAL] = array($originalImage->getWidth(), $originalImage->getHeight());
             $originalImage->destroy();
             if ($tmp->hasFullsize && (bool) OW::getConfig()->getValue('photo', 'store_fullsize')) {
                 $fullscreen = new UTIL_Image($tmpImage);
                 $fullscreen->resizeImage(PHOTO_BOL_PhotoService::DIM_FULLSCREEN_WIDTH, PHOTO_BOL_PhotoService::DIM_FULLSCREEN_HEIGHT)->rotate($angle)->saveImage($fullscreenTmp);
                 $storage->copyFile($fullscreenTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_FULLSCREEN));
                 $dimension[PHOTO_BOL_PhotoService::TYPE_FULLSCREEN] = array($fullscreen->getWidth(), $fullscreen->getHeight());
                 $fullscreen->destroy();
             }
         } else {
             $storage->copyFile($smallTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_SMALL));
             list($width, $height) = getimagesize($smallTmp);
             $dimension[PHOTO_BOL_PhotoService::TYPE_SMALL] = array($width, $height);
             $storage->copyFile($previewTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_PREVIEW));
             list($width, $height) = getimagesize($previewTmp);
             $dimension[PHOTO_BOL_PhotoService::TYPE_PREVIEW] = array($width, $height);
             $storage->copyFile($mainTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_MAIN));
             list($width, $height) = getimagesize($mainTmp);
             $dimension[PHOTO_BOL_PhotoService::TYPE_MAIN] = array($width, $height);
             $storage->copyFile($originalTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_ORIGINAL));
             list($width, $height) = getimagesize($originalTmp);
             $dimension[PHOTO_BOL_PhotoService::TYPE_ORIGINAL] = array($width, $height);
             if ($tmp->hasFullsize && (bool) OW::getConfig()->getValue('photo', 'store_fullsize')) {
                 $storage->copyFile($fullscreenTmp, $photoService->getPhotoPath($photo->id, $photo->hash, PHOTO_BOL_PhotoService::TYPE_FULLSCREEN));
                 list($width, $height) = getimagesize($fullscreenTmp);
                 $dimension[PHOTO_BOL_PhotoService::TYPE_FULLSCREEN] = array($width, $height);
             }
         }
         $photo->setDimension(json_encode($dimension));
         PHOTO_BOL_PhotoDao::getInstance()->save($photo);
         if (mb_strlen($desc)) {
             BOL_TagService::getInstance()->updateEntityTags($photo->id, 'photo', $photoService->descToHashtag($desc));
         }
         if (mb_strlen($tag)) {
             BOL_TagService::getInstance()->updateEntityTags($photo->id, 'photo', explode(',', $tag));
         }
         OW::getEventManager()->trigger(new OW_Event('photo.onMoveTemporaryPhoto', array('tmpId' => $tmpId, 'albumId' => $albumId, 'photoId' => $photo->id)));
     } catch (Exception $e) {
         $photo = NULL;
     }
     return $photo;
 }
 public function processUploadedFile($pluginKey, array $fileInfo, $bundle = null, $validFileExtensions = array(), $maxUploadSize = null, $dimensions = null)
 {
     $language = OW::getLanguage();
     $error = false;
     if (!OW::getUser()->isAuthenticated()) {
         throw new InvalidArgumentException($language->text('base', 'user_is_not_authenticated'));
     }
     if (empty($fileInfo) || !is_uploaded_file($fileInfo['tmp_name'])) {
         throw new InvalidArgumentException($language->text('base', 'upload_file_fail'));
     }
     if ($fileInfo['error'] != UPLOAD_ERR_OK) {
         switch ($fileInfo['error']) {
             case UPLOAD_ERR_INI_SIZE:
                 $error = $language->text('base', 'upload_file_max_upload_filesize_error');
                 break;
             case UPLOAD_ERR_PARTIAL:
                 $error = $language->text('base', 'upload_file_file_partially_uploaded_error');
                 break;
             case UPLOAD_ERR_NO_FILE:
                 $error = $language->text('base', 'upload_file_no_file_error');
                 break;
             case UPLOAD_ERR_NO_TMP_DIR:
                 $error = $language->text('base', 'upload_file_no_tmp_dir_error');
                 break;
             case UPLOAD_ERR_CANT_WRITE:
                 $error = $language->text('base', 'upload_file_cant_write_file_error');
                 break;
             case UPLOAD_ERR_EXTENSION:
                 $error = $language->text('base', 'upload_file_invalid_extention_error');
                 break;
             default:
                 $error = $language->text('base', 'upload_file_fail');
         }
         throw new InvalidArgumentException($error);
     }
     if (empty($validFileExtensions)) {
         $validFileExtensions = json_decode(OW::getConfig()->getValue('base', 'attch_ext_list'), true);
     }
     if ($maxUploadSize === null) {
         $maxUploadSize = OW::getConfig()->getValue('base', 'attch_file_max_size_mb');
     }
     if (!empty($validFileExtensions) && !in_array(UTIL_File::getExtension($fileInfo['name']), $validFileExtensions)) {
         throw new InvalidArgumentException($language->text('base', 'upload_file_extension_is_not_allowed'));
     }
     // get all bundle upload size
     $bundleSize = floor($fileInfo['size'] / 1024);
     if ($bundle !== null) {
         $list = $this->attachmentDao->findAttahcmentByBundle($pluginKey, $bundle);
         /* @var $item BOL_Attachment */
         foreach ($list as $item) {
             $bundleSize += $item->getSize();
         }
     }
     if ($maxUploadSize > 0 && $bundleSize > $maxUploadSize * 1024) {
         throw new InvalidArgumentException($language->text('base', 'upload_file_max_upload_filesize_error'));
     }
     $attachDto = new BOL_Attachment();
     $attachDto->setUserId(OW::getUser()->getId());
     $attachDto->setAddStamp(time());
     $attachDto->setStatus(0);
     $attachDto->setSize(floor($fileInfo['size'] / 1024));
     $attachDto->setOrigFileName(htmlspecialchars($fileInfo['name']));
     $attachDto->setFileName(uniqid() . '_' . UTIL_File::sanitizeName($attachDto->getOrigFileName()));
     $attachDto->setPluginKey($pluginKey);
     if ($bundle !== null) {
         $attachDto->setBundle($bundle);
     }
     $this->attachmentDao->save($attachDto);
     $uploadPath = $this->getAttachmentsDir() . $attachDto->getFileName();
     $tempPath = $this->getAttachmentsDir() . 'temp_' . $attachDto->getFileName();
     if (in_array(UTIL_File::getExtension($fileInfo['name']), array('jpg', 'jpeg', 'gif', 'png'))) {
         try {
             $image = new UTIL_Image($fileInfo['tmp_name']);
             if (empty($dimensions)) {
                 $dimensions = array('width' => 1000, 'height' => 1000);
             }
             $image->resizeImage($dimensions['width'], $dimensions['height'])->orientateImage()->saveImage($tempPath);
             $image->destroy();
             @unlink($fileInfo['tmp_name']);
         } catch (Exception $e) {
             throw new InvalidArgumentException($language->text('base', 'upload_file_fail'));
         }
     } else {
         move_uploaded_file($fileInfo['tmp_name'], $tempPath);
     }
     OW::getStorage()->copyFile($tempPath, $uploadPath);
     OW::getStorage()->chmod($uploadPath, 0666);
     unlink($tempPath);
     return array('uid' => $attachDto->getBundle(), 'dto' => $attachDto, 'path' => $uploadPath, 'url' => $this->getAttachmentsUrl() . $attachDto->getFileName());
 }
 public function cropAlbumCover(PHOTO_BOL_PhotoAlbum $album, $coords, $viewSize, $photoId = 0)
 {
     if (!empty($photoId) && ($photo = $this->photoDao->findById($photoId)) !== NULL) {
         $path = $this->photoDao->getPhotoPath($photo->id, $photo->hash, 'main');
     } else {
         $path = PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverPathByAlbumId($album->id);
     }
     $storage = OW::getStorage();
     $tmpPath = OW::getPluginManager()->getPlugin('photo')->getPluginFilesDir() . uniqid(time(), TRUE) . '.jpg';
     $storage->copyFileToLocalFS($path, $tmpPath);
     if (($coverDto = PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->findByAlbumId($album->id)) === NULL) {
         $coverDto = new PHOTO_BOL_PhotoAlbumCover();
         $coverDto->albumId = $album->id;
         PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->save($coverDto);
     }
     $oldCover = PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverPathForCoverEntity($coverDto);
     $oldCoverOrig = PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverOrigPathForCoverEntity($coverDto);
     $coverDto->hash = uniqid();
     $coverDto->auto = 0;
     try {
         $image = new UTIL_Image($tmpPath);
         if ($image->getWidth() >= $coords['w'] && $coords['w'] > 0 && $image->getHeight() >= $coords['h'] && $coords['h'] > 0) {
             $width = $image->getWidth();
             $k = $width / $viewSize;
             $image->cropImage($coords['x'] * $k, $coords['y'] * $k, $coords['w'] * $k, $coords['h'] * $k);
         }
         $saveImage = OW::getPluginManager()->getPlugin('photo')->getPluginFilesDir() . uniqid(time(), TRUE) . '.jpg';
         $image->saveImage($saveImage);
         $image->destroy();
         $storage->copyFile($saveImage, PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverPathForCoverEntity($coverDto));
         $storage->copyFile($tmpPath, PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverOrigPathForCoverEntity($coverDto));
         $storage->removeFile($oldCover);
         $storage->removeFile($oldCoverOrig);
         @unlink($saveImage);
         @unlink($tmpPath);
         PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->save($coverDto);
     } catch (Exception $e) {
         return FALSE;
     }
     return array('cover' => PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverUrlForCoverEntity($coverDto), 'coverOrig' => PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverOrigUrlForCoverEntity($coverDto));
 }
Exemple #4
0
 public function createAlbumCover($albumId, array $photos)
 {
     if (empty($albumId) || count($photos) === 0 || PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->isAlbumCoverExist($albumId)) {
         return FALSE;
     }
     foreach ($photos as $photo) {
         $path = $this->getPhotoPath($photo->id, $photo->hash, 'main');
         $storage = OW::getStorage();
         if (!$storage->fileExists($path)) {
             continue;
         }
         $tmpPathCrop = OW::getPluginManager()->getPlugin('photo')->getPluginFilesDir() . uniqid(uniqid(), TRUE) . '.jpg';
         $tmpPathOrig = OW::getPluginManager()->getPlugin('photo')->getPluginFilesDir() . uniqid(uniqid(), TRUE) . '.jpg';
         if (!$storage->copyFileToLocalFS($path, $tmpPathOrig)) {
             continue;
         }
         $info = getimagesize($tmpPathOrig);
         if ($info['0'] < 330 || $info['1'] < 330) {
             @unlink($tmpPathOrig);
             continue;
         }
         $coverDto = new PHOTO_BOL_PhotoAlbumCover();
         $coverDto->albumId = $albumId;
         $coverDto->hash = uniqid();
         PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->save($coverDto);
         $image = new UTIL_Image($tmpPathOrig);
         $left = $image->getWidth() / 2 - 165;
         $top = $image->getHeight() / 2 - 165;
         $image->cropImage($left, $top, 330, 330);
         $image->saveImage($tmpPathCrop);
         $image->destroy();
         $storage->copyFile($tmpPathCrop, PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverPathForCoverEntity($coverDto));
         $storage->copyFile($tmpPathOrig, PHOTO_BOL_PhotoAlbumCoverDao::getInstance()->getAlbumCoverOrigPathForCoverEntity($coverDto));
         @unlink($tmpPathCrop);
         @unlink($tmpPathOrig);
         return TRUE;
     }
     return FALSE;
 }