public function uploadImage($model, $attribute) { if ($this->isFileUploadedWithoutError($model, $attribute)) { $this->imageUploader = EUploadedImage::getInstance($model, $attribute); $this->imageUploader->maxWidth = $this->maxWidth; $this->imageUploader->maxHeight = $this->maxHeight; $filePath = $this->getFilePath(); if ($this->createFolder()) { if ($this->thumb !== null) { $this->thumb['dir'] = '../' . $this->thumbDir . '/' . $this->subfolder; } $this->imageUploader->thumb = $this->thumb; if ($this->imageUploader->saveAs($filePath)) { return true; } else { $this->setUploadedImagePath(''); } } } else { return false; } }
public function actionAddCustomPhoto($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); $aAllBannerSize = Banners::getHomeBannerSize(); $aBannerSize = $aAllBannerSize[$model->place_holder_id]; if (isset($_POST['Banners'])) { $model->attributes = $_POST['Banners']; $model->image = CUploadedFile::getInstance($model, 'image'); $model->scenario = 'addCustomPhoto'; if ($model->validate()) { $ext = $model->image->getExtensionName(); $file_name = 'banner_' . $model->place_holder_id; $model->large_image = $file_name . '.' . $ext; $model->thumb_image = 'thumb_' . $file_name . '.' . $ext; if ($model->save()) { $model->image = EUploadedImage::getInstance($model, 'image'); //$model->image->maxWidth = $aBannerSize['width']; //$model->image->maxHeight = $aBannerSize['height']; /*$model->image->thumb = array( 'maxWidth' => $aBannerSize['width'], 'maxHeight' => $aBannerSize['height'], 'dir' => 'thumbs', 'prefix' => 'thumb_', );*/ $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/upload/admin/homeBanner/' . $file_name . '.' . $ext); $thumb = new EPhpThumb(); $thumb->init(); $thumb->create(Yii::getPathOfAlias('webroot') . '/upload/admin/homeBanner/' . $file_name . '.' . $ext)->resize($aBannerSize['width'], $aBannerSize['height'])->save(Yii::getPathOfAlias('webroot') . '/upload/admin/homeBanner/thumbs/thumb_' . $file_name . '.' . $ext); $this->redirect(array('/admin/manageBanner')); } } } $this->render('addCustomPhoto', array('model' => $model, 'bannerSize' => $aBannerSize)); }
/** * Initially processes $_FILES superglobal for easier use. * Only for internal usage. */ protected static function prefetchFiles() { self::$_files = array(); if (!isset($_FILES) || !is_array($_FILES)) { return; } foreach ($_FILES as $class => $info) { self::collectFilesRecursive($class, $info['name'], $info['tmp_name'], $info['type'], $info['size'], $info['error']); } }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate() { $this->model = $this->loadModel(Yii::app()->user->id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($this->model); if (isset($_POST['User'])) { $this->model->attributes = $_POST['User']; if ($this->model->Year) { if (!$this->model->Month) { $this->model->Month = 01; } if (!$this->model->Day) { $this->model->Day = 01; } $this->model->birthdate = strtotime($this->model->Year . "/" . $this->model->Month . "/" . $this->model->Day); } $this->model->addError('firstname', Yii::t('user', 'Please select an image smaller than 7MB.')); if ($this->model->validate()) { //Delete image reference if marked if ($this->model->deleteimage) { $this->model->image_name = ''; } //Save the image if any $image = CUploadedFile::getInstance($this->model, 'image'); if (get_class($image) == 'CUploadedFile') { if ($image->getSize() > 1024 * 1024 * Yii::app()->params['max_image_size']) { $this->model->addError('image', Yii::t('user', 'Please select an image smaller than 7MB.')); $error = true; } //MB $types = array("image/jpg", "image/png", "image/gif", "image/jpeg"); if (!in_array($image->type, $types)) { $this->model->addError('image', Yii::t('user', 'File type {filetype} not supported. Please select a valid image type.', array('{filetype}' => CFileHelper::getMimeType($image->getTempName())))); $error = true; } } if (!$error) { Yii::import('ext.EUploadedImage'); if ($image) { $img_extension = $image->getExtensionName() ? $image->getExtensionName() : ''; $img_name = $this->model->id . '.' . $img_extension; $this->model->image_name = $img_name; $this->model->image = EUploadedImage::getInstance($this->model, 'image'); $this->model->image->maxWidth = 980; $this->model->image->maxHeight = 750; $this->model->image->thumb = array('maxWidth' => 45, 'maxHeight' => 45, 'keepratio' => false, 'prefix' => 'small_'); if (!$this->model->image->saveAs(Yii::app()->params['webdir'] . DIRECTORY_SEPARATOR . Yii::app()->params['user_img_path'] . DIRECTORY_SEPARATOR . $img_name)) { $this->model->addError('image', Yii::t('user', 'We could not save the image in the disk.')); return false; } } } } $this->model->validate(); if ($this->model->save()) { Yii::app()->user->setFlash('profile_success', Yii::t('user', 'Profile Settings updated successfully.')); } //$this->redirect(array('index')); } $this->render('update', array('model' => $this->model)); }
function get_image_resized($filename, $path, $width, $height, $keepratio = true, $keepmaxresolution = true) { //First check that the file exists( created by a previous invocation), if so return it if (!($image_size = getimagesize($path . DIRECTORY_SEPARATOR . $filename))) { return false; } if (!$width) { $width = $height; } //if width isn't set, then set it as height if (!$height) { $height = $width; } //if height isn't set, then set it as width $new_width = $width; $new_height = $height; //Try to find an original file with larger size if possible and desiareable for the ocation if (($width > 980 or $height > 750) and !strstr($filename, 'org')) { //Don't use strpos() as first position is 0, so will evaluate as false if ($image_size2 = getimagesize($path . DIRECTORY_SEPARATOR . 'org_' . $filename)) { //if the file exists $image_size = $image_size2; $filename = 'org_' . $filename; } } //TODO:When keep max resolution is false we need to do some work on the naming, //as EUploadedImage there is not available solution for grater than original size $keepmaxresolution = true; if ($keepmaxresolution and $keepratio) { if ($width > $image_size[0]) { $new_width = $image_size[0]; } if ($height > $image_size[1]) { $new_height = $image_size[1]; } } if ($keepratio) { if ($image_size[0] > $width) { $new_width = $width; $new_height = floor($width * $image_size[1] / $image_size[0]); $image_size[1] = $width * $image_size[1] / $image_size[0]; //notice whiout floor. Also this. EUploadedImage:resizeImage, does this too, so we need to do it also. } if ($image_size[1] > $height) { $new_width = floor($height * $new_width / $image_size[1]); $new_height = $height; } } $split_name = explode(".", $filename); $new_name = str_replace('org_', '', $split_name[0]) . "_" . $new_width . "x" . $new_height . "." . $split_name[1]; //in case org,Set the name back, we dont want 'org_' prefix in the new image names if (file_exists($path . DIRECTORY_SEPARATOR . $new_name)) { return $new_name; } Yii::import('ext.EUploadedImage'); $newimage = new EUploadedImage("some.img", addslashes($path . DIRECTORY_SEPARATOR . $filename), $image_size['mime'], 123, 0); //_size doesnt matters just a number, _error = 0 for UPLOAD_ERR_OK pass ok $newimage->keepratio = $keepratio; $newimage->maxWidth = $width; $newimage->maxHeight = $height; if ($newimage->saveAs(addslashes($path . DIRECTORY_SEPARATOR . $new_name), true)) { return $new_name; } else { return false; } }
/** * Check some things prior to save * */ public function beforeSave() { //If its being modified if (!$this->getIsNewRecord()) { //Invalidate any change if the subject has been showed while it was being modified if (Yii::app()->db->createCommand("SELECT * FROM live_subject WHERE subject_id = {$this->id}")->queryRow()) { if (Yii::app()->controller->action->id != 'fetch' and Yii::app()->controller->action->id != 'view' and Yii::app()->controller->action->id != 'vote') { $this->addError('title', Yii::t('subject', 'Right now this subject is either in the comming up queue or in the live-now stream. You can not modify it.')); return false; } } if (Yii::app()->controller->action->id == 'update') { //Insert the content type on its proper table switch ($this->content_type_id) { case 1: //Image if (strlen($this->image_url) > 2) { if (!Yii::app()->db->createCommand()->update('content_image', array('url' => $this->image_url), 'id=:id', array(':id' => $this->content_id))) { } $this->addError('image', Yii::t('subject', 'We could not save the image url in the database.')); return false; break; } if (get_class($this->image) != 'CUploadedFile') { break; } //Nothing to do, user didn't submitted any file, so things stay the same $content_image = Yii::app()->db->createCommand()->select('*')->from('content_image')->where('id=:id', array(':id' => $this->content_id))->queryRow(); $img_name = $content_image['id'] . '.' . $content_image['extension']; $img_path = Yii::app()->params['img_path']; Yii::import('ext.EUploadedImage'); $this->image = EUploadedImage::getInstance($this, 'image'); $this->image->maxWidth = 980; $this->image->maxHeight = 750; $this->image->thumb = array('maxWidth' => 300, 'maxHeight' => 300, 'prefix' => 'small_'); if (!$this->image->saveAs(Yii::app()->params['webdir'] . DIRECTORY_SEPARATOR . $img_path . DIRECTORY_SEPARATOR . $img_name)) { $this->addError('image', Yii::t('subject', 'We could not save the image in the disk.')); return false; } break; case 2: //Text if (!Yii::app()->db->createCommand()->update('content_text', array('text' => $this->text), array(':id' => $this->content_id))) { $this->addError('text', Yii::t('subject', 'We could not save the text.')); return false; } break; case 3: //Video //echo "aaaaa".$this->video.$this->content_id; if (!Yii::app()->db->createCommand()->update('content_video', array('embed_code' => $this->video), 'id=:id', array(':id' => $this->content_id))) { // $this->addError('video',Yii::t('subject', 'We could not save the video.')); //return false; } break; } } } else { // Assign the user_id 1 if is a guest $this->user_id = Yii::app()->user->id ? Yii::app()->user->id : 1; $this->deleted = Yii::app()->user->isGuest ? 1 : 0; //Hide the subject by default if its a guest $this->time_submitted = SiteLibrary::utc_time(); $this->user_ip = $_SERVER['REMOTE_ADDR']; //Assign subject hash(for guest users that want to register and own an added subject) $this->hash = md5(uniqid("")); //Insert the content type on its proper table switch ($this->content_type_id) { case 1: //Image if (strlen($this->image_url) > 2) { if (!Yii::app()->db->createCommand()->insert('content_image', array('url' => $this->image_url))) { $this->addError('image', Yii::t('subject', 'We could not save the image url in the database.')); return false; } break; } //If there was an image in the post submitted, then save it in the disk and on its proper content table $img_extension = $this->image->getExtensionName() ? $this->image->getExtensionName() : ''; $img_type = CFileHelper::getMimeType($this->image->getTempName()); $img_size = $this->image->getSize(); //The path should be changed as time passes so that directory isn't very full(ie:img/1, img/2...) $img_path = Yii::app()->params['img_path']; //If can't save the image in the db or in the disk, then invalidate if (!Yii::app()->db->createCommand()->insert('content_image', array('path' => $img_path, 'extension' => $img_extension, 'type' => $img_type, 'size' => $img_size))) { $this->addError('image', Yii::t('subject', 'We could not save the image in the database.')); return false; } $img_name = Yii::app()->db->getLastInsertID() . '.' . $img_extension; //if(! $this->image->saveAs(Yii::app()->basePath . '/../'.$img_path.'/' . 'large_'.$img_name)) //{ // $this->addError('image','We could not save the image in the disk.'); // return false; //} //WARNING: make sure you have at least ini_set("memory_limit","64M"); Yii::import('ext.EUploadedImage'); $this->image = EUploadedImage::getInstance($this, 'image'); $this->image->maxWidth = 980; $this->image->maxHeight = 750; $this->image->thumb = array('maxWidth' => 300, 'maxHeight' => 300, 'prefix' => 'small_'); if (!$this->image->saveAs(Yii::app()->params['webdir'] . DIRECTORY_SEPARATOR . $img_path . DIRECTORY_SEPARATOR . $img_name)) { $this->addError('image', Yii::t('subject', 'We could not save the image in the disk.')); return false; } break; case 2: //Text if (!Yii::app()->db->createCommand()->insert('content_text', array('text' => $this->text))) { $this->addError('text', Yii::t('subject', 'We could not save the text.')); return false; } break; case 3: //Video if (!Yii::app()->db->createCommand()->insert('content_video', array('embed_code' => $this->video))) { $this->addError('video', Yii::t('subject', 'We could not save the video.')); return false; } break; } //Get the insert id as our content id for the subject $this->content_id = Yii::app()->db->getLastInsertID(); } return true; }