public function actionUpload() { $params = Yii::app()->params; $file = CUploadedFile::getInstanceByName('avatar'); try { if ($file === null) { throw new Exception(Yii::t('common', 'No file uploaded'), 1); } if ($file->getHasError()) { throw new Exception(Yii::t('common', 'Upload failed, please contact the administrator'), $file->getError()); } $imagesize = getimagesize($file->getTempName()); if ($imagesize === false) { throw new Exception(Yii::t('common', 'Invalid file type of image'), 2); } if ($imagesize[0] > $params['avatar']['width'] || $imagesize[1] > $params['avatar']['height']) { throw new Exception(Yii::t('common', 'Image height or width exceeded, the limited width is {width} and height is {height}', array('{width}' => $params['avatar']['width'], '{height}' => $params['avatar']['height'])), 3); } if (filesize($file->getTempName()) > $params['avatar']['size']) { throw new Exception(Yii::t('common', 'File is too large, the limited size is {size}', array('{size}' => sprintf('%.2fMB', $params['avatar']['size'] / 1048576))), 4); } $basePath = $params->staticPath; $extension = image_type_to_extension($imagesize[2]); $md5 = md5(file_get_contents($file->getTempName())); $filename = $md5 . $extension; $dirname = 'upload/' . $md5[0] . '/'; $fullPath = $params->staticPath . $dirname . $filename; $fullDir = dirname($fullPath); if (!is_dir($fullDir)) { mkdir($fullDir, 0755, true); } if (file_exists($fullPath) || $file->saveAs($fullPath)) { $userAvatar = new UserAvatar(); $userAvatar->user_id = $this->user->id; $userAvatar->md5 = $md5; $userAvatar->extension = $extension; $userAvatar->width = $imagesize[0]; $userAvatar->height = $imagesize[1]; $userAvatar->add_time = time(); $userAvatar->save(false); $this->user->avatar_id = $userAvatar->id; $this->user->save(); $url = $params->staticUrlPrefix . $dirname . $filename; $errorCode = 0; $errorMsg = ''; } else { throw new Exception(Yii::t('common', 'Upload failed, please contact the administrator'), 1); } } catch (Exception $e) { $url = ''; $errorCode = $e->getCode(); $errorMsg = $e->getMessage(); } $this->render('upload', array('url' => $url, 'errorCode' => $errorCode, 'errorMsg' => $errorMsg)); }
public static function importByUrlAndUserId($pUrl, $pUserId, $pOnlineIdentity) { try { $lImported = ImageImporter::importImageFromUrl($pUrl, $pUserId); if ($lImported) { $lAvatar = new UserAvatar(); $lAvatar->setUserId($pUserId); $lAvatar->setAvatar(md5(strtolower($pUrl)) . '-' . $pUserId . '.jpg'); $lAvatar->setOnlineIdentityId($pOnlineIdentity->getId()); // inititalize gravatar info $lAvatar->setImportedIdentifier($pUrl); // generate missing $lAvatar->generateAvatarThumbnails(); $lAvatar->setImportedCommunity($pOnlineIdentity->getCommunityId()); $lAvatar->save(); } } catch (Exception $e) { sfContext::getInstance()->getLogger()->err("{ImageImporter} Exception: " . print_r($e, true)); } }