public function changeSlide($fileName) { // folder for uploaded files $tempFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . Yii::app()->params['folders']['temp']; if (!is_writable($tempFolder)) { throw new CException('temporary folder is not exists or not writable. Path:' . $tempFolder); } $uploader = new FileUploader(Yii::app()->params['uploader']['allowedFileExtensions'], Yii::app()->params['uploader']['sizeLimit']); $result = $uploader->handleUpload($tempFolder); if (!isset($result['error'])) { $imageHandler = new CImageHandler(); $imageHandler->load($tempFolder . $result['filename']); try { $imageHandler->cropAndScaleFromCenter(1040, 380); $imageHandler->save($this->getSliderImagePath($fileName)); $this->response = array('fileName' => $fileName, 'imageSrc' => $this->getSliderImageSrc($fileName)); return TRUE; } catch (CException $e) { $this->response = array('errorMessage' => $fileName . $e->getMessage()); return FALSE; } } else { $this->response = array('errorMessage' => $result['error']); return FALSE; } }
public static function createFullSize($image) { $newPath = self::returnModifiedFullImgPath($image); $originalPath = self::returnOrigPath($image); if (!file_exists($originalPath)) { return ''; } if (!param('useWatermark')) { return self::createSimple($image, $originalPath, $newPath); } else { // using watermark $positionTransform = array(ImageSettings::POS_LEFT_TOP => CImageHandler::CORNER_LEFT_TOP, ImageSettings::POS_LEFT_MIDDLE => CImageHandler::CORNER_LEFT_CENTER, ImageSettings::POS_LEFT_BOTTOM => CImageHandler::CORNER_LEFT_BOTTOM, ImageSettings::POS_CENTER_TOP => CImageHandler::CORNER_CENTER_TOP, ImageSettings::POS_CENTER_MIDDLE => CImageHandler::CORNER_CENTER, ImageSettings::POS_CENTER_BOTTOM => CImageHandler::CORNER_CENTER_BOTTOM, ImageSettings::POS_RIGHT_TOP => CImageHandler::CORNER_RIGHT_TOP, ImageSettings::POS_RIGHT_MIDDLE => CImageHandler::CORNER_RIGHT_CENTER, ImageSettings::POS_RIGHT_BOTTOM => CImageHandler::CORNER_RIGHT_BOTTOM); if (param('watermarkType') == ImageSettings::WATERMARK_FILE) { $waterMarkFileName = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . self::UPLOAD_DIR . DIRECTORY_SEPARATOR . param('watermarkFile'); if (!file_exists($waterMarkFileName)) { return self::createSimple($image, $originalPath, $newPath); } $img = new CImageHandler(); if (!$img->load($originalPath)) { return ''; } $img->watermark($waterMarkFileName, self::WATERMARK_OFFSET_X, self::WATERMARK_OFFSET_Y, $positionTransform[param('watermarkPosition', ImageSettings::POS_RIGHT_BOTTOM)]); $img->save($newPath); return self::returnFullSizeUrl($image); } if (param('watermarkType') == ImageSettings::WATERMARK_TEXT) { // apply text to image $img = new CImageHandler(); $img->load($originalPath); $font = Yii::getPathOfAlias('application.modules.images.fonts') . DIRECTORY_SEPARATOR . 'Verdana.ttf'; $textColor = param('watermarkTextColor'); $textColor = str_replace('#', '', $textColor); $color = array(hexdec(substr($textColor, 0, 2)), hexdec(substr($textColor, 2, 2)), hexdec(substr($textColor, 4, 2))); // alpha between 0 and 127 $alpha = 100 - param('watermarkTextOpacity', 0); $alpha = $alpha / 100 * 127; $img->text(param('watermarkContent'), $font, param('watermarkTextSize'), $color, $positionTransform[param('watermarkPosition', ImageSettings::POS_RIGHT_BOTTOM)], self::TEXT_WATERMARK_OFFSET_X, self::TEXT_WATERMARK_OFFSET_Y, 0, $alpha); $img->save($newPath); return self::returnFullSizeUrl($image); } } return ''; }
/** * Upload and apply new avatar * @return bool * @throws CException */ public function changeAvatar() { // folder for uploaded files $tempFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . Yii::app()->params['folders']['temp']; $avatarsFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . Yii::app()->params['folders']['userAvatars']; if (!is_writable($tempFolder)) { throw new CException('temporary folder is not exists or not writable. Path:' . $tempFolder); } // Upload to temp folder $uploader = new FileUploader(Yii::app()->params['uploader']['allowedFileExtensions'], Yii::app()->params['uploader']['sizeLimit']); $result = $uploader->handleUpload($tempFolder); if (!isset($result['error'])) { // Move file to target folder and make thumbs $imageHandler = new CImageHandler(); $imageHandler->load($tempFolder . $result['filename']); try { $imageHandler->save($avatarsFolder . $result['filename']); $imageHandler->cropAndScaleFromCenter(self::SMALL_THUMB_WIDTH, self::SMALL_THUMB_HEIGHT); $imageHandler->save($avatarsFolder . self::THUMB_PREFIX_SMALL . $result['filename']); $imageHandler->load($tempFolder . $result['filename']); $imageHandler->cropAndScaleFromCenter(self::MICRO_THUMB_WIDTH, self::MICRO_THUMB_HEIGHT); $imageHandler->save($avatarsFolder . self::THUMB_PREFIX_MICRO . $result['filename']); if ($this->hasPhoto()) { $this->_removeOldPhoto(); } $this->photo = $result['filename']; $this->_response = array('originalSrc' => $this->getOriginalPhoto(), 'thumbSrc' => $this->getSmallThumbnail()); $this->save(FALSE); return TRUE; } catch (CException $e) { $this->_response = array('errorMessage' => $e->getMessage()); return FALSE; } } else { $this->_response = array('errorMessage' => $result['error']); return FALSE; } }
/** * Copy image from temporary folder to proper destination and create thumbnails */ protected function saveImage() { $imageHandler = new CImageHandler(); $imageHandler->load($this->getTempFolder(TRUE) . $this->image); $imageHandler->save($this->getImagesFolder(TRUE) . $imageHandler->getBaseFileName()); $settings = $this->getThumbsSettings(); if (!empty($settings)) { $imageHandler = new CImageHandler(); $imageHandler->load($this->getTempFolder(TRUE) . $this->image); foreach ($settings as $prefix => $dimensions) { list($width, $height) = $dimensions; $imageHandler->thumb($width, $height)->save($this->getImagesFolder(TRUE) . $prefix . $imageHandler->getBaseFileName()); } } }
/** * Upload and apply new avatar * @return bool * @throws CException */ public static function uploadPhoto($userId) { // folder for uploaded files $tempFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . Yii::app()->params['folders']['temp']; $photosFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . Yii::app()->params['folders']['userPhotos']; $photosFolder .= $userId . DIRECTORY_SEPARATOR; if (!is_writable($tempFolder)) { throw new CException('temporary folder is not exists or not writable. Path:' . $tempFolder); } // Upload to temp folder $uploader = new FileUploader(Yii::app()->params['uploader']['allowedFileExtensions'], Yii::app()->params['uploader']['sizeLimit']); $result = $uploader->handleUpload($tempFolder); if (!isset($result['error'])) { // Move file to target folder and make thumbs $imageHandler = new CImageHandler(); $imageHandler->load($tempFolder . $result['filename']); $imageHandler->save($photosFolder . $result['filename']); $imageHandler->cropAndScaleFromCenter(300, 300); $imageHandler->save($photosFolder . self::THUMB_PREFIX_SMALL . $result['filename']); $model = new Photos(); $model->owner_id = $userId; $model->file_name = $result['filename']; $model->save(); return array('originalSrc' => $model->getOriginal(), 'thumbSrc' => $model->getSmallThumbnail(), 'photoId' => $model->id); } else { throw new CException($result['error']); } }