/** * 单例 * @return Photo */ public static function &instance() { if (!is_object(Photo::$instance)) { // Create a new instance Photo::$instance = new Photo(); } return Photo::$instance; }
/** * Function adds photo to gallery. * Photo has user id, title, list of tags, date of creation and name of chosen picture. */ public function action() { checkUnauthorizedAccess(); $id = \dispatcher\DefaultDispatcher::instance()->getMatched()->getParam("galleryID"); checkIntValueOfId($id); $gallery = GalleryRepository::getByID($id); if ($gallery == null) { redirect(\route\Route::get("errorPage")->generate()); } $main = new Main(); $body = new \templates\AddPhoto(); $main->setBody($body)->setPageTitle("Upload photo"); echo $main; if (post('submit')) { $title = trim(post('title')); $tags = trim(post('tags')); $error = false; if (strlen($title) < 4 || strlen($title) > 25) { $error = true; } if (strlen($tags) < 4 || strlen($tags) > 250) { $error = true; } if (!$error) { $dir = $gallery['title']; $path = 'assets/images/galleries/' . $dir; $localPath = $path . "/" . $_FILES['file']['name']; $completePath = "/TwitterApp/" . $path . "/" . $_FILES['file']['name']; $photo = new Photo(); $photo->setGalleryid($id); $photo->setTitle($title); $photo->setTags($tags); $photo->setCreated(date('Y-m-d H:i:s')); $photo->setImageName($_FILES['file']['name']); $photo->setImagePath($completePath); try { if (!file_exists($path)) { mkdir($path); } move_uploaded_file($_FILES['file']['tmp_name'], $localPath); PhotoRepository::addPhoto($photo); redirect(\route\Route::get("viewGallery")->generate(array("id" => $id))); } catch (\PDOException $e) { $e->getMessage(); } } } }
public function rotate($jobget, $pid = 0, $direction = 1, $is_step = TRUE) { list($pid, $direction) = $this->_jobInit($jobget, $pid, $direction); $photoModel = new Photo(); if (!$photoModel->findOne($pid)) { //获取原图信息 return $this->_jobResult(FALSE); } if (!$is_step) { $photoModel->direction = $direction; } else { $photoModel->direction += $direction; if ($photoModel->direction >= 4) { $photoModel->direction = 0; } if ($photoModel->direction < 0) { $photoModel->direction = 3; } } //判断是否存在 $this->thumb_source = $photoModel->getSource(); if (!$this->thumb_source) { $raw = (array) $photoModel; $raw['direction'] = 0; $originSource = $photoModel->getRawSource($raw); if ($originSource && ($tmp_file = $originSource->downBuffer())) { $angle = $photoModel->direction * 90; $imagine = new Imagine\Imagick\Imagine(); $image = $imagine->open($tmp_file); $thumb_file = $tmp_file . '.jpg'; $image->rotate($angle)->save($thumb_file, array('quality' => Core::config('photo_quality'))); @unlink($tmp_file); $this->uploader = new Uploader(); $this->uploader->process($thumb_file); //原图属性 $updata['fmime'] = $photoModel->mime; $updata['flength'] = $photoModel->size; $updata['fmd5'] = $photoModel->md5; $updata['direction'] = $photoModel->direction; //缩略图属性 $updata['mime'] = $this->uploader->getMIME(); $updata['meta'] = FALSE; //缩略图不需要meta信息 $photoinfo = $this->uploader->getInfo(); $updata['width'] = $photoinfo['width']; $updata['height'] = $photoinfo['height']; if ($this->uploader->getType() !== Uploader::FILETYPE_IMAGE) { return $this->_jobResult(FALSE); } if ($originSource->upBuffer($this->uploader->tmpfile, $updata)) { $this->thumb_source = $originSource; } } } if ($this->thumb_source) { $row['width'] = $this->thumb_source->width; $row['height'] = $this->thumb_source->height; $row['direction'] = $photoModel->direction; $row['mtime'] = time(); $photoModel->update($row); return $this->_jobResult(TRUE); } return $this->_jobResult(FALSE); }
public function geturiByMD5($md5_arr, $self = FALSE) { $r = array(); if (!$md5_arr) { return $r; } if (!is_array($md5_arr)) { $md5_arr = array($md5_arr); } $md5_arr = array_map('strtolower', $md5_arr); //构建sql查询语句 $sql = "SELECT `fid`,`md5`,`path`,`mime`,`thumb_id` FROM `fs_fileentry` WHERE 0 "; foreach ($md5_arr as $md5) { $sql .= "OR (`md5`='{$md5}'" . ($self ? " AND `uid`=" . Core::getUserID() : "") . ") "; } $this->connectDB(); $query = self::$db->query($sql); $result = array(); while ($row = self::$db->fetchArray($query)) { $md5 = $row['md5']; $id = intval($row['fid']); $thumb_id = intval($row['thumb_id']); $result[$md5] = array('id' => $id, 'src' => $this->geturi($id), 'mime' => $row['mime'], 'name' => $row['name']); if ($thumb_id) { $photoModel = new Photo(); if ($photoModel->findOne($thumb_id)) { $result[$md5]['thumb']['id'] = $photoModel->get_pid(); $result[$md5]['thumb']['mime'] = $photoModel->mime; $imgurls = $photoModel->geturi($thumb_id, 130); $result[$md5]['thumb']['src'] = $imgurls[0]; } } } foreach ($md5_arr as $md5) { $r[] = $result[$md5] ? $result[$md5] : array('id' => 0, 'src' => ''); } return $r; }
/** * * 创建新文件 * @param FileDirectory $basedir * @param Uploader $uploader */ public function makeFile(Uploader $uploader) { $filename = $uploader->getTitle(); if ($file_name = $this->getFilename($filename)) { $filepath = $this->path . '/' . $file_name; preg_match("/\\.([a-zA-Z0-9]{2,4})\$/", $file_name, $match); $ext = $match[1] ? strtolower($match[1]) : ''; $fileModel = new FileEntry($this->uid); if ($fileModel->findOne($filepath)) { $filepath = $fileModel->getRecoverPath(); //获取新文件路径名 } //视频截图 $thumbID = intval($uploader->thumb_id); if (!$thumbID) { $thumbInstance = $uploader->getThumbInstance(); if ($thumbInstance) { //更正截图的方向 $fileinfo = $uploader->getInfo(); if ($fileinfo['orientation']) { if ($fileinfo['orientation'] < 0) { $angle = intval(360 + $fileinfo['orientation']); } else { $angle = intval($fileinfo['orientation']); } $imagine = new \Imagine\Imagick\Imagine(); $image = $imagine->open($thumbInstance->tmpfile); $image->rotate($angle)->save($thumbInstance->tmpfile); } $photoModel = new Photo(); if ($photoModel->create($thumbInstance, array('ctrl_type' => 2))) { $thumbID = $photoModel->get_pid(); } } } $meta = array('oid' => intval($uploader->oid), 'uid' => $this->uid, 'source' => $uploader->tmpfile, 'size' => $uploader->getLength(), 'mime' => $uploader->getMIME(), 'md5' => $uploader->getMD5(), 'ext' => $ext, 'cid' => $uploader->cid ? $uploader->cid : 0, 'ctrl_type' => $uploader->ctrl_type ? $uploader->ctrl_type : 0, 'thumb_id' => $thumbID, 'meta' => $uploader->getInfo()); if ($fileModel->create($filepath, $this->get_fid(), $meta)) { $this->updateStat($fileModel->size, 1); return $fileModel; } } return FALSE; }
/** * Adds photo to database. Photo is inside gallery with galleryid. * @param Photo $photo */ public static function addPhoto(Photo $photo) { $db = Database::getInstance(); $query = $db->prepare('INSERT INTO photo (galleryid,title,tags,created,image, path) VALUES (?, ?, ?, ?, ?, ?)'); $query->execute([$photo->getGalleryid(), $photo->getTitle(), $photo->getTags(), $photo->getCreated(), $photo->getImageName(), $photo->getImagePath()]); }