Skip to content

Instantly share code, notes, and snippets.

@weblegko
Created July 29, 2019 21:14
Show Gist options
  • Save weblegko/f0f4af82e203d484f64514779557f44c to your computer and use it in GitHub Desktop.
Save weblegko/f0f4af82e203d484f64514779557f44c to your computer and use it in GitHub Desktop.
<?php
/**
* Date: 29.07.2019
* Time: 20:49
* hacked by @weblegko
* plugin for slug and many different thubnail creation on file upload
*/
namespace mihaildev\elfinder\plugin;
use mihaildev\elfinder\PluginInterface;
use yii\helpers\Inflector;
use yii\imagine\Image;
use Imagine\Gd;
use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
class SluggableAndThumnails extends PluginInterface
{
public $bind = [
'upload.presave' => 'createSlugAndThumnails'
];
public $lowercase = true;
public $replacement = '-';
/**
* @return string
*/
public function getName()
{
return 'Sluggable';
}
private function createThumb($originFilePath, $thmbDir, $thumbFile, $width, $height){
if(!file_exists( $thmbDir )) {
mkdir($thmbDir, 0777);
}
$thumbPath = $thmbDir .'/'. $thumbFile;
Image::thumbnail($originFilePath, $width, $height)->save($thumbPath, ['quality' => 90]);
//Image::getImagine()->open($originFilePath)->thumbnail(new Box($width, $height))->save($savePath , ['quality' => 90]);
}
public function createSlugAndThumnails(&$path, &$name, $src, $elfinder, $volume){
if(!$this->isEnable($volume))
return false;
$ext = pathinfo($name, PATHINFO_EXTENSION);
$filename = pathinfo($name, PATHINFO_FILENAME);
$lowercase = $this->getOption('lowercase', $volume);
$replacement = $this->getOption('replacement', $volume);
$originFilePath = $src;
$filename = Inflector::slug($filename, $replacement, $lowercase);
if($lowercase)
$ext = strtolower($ext);
$name = empty($ext) ? $filename : $filename . '.' . $ext;
// создаем миниатюрки тольк оесли была загружена картинка
if($ext == 'jpg' || $ext == 'png' ) {
$thumbFile = empty($ext) ? $filename : $filename . '.' . $ext;
$uploads = \Yii::getAlias('@frontend') . '/web/files/uploads';
//Создаем миниатюру Mini - 150х150
$this->createThumb($originFilePath, \Yii::getAlias('@thumbPath'), $thumbFile, 50, 50);
$this->createThumb($originFilePath, \Yii::getAlias('@thumbMiniPath'), $thumbFile, 150, null);
$this->createThumb($originFilePath, \Yii::getAlias('@thumbMediumPath'), $thumbFile, 650, null);
$this->createThumb($originFilePath, \Yii::getAlias('@thumbLargePath'), $thumbFile, 1024, null);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment