當前位置: 首頁>>代碼示例>>PHP>>正文


PHP User::avatar方法代碼示例

本文整理匯總了PHP中RainLab\User\Models\User::avatar方法的典型用法代碼示例。如果您正苦於以下問題:PHP User::avatar方法的具體用法?PHP User::avatar怎麽用?PHP User::avatar使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在RainLab\User\Models\User的用法示例。


在下文中一共展示了User::avatar方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: uploadAvatarFromString

 /**
  * Upload Avatar from Base64 encoded image
  * 
  * @param \RainLab\User\Models\User $user
  * @param string $source
  * string contend of an image on Base64 enconding 
  */
 public static function uploadAvatarFromString($user, $source)
 {
     $dst = '/tmp/avatar_' . $user->getKey() . '_' . uniqid();
     FileHelper::put($dst, base64_decode($source));
     $validImage = true;
     try {
         // Validated is a JPG or PNG
         $imageType = exif_imagetype($dst);
         $validImage = in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF]);
         // Validated is not bigger that xx by xx
         if ($validImage) {
             // Test if image is corrupted if OctoberCMS Resizer can open it
             // is more likely the image is ok
             Resizer::open($dst);
             // Test image dimensions
             list($width, $height, $type, $attr) = getimagesize($dst);
             $validImage = $width <= 400 && $height <= 400;
         }
         // Add right file extension to the upload file
         if ($validImage) {
             // Save image with correct image extension
             $extension = [IMAGETYPE_JPEG => 'jpg', IMAGETYPE_PNG => 'png', IMAGETYPE_GIF => 'gif'][$imageType];
             $newDst = $dst . '.' . $extension;
             rename($dst, $newDst);
             $dst = $newDst;
         }
     } catch (\Exception $e) {
         $validImage = false;
     }
     if (!$validImage) {
         throw new \Exception('Must be a valid JPG, GIF or PNG. And not bigger that 400x400 pixels.');
     }
     $file = new File();
     $file->data = $dst;
     $file->is_public = true;
     $file->save();
     if ($file) {
         $user->avatar()->add($file);
     }
 }
開發者ID:janusnic,項目名稱:OctoberFriends,代碼行數:47,代碼來源:UserExtend.php


注:本文中的RainLab\User\Models\User::avatar方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。