Beispiel #1
0
 public static function processAvatar($model, $source, $type = "artist")
 {
     try {
         $fileSystem = new Filesystem();
         $alowSize = Yii::app()->params['imageSize'];
         $maxSize = max($alowSize);
         $folderMax = "s0";
         foreach ($alowSize as $folder => $size) {
             // Create folder by ID
             $fileSystem->mkdirs($model->getAvatarPath($model->id, $folder, true));
             @chmod($model->getAvatarPath($model->id, $folder, true), 0755);
             // Get link file by ID
             $savePath[$folder] = $model->getAvatarPath($model->id, $folder);
             if ($size == $maxSize) {
                 $folderMax = $folder;
             }
         }
         // Delete file if exists
         if (file_exists($savePath[$folder])) {
             $fileSystem->remove($savePath);
         }
         if (file_exists($source)) {
             list($width, $height) = getimagesize($source);
             $imgCrop = new ImageCrop($source, 0, 0, $width, $height);
             // aspect ratio for image size
             $aspectRatioW = $aspectRatioH = 1;
             if ($type == "video") {
                 $videoAspectRatio = Yii::app()->params['videoResolutionRate'];
                 list($aspectRatioW, $aspectRatioH) = explode(":", $videoAspectRatio);
             }
             $res = array();
             foreach ($savePath as $k => $v) {
                 $desWidth = $alowSize[$k];
                 $desHeight = round($alowSize[$k] * intval($aspectRatioH) / intval($aspectRatioW));
                 if (file_exists($v) && is_file($v)) {
                     @unlink($v);
                 }
                 if ($width > 4000) {
                     self::ImageCropPro($v, $source, $desWidth, $desHeight, 70);
                 } else {
                     if ($k == $folderMax) {
                         $imgCrop->resizeRatio($v, $desWidth, $desHeight, 70);
                     } else {
                         $imgCrop->resizeCrop($v, $desWidth, $desHeight, 70);
                     }
                 }
             }
             if ($type != "video") {
                 $fileSystem->remove($source);
             }
         }
     } catch (Exception $e) {
         $error = $e->getMessage();
     }
 }
Beispiel #2
0
 public static function processAvatar($id, $source, $type = "blog")
 {
     $fileSystem = new Filesystem();
     $alowSize = Yii::app()->params['imageSize']["{$type}"];
     $maxSize = max($alowSize);
     $folderMax = "s0";
     $pathDir = Yii::app()->params[$type . '_path'];
     foreach ($alowSize as $folder => $size) {
         // Create folder by ID
         $avatarPath = self::getAvatarPath($id, $folder, true, $pathDir);
         $fileSystem->mkdirs($avatarPath);
         @chmod($avatarPath, 0777);
         // Get link file by ID
         $savePath[$folder] = self::getAvatarPath($id, $folder, false, $pathDir);
         if ($size == $maxSize) {
             $folderMax = $folder;
         }
     }
     // Delete file if exists
     if (file_exists($savePath[$folder])) {
         $fileSystem->remove($savePath);
     }
     if (file_exists($source)) {
         list($width, $height) = getimagesize($source);
         $imgCrop = new ImageCrop($source, 0, 0, $width, $height);
         // aspect ratio for image size
         $aspectRatioW = $aspectRatioH = 1;
         foreach ($savePath as $k => $v) {
             $desWidth = $alowSize[$k];
             $desHeight = round($alowSize[$k] * intval($aspectRatioH) / intval($aspectRatioW));
             if (file_exists($v) && is_file($v)) {
                 @unlink($v);
             }
             if ($k == $folderMax) {
                 $imgCrop->resizeRatio($v, $desWidth, $desHeight, 100);
             } else {
                 $imgCrop->resizeCrop($v, $desWidth, $desHeight, 100);
             }
         }
         //remove file source
         $fileSystem->remove($source);
     }
 }
Beispiel #3
0
 public function resizeImage($source, $width, $height, $type = 'ratio')
 {
     $assetPath = $this->imgResizePath;
     $fullPathSource = $source;
     if (!file_exists($fullPathSource) || !is_file($fullPathSource)) {
         return false;
     }
     $ext = strtolower(substr(strrchr($source, '.'), 1));
     $fileName = strtolower(substr(strrchr($source, '/'), 1));
     $folderType = substr($type, 0, 1);
     $source = str_replace($this->imgOrgPath, "", $source);
     //$rzname = strtolower(substr($source, 0, strpos($source,'.')))."_{$width}_{$height}.{$ext}";
     $rzname = strtolower(substr($source, 0, strrpos($source, '/'))) . "/{$width}_{$height}_{$folderType}/{$fileName}";
     $rzname = strtolower(substr($rzname, 0, strpos($rzname, '.'))) . "/" . md5_file($fullPathSource) . ".jpg";
     if (file_exists($assetPath . DS . $rzname)) {
         return $rzname;
     }
     //empty dir first
     Utils::emptyDir(dirname($assetPath . DS . $rzname));
     Utils::makeDir($assetPath);
     if (!file_exists($assetPath) && !mkdir($assetPath, 0755)) {
         return $source;
     }
     //$folders = explode('/',$source);
     $folders = explode('/', $rzname);
     $tmppath = $assetPath . DS;
     for ($i = 0; $i < count($folders) - 1; $i++) {
         if (!file_exists($tmppath . $folders[$i]) && !mkdir($tmppath . $folders[$i], 0755)) {
             return $source;
         }
         $tmppath = $tmppath . $folders[$i] . '/';
     }
     if (!file_exists($assetPath . "/tmp")) {
         mkdir($assetPath . "/tmp", 0755);
     }
     list($r_width, $r_height) = getimagesize($fullPathSource);
     $imgCrop = new ImageCrop($fullPathSource, 0, 0, $r_width, $r_height, $assetPath . "/tmp");
     switch ($type) {
         case "fix":
             $imgCrop->resizeFix($assetPath . DS . $rzname, $width, $height, 90);
             break;
         case "ratio":
             $imgCrop->resizeRatio($assetPath . DS . $rzname, $width, $height, 90);
             break;
         case "crop":
         default:
             $imgCrop->resizeCrop($assetPath . DS . $rzname, $width, $height, 90);
             break;
     }
     @chmod($assetPath . DS . $rzname, 0775);
     return $rzname;
 }
 /**
  * upload file
  */
 protected function uploadFile($file, $model)
 {
     $coverPath = "";
     $fileSystem = new Filesystem();
     if (isset($file['error']) && $file['error'] == 0) {
         $ext = explode('.', $file['name']);
         $extFile = $ext[count($ext) - 1];
         $id = $model->id;
         $srcFileName = $id . time() . "." . $extFile;
         $tmpPath = Yii::app()->getRuntimePath();
         $fileDesPath = $tmpPath . DIRECTORY_SEPARATOR . $srcFileName;
         try {
             if (move_uploaded_file($file['tmp_name'], $fileDesPath)) {
                 list($width, $height) = getimagesize($fileDesPath);
                 if ($width < $this->coverWidth || $height < $this->coverHeight) {
                     return false;
                 }
                 $imgCrop = new ImageCrop($fileDesPath, 0, 0, $width, $height);
                 $coverPath = $model->getCoverPath($model->id);
                 Utils::makeDir(dirname($coverPath));
                 $imgCrop->resizeCrop($coverPath, $this->coverWidth, $this->coverHeight, 100);
                 $url = $model->getCoverUrl($model->id);
                 unlink($fileDesPath);
             }
         } catch (Exception $e) {
             echo $e->getMessage();
         }
     }
     return $url;
 }