/** * Parse the avatar file in $tempPath, * by creating small square image from it, * save into file system and then add path to new avatar * in User object as 'avatar' element * * * @param User $User * @param unknown_type $tempPath * @throws \Lampcms\Exception * * @return bool false if unable to parse image | true if added image */ public static function addIcon(Clientdata $o, $tempPath) { d('$tempPath: ' . $tempPath); $size = 72; $avatarDir = LAMPCMS_DATA_DIR . 'img' . DIRECTORY_SEPARATOR . 'avatar' . DIRECTORY_SEPARATOR . 'sqr' . DIRECTORY_SEPARATOR; d('$avatarDir: ' . $avatarDir); $savePath = Path::prepare($o['_id'], $avatarDir); d('$savePath: ' . $savePath); /** * Create avatar and save it * with compression level of 80% (small compression) */ try { $ImgParser = \Lampcms\Image\Editor::factory($o->getRegistry())->loadImage($tempPath)->makeSquare($size); $savePath .= $ImgParser->getExtension(); $ImgParser->save($avatarDir . $savePath, null, 80); d('avatar saved to ' . $savePath); } catch (\Lampcms\ImageException $e) { e('ImageException caught in: ' . $e->getFile() . ' on line: ' . $e->getLine() . ' error: ' . $e->getMessage()); return false; } /** * Now remove tempPath file */ @\unlink($tempPath); /** * Now add the path to avatar * to Clientdata object * save() is not invoked on Clientdata object here! * Either rely on auto-save or call save() * from a function that invoked this method */ $o['icon'] = $savePath; return true; }
/** * Parse the avatar file in $tempPath, * by creating small square image from it, * save into file system and then add path to new avatar * in User object as 'avatar' element * * * @param User $User * @param unknown_type $tempPath * @throws \Lampcms\Exception * * @return object $this */ public function addAvatar(User $User, $tempPath) { d('$tempPath: ' . $tempPath); if (empty($tempPath)) { d('no avatar to add'); return $this; } $size = $this->Registry->Ini->AVATAR_SQUARE_SIZE; $avatarDir = LAMPCMS_DATA_DIR . 'img' . DS . 'avatar' . DS . 'sqr' . DS; d('$avatarDir: ' . $avatarDir); $savePath = Path::prepare($User->getUid(), $avatarDir); d('$savePath: ' . $savePath); /** * Create avatar and save it * with compression level of 80% (small compression) */ //try{ $ImgParser = \Lampcms\Image\Editor::factory($this->Registry)->loadImage($tempPath)->makeSquare($size); $savePath .= $ImgParser->getExtension(); $ImgParser->save($avatarDir . $savePath, null, 80); d('avatar saved to ' . $savePath); //} catch(\Lampcms\ImageException $e){ //e('ImageException caught in: '.$e->getFile().' on line: '.$e->getLine().' error: '.$e->getMessage()); //throw new \Lampcms\Exception('Unable to process your avatar image at this time'); //} /** * Now remove tempPath file */ @\unlink($tempPath); /** * Now add the path to avatar * to user object * save() is not invoked on User object here! * Either rely on auto-save (may not work in case User is * actually the Viewer object) or call save() * from a function that invoked this method */ $User['avatar'] = $savePath; return $this; }
/** * Resize uploaded image if necessary * if image dimensions are smaller than defined * in settings then just save the original * and return path to saved image * * @return mixed string path to saved image * * @throws \Lampcms\ImageException * @throws \Lampcms\DevException */ protected function resize() { $Editor = \Lampcms\Image\Editor::factory($this->Registry); $Editor->loadImage($this->file); /** * If we get here it means file was an image * update USER collection to insert i_last_upload_ts timestamp * will be used to check upload flood * do this now before the actual resize/save * in case resize does not work the upload will still * be counted */ $this->User[Schema::LAST_UPLOAD_TIME] = time(); $this->User->save(); $basePath = LAMPCMS_DATA_DIR . 'img'; $fileName = time(); $fileExt = $Editor->getExtension(); $destFolder = Path::prepareByTimestamp($basePath, 'u' . $this->User->getUid(), false); $origPath = $destFolder . $fileName . $fileExt; if (!copy($this->file, LAMPCMS_DATA_DIR . 'img' . DIRECTORY_SEPARATOR . $origPath)) { throw new \Lampcms\DevException('Unable to copy orig file from ' . $this->file . ' to ' . LAMPCMS_DATA_DIR . 'img' . DIRECTORY_SEPARATOR . $origPath); } $max = $this->EditorOptions['IMAGE_UPLOAD_W_X']; list($w, $h) = explode(',', $max); $scaleSize = $Editor->getFactor(trim($w), trim($h)); if ($scaleSize) { $resizedPath = $destFolder . $fileName . '_ws' . $fileExt; try { $Editor->scale($w, $h)->save(LAMPCMS_DATA_DIR . 'img' . DIRECTORY_SEPARATOR . $resizedPath); return \str_replace(DIRECTORY_SEPARATOR, '/', $resizedPath); } catch (\Exception $e) { e('Unable to resize and save uploaded image. ' . $e->getMessage() . ' ' . $e->getFile() . ' on ' . $e->getLine()); throw new ImageException('@@Unable to resize image@@'); } } return \str_replace(DIRECTORY_SEPARATOR, '/', $origPath); }