/** * Get a path or URL to a user pciture * * @param integer $id * @param string $name * @param string $email * @param bool $thumbnail * @return string */ public function picture($id, $name, $email, $thumbnail = true) { $member = Profile::getInstance($id); if (!$member) { return ''; } // If member has a picture set if ($file = $member->get('picture')) { $path = $this->path . DIRECTORY_SEPARATOR . String::pad($id, 5) . DIRECTORY_SEPARATOR; $file = ltrim($file, DIRECTORY_SEPARATOR); // Does the file exist? if ($file != 'profile.png' && file_exists($path . $file)) { try { // Attempt to rename and resize to 'profile.png' $hi = new Processor($path . $file); if (count($hi->getErrors()) == 0) { $hi->autoRotate(); $hi->resize(400); $hi->setImageType(IMAGETYPE_PNG); $hi->save($path . $this->pictureName); } // If we sucessfully made a 'profile.png', // attempt to rename and resize to 'thumb.png' if (file_exists($path . $this->pictureName)) { $hi = new Processor($path . $this->pictureName); if (count($hi->getErrors()) == 0) { $hi->resize(50, false, true, true); $hi->save($path . $this->thumbnailName); } } } catch (\Exception $e) { return ''; } } $file = $this->pictureName; if ($thumbnail) { $file = $this->thumbnailName; } $path .= $file; if (file_exists($path)) { return with(new Moderator($path))->getUrl(); } } return ''; }
/** * Resize an image * * @param string $orig * @param string $dest * @param integer $size * @return boolean True on success, false if errors */ private function resize($orig, $dest, $size) { if (!file_exists($dest)) { list($originalWidth, $originalHeight) = getimagesize($orig); if ($originalWidth > $size || $originalHeight > $size) { $useHeight = $originalHeight > $originalWidth ? true : false; // Resize image $processor = new \Hubzero\Image\Processor($orig); if (!$processor->getErrors()) { $processor->resize($size, $useHeight); if (!$processor->save($dest)) { return false; } } } else { return false; } } return true; }
/** * Upload a file * * @return void */ public function uploadTask() { // Check for request forgeries Request::checkToken(); // Incoming $id = Request::getInt('id', 0); $curfile = Request::getVar('file', ''); $file = Request::getVar('upload', '', 'files', 'array'); // Build upload path $dir = String::pad($id); $path = PATH_APP . DS . trim($this->config->get('webpath', '/site/members'), DS) . DS . $dir; //allowed extensions for uplaod $allowedExtensions = array('png', 'jpe', 'jpeg', 'jpg', 'gif'); //max upload size $sizeLimit = $this->config->get('maxAllowed', '40000000'); // make sure we have id if (!$id) { $this->setError(Lang::txt('COM_MEMBERS_NO_ID')); return $this->displayTask($id); } // make sure we have a file if (!$file['name']) { $this->setError(Lang::txt('COM_MEMBERS_NO_FILE')); return $this->displayTask($id); } // make sure we have an upload path if (!is_dir($path)) { if (!Filesystem::makeDirectory($path)) { $this->setError(Lang::txt('COM_MEMBERS_UNABLE_TO_CREATE_UPLOAD_PATH')); return $this->displayTask($id); } } // make sure file is not empty if ($file['size'] == 0) { $this->setError(Lang::txt('COM_MEMBERS_FILE_HAS_NO_SIZE')); return $this->displayTask($id); } // make sure file is not empty if ($file['size'] > $sizeLimit) { $max = preg_replace('/<abbr \\w+=\\"\\w+\\">(\\w{1,3})<\\/abbr>/', '$1', \Hubzero\Utility\Number::formatBytes($sizeLimit)); $this->setError(Lang::txt('FILE_SIZE_TOO_BIG', $max)); return $this->displayTask($id); } // must be in allowed extensions $pathInfo = pathinfo($file['name']); $ext = $pathInfo['extension']; if (!in_array($ext, $allowedExtensions)) { $these = implode(', ', $allowedExtensions); $this->setError(Lang::txt('COM_MEMBERS_FILE_TYPE_NOT_ALLOWED', $these)); return $this->displayTask($id); } // build needed paths $filePath = $path . DS . $file['name']; $profilePath = $path . DS . 'profile.png'; $thumbPath = $path . DS . 'thumb.png'; // upload image if (!Filesystem::upload($file['tmp_name'], $filePath)) { $this->setError(Lang::txt('COM_MEMBERS_ERROR_UPLOADING')); return $this->displayTask($id); } // create profile pic $imageProcessor = new Processor($filePath); if (count($imageProcessor->getErrors()) == 0) { $imageProcessor->autoRotate(); $imageProcessor->resize(400); $imageProcessor->setImageType(IMAGETYPE_PNG); $imageProcessor->save($profilePath); } // create thumb $imageProcessor = new Processor($filePath); if (count($imageProcessor->getErrors()) == 0) { $imageProcessor->resize(50, false, true, true); $imageProcessor->save($thumbPath); } // remove orig file unlink($filePath); // Push through to the image view $this->displayTask($id); }