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(); } }
public static function generalStoragePath($objId, $fileType = 'jpg', $storage, $isUrl = false) { $year = date('Y'); $month = date('m'); $day = date('d'); $sep = $isUrl ? '/' : DS; $exPath = $year . $sep . $month . $sep . $day; $filePath = $storage . $sep . $exPath; $fileSystem = new Filesystem(); $res = $fileSystem->mkdirs($filePath, '0755'); return $filePath . $sep . $objId . '.' . $fileType; }
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); } }
function createImage($model, $image_path) { if (file_exists($image_path)) { $fileSystem = new Filesystem(); $s0Path = $model->getAvatarPath($model->id, "s0", false); // Origin $s1Path = $model->getAvatarPath($model->id, "s1", false); // Resize $s2Path = $model->getAvatarPath($model->id, "s2", false); // Thumb $fileSystem->mkdirs(dirname($s0Path)); $fileSystem->mkdirs(dirname($s1Path)); $fileSystem->mkdirs(dirname($s2Path)); list($width, $height) = getimagesize($image_path); $imgCrop = new ImageCrop($image_path, 0, 0, $width, $height); $fileSystem->copy($image_path, $s0Path); $imgCrop->resizeFix($s1Path, 690, 250); $imgCrop->resizeFix($s2Path, 100, 100); $fileSystem->remove($image_path); } }