示例#1
0
 /**
  * 按比例产生缩略图
  * @param unknown_type $fileName 相对于Yii::app()->uploads
  * @param unknown_type $width
  * @param unknown_type $height
  */
 public static function xImage($fileName, $width, $height)
 {
     //文件是否存在
     $sourceFile = Yii::app()->basePath . "/../" . $fileName;
     if (!file_exists($sourceFile)) {
         return false;
     }
     $ext = DxdUtil::fileExt($fileName);
     if (!in_array(strtolower($ext), array('jpg', 'png', 'jpeg'))) {
         return false;
     }
     //新文件名,形如 filename_120_100.jpg
     $newFileName = substr($fileName, 0, strrpos($fileName, ".")) . "_" . $width . "_" . $height . "." . $ext;
     $targetFile = Yii::app()->basePath . "/../caches/" . $newFileName;
     //文件是否已经被缩略过
     if (file_exists($targetFile)) {
         return "caches/" . $newFileName;
     }
     //开始压缩
     Yii::import('application.extensions.EWideImage.EWideImage');
     $dir = dirname($targetFile);
     if (!file_exists($dir)) {
         DxdUtil::createFolders($dir);
     }
     try {
         EWideImage::load(Yii::app()->basePath . "/../" . $fileName)->resize($width, $height)->saveToFile($targetFile);
     } catch (Exception $e) {
         return false;
     }
     return "caches/" . $newFileName;
 }
示例#2
0
 public function actionPhotosendproc()
 {
     if (isset($_REQUEST["idPerson"])) {
         try {
             $model = Person::model()->find("idPerson = " . $_REQUEST["idPerson"]);
             $path = Yii::app()->basePath . "/.." . Yii::app()->params['photosBigPath'];
             $id = $model->idPerson;
             $tfio = $model->PhotoName;
             $file = $path . $tfio;
             if (file_exists($file)) {
                 // $data = file_get_contents($file);
                 //$type = pathinfo($file, PATHINFO_EXTENSION);
                 //$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
                 $img = EWideImage::loadFromFile($file);
                 $tmp_name = $path . md5(time()) . ".jpg";
                 $img->resize(255, null)->saveToFile($tmp_name);
                 $data = file_get_contents($tmp_name);
                 $base64 = base64_encode($data);
                 $res = WebServices::updatePersonPhoto($model->codeU, $base64);
                 if (file_exists($tmp_name)) {
                     unlink($tmp_name);
                 }
                 if ($res == 1) {
                     echo $model->codeU . " : ok!";
                 } else {
                     echo $model->codeU . " : error!";
                 }
             } else {
                 echo "Фото відсутне! ({$file})";
             }
         } catch (Exception $ex) {
             if (file_exists($tmp_name)) {
                 unlink($tmp_name);
             }
             echo $ex->getMessage();
         }
     } else {
         echo "Необхыдно вказати ідентифікатор персони.";
     }
 }
 /**
  * Ajax photoreloader
  * @param integer $id
  */
 public function actionReloadphoto($id)
 {
     try {
         $temp_dir = sys_get_temp_dir();
         $model = $this->loadModel($id);
         //$model = new Person;
         //$model->scenario = "PHOTO";
         $edbeo_file = WebServices::getPersonPhotoByCodeU($model->codeU);
         if (!empty($edbeo_file)) {
             $oldPhoto = $model->PhotoName;
             $tfio = Transliteration::translit2010($model->FirstName) . "_" . Transliteration::translit2010($model->LastName) . "_" . Transliteration::translit2010($model->MiddleName);
             $model->PhotoName = "person_{$id}" . "_{$tfio}.jpg";
             $path = Yii::app()->basePath . "/.." . Yii::app()->params['photosPath'];
             $bigpath = Yii::app()->basePath . "/.." . Yii::app()->params['photosBigPath'];
             $img = EWideImage::loadFromString(base64_decode($edbeo_file));
             if ($img->getWidth() < $img->getHeight()) {
                 $img->resize(120, null)->crop("center", "middle", 120, 150)->saveToFile($path . "person_{$id}" . "_{$tfio}.jpg");
                 $img->resize(180, null)->crop("center", "middle", 180, 225)->saveToFile($bigpath . "person_{$id}" . "_{$tfio}.jpg");
             } else {
                 $img->resize(null, 150)->crop("center", "middle", 120, 150)->saveToFile($path . "person_{$id}" . "_{$tfio}.jpg");
                 $img->resize(null, 225)->crop("center", "middle", 180, 225)->saveToFile($bigpath . "person_{$id}" . "_{$tfio}.jpg");
             }
             if ($model->save()) {
                 //PC::debug($model->PhotoName);
             } else {
                 //PC::debug(print_r($model->getErrors()));
             }
             echo CJSON::encode(array("result" => "SUCCESS", "data" => Yii::app()->request->baseUrl . Yii::app()->params['photosPath'] . $model->PhotoName));
         } else {
             echo CJSON::encode(array("result" => "ERROR", "data" => "Фото відсутнє!"));
         }
     } catch (Exception $e) {
         if (defined('YII_DEBUG')) {
             Yii::log($e->getMessage(), CLogger::LEVEL_INFO, 'actionReloadphoto');
         }
         echo CJSON::encode(array("result" => "ERROR", "data" => $e->getMessage()));
     }
 }
示例#4
0
 public function getFileUrl($width = null, $height = null, $fit = null)
 {
     assert(null === $fit || self::COVER === $fit || self::CONTAIN === $fit);
     $path = $this->path($width, $height, $fit);
     if (!file_exists($path)) {
         $originalPath = $this->path();
         set_error_handler(create_function('$severity, $message, $file, $line', 'throw new ErrorException($message, $severity, $severity, $file, $line);'));
         try {
             $image = EWideImage::load($originalPath);
             if (self::COVER === $fit) {
                 $image = $image->resize($width, $height, 'outside')->crop('center', 'center', $width, $height);
             } else {
                 $image = $image->resize($width, $height, 'fill');
             }
             $image->saveToFile($path);
         } catch (Exception $exception) {
             Yii::log('Failed to resize image: ' . print_r(array('id' => $this->id, 'width' => $width, 'height' => $height, 'fit' => $fit), true), 'error', 'app.image.getUrl');
         }
         restore_error_handler();
     }
     return strtr('{baseUrl}/{basePath}/{filename}', array('{baseUrl}' => Yii::app()->baseUrl, '{basePath}' => Yii::app()->params['images']['basePath'], '{filename}' => $this->filename($width, $height, $fit)));
 }
示例#5
0
 /**
  * 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($id)
 {
     $this->layout = '//layouts/column2_noblock';
     $model = $this->loadModel($id);
     //var_dump($_POST);
     //var_dump($model);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Person'])) {
         $model->attributes = $_POST['Person'];
         if (empty($_POST['Person']['KOATUUCodeL2ID'])) {
             $model->KOATUUCodeL2ID = null;
         }
         if (empty($_POST['Person']['KOATUUCodeL3ID'])) {
             $model->KOATUUCodeL3ID = null;
         }
         /*if (isset($_POST['Documents']['inndoc'])) {
               $model->inndoc->attributes = $_POST['Documents']['inndoc'];
               $model->inndoc->PersonID = $model->idPerson;
           }*/
         if (isset($_POST['PersonContacts']['homephone'])) {
             $model->homephone->attributes = $_POST['PersonContacts']['homephone'];
             $model->homephone->PersonID = $model->idPerson;
         }
         if (isset($_POST['PersonContacts']['mobphone'])) {
             $model->mobphone->attributes = $_POST['PersonContacts']['mobphone'];
             $model->mobphone->PersonID = $model->idPerson;
         }
         //if ($model->validate() && $model->inndoc->validate("INN") && $model->homephone->validate() && $model->mobphone->validate() && $model->save()) {
         if ($model->validate() && $model->homephone->validate() && $model->mobphone->validate() && $model->save()) {
             $file = CUploadedFile::getInstance($model, 'image');
             if (!empty($file)) {
                 $tfio = Transliteration::text($model->FirstName) . "_" . Transliteration::text($model->LastName) . "_" . Transliteration::text($model->MiddleName);
                 $path = Yii::app()->basePath . "/.." . Yii::app()->params['photosPath'];
                 $bigpath = Yii::app()->basePath . "/.." . Yii::app()->params['photosBigPath'];
                 $img = EWideImage::loadFromFile($file->getTempName());
                 if (file_exists($path . "person_{$id}" . "_{$tfio}.jpg")) {
                     unlink($path . "person_{$id}" . "_{$tfio}.jpg");
                 }
                 if (file_exists($bigpath . "person_{$id}" . "_{$tfio}.jpg")) {
                     unlink($bigpath . "person_{$id}" . "_{$tfio}.jpg");
                 }
                 if ($img->getWidth() < $img->getHeight()) {
                     $img->resize(120, null)->crop("center", "middle", 120, 150)->saveToFile($path . "person_{$id}" . "_{$tfio}.jpg");
                     $img->resize(180, null)->crop("center", "middle", 180, 225)->saveToFile($bigpath . "person_{$id}" . "_{$tfio}.jpg");
                 } else {
                     $img->resize(null, 150)->crop("center", "middle", 120, 150)->saveToFile($path . "person_{$id}" . "_{$tfio}.jpg");
                     $img->resize(null, 225)->crop("center", "middle", 180, 225)->saveToFile($bigpath . "person_{$id}" . "_{$tfio}.jpg");
                 }
                 unlink($file->getTempName());
                 $model->PhotoName = "person_{$id}" . "_{$tfio}.jpg";
                 $model->save();
             }
             //$model->inndoc->save();
             $model->homephone->save();
             $model->mobphone->save();
             //$model->SendEdboRequest();
             $this->redirect(array('view', 'id' => $model->idPerson));
         }
     }
     $this->render('update', array('model' => $model));
 }
 public function createImages($path, $bigfilename, $thumbfilename)
 {
     Yii::import('common.extensions.EWideImage.EWideImage');
     //include library for image manipulation
     $srcfile = $this->makeFileName($path, 'raw', $bigfilename);
     if (file_exists($srcfile)) {
         $srcImg = EWideImage::load($srcfile);
         //load image from raw
         //create main image
         $fileName = $this->makeFileName($path, '', $bigfilename);
         $dstImg = $this->fitwidth($srcImg, $this->_fullSizeW, $this->_fullSizeH);
         $dstImg->saveToFile($fileName);
         //create thumb image
         $fileName = $this->makeFileName($path, '', $thumbfilename);
         $dstImg = $this->fitwidth($srcImg, $this->_thumbSizeW, $this->_thumbSizeH);
         $dstImg->saveToFile($fileName);
         foreach ($this->_imageSizes as $size) {
             switch ($size['operation']) {
                 case 'crop':
                     $dstImg = $srcImg->crop('center', 'center', $size['options']['width'], $size['options']['height']);
                     break;
                 case 'resize':
                     $dstImg = $srcImg->resize($size['options']['width'], $size['options']['height'], 'fill');
                     break;
                 case 'bestfit':
                     $dstImg = $this->bestfit($srcImg, $size['options']['width'], $size['options']['height']);
                     break;
                 case 'fitwidth':
                     $dstImg = $this->fitwidth($srcImg, $size['options']['width'], $size['options']['height']);
                     break;
                 case 'resizewidth':
                     $dstImg = $this->resizewidth($srcImg, $size['options']['width'], $size['options']['height']);
                     break;
                 default:
                     break;
             }
             $fileName = $this->makeFileName($path, $size['name'], $bigfilename);
             $this->makeDir(dirname($fileName));
             $dstImg->saveToFile($fileName);
         }
     }
 }