diff --git a/.htaccess b/.htaccess index a8a21a1..4434352 100644 --- a/.htaccess +++ b/.htaccess @@ -12,6 +12,7 @@ RewriteBase / # deal with admin first + RewriteRule ^storage/(.*)?$ /storage/$1 [L,PT] RewriteCond %{REQUEST_URI} ^/(admin) diff --git a/backend/components/ImageSizer.php b/backend/components/ImageSizer.php index 0880a9b..9aa9ff6 100644 --- a/backend/components/ImageSizer.php +++ b/backend/components/ImageSizer.php @@ -18,6 +18,7 @@ class ImageSizer extends Widget public $file; public $model; public $form; + public $multi = false; public function init(){ @@ -35,7 +36,8 @@ class ImageSizer extends Widget 'field' => $this->field, 'form' => $this->form, 'height' => $this->height, - 'width' => $this->width + 'width' => $this->width, + 'multi' => $this->multi ]); } diff --git a/backend/components/base/BaseController.php b/backend/components/base/BaseController.php index 21e496a..7141e09 100644 --- a/backend/components/base/BaseController.php +++ b/backend/components/base/BaseController.php @@ -26,49 +26,46 @@ class BaseController extends Controller { $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); $size = $img->getSize(); - $ratio = $size->getWidth()/$size->getHeight(); - $height = $h; - $width = round($height * $ratio); + $width = $size->getWidth(); + $height = $size->getHeight(); + + if($width > $height) { + $y = 0; + $x = ($width - $height) / 2; + $smallestSide = $height; + } else { + $x = 0; + $y = ($height - $width) / 2; + $smallestSide = $width; + } + + + + +// die('$width = '.$width.','.'$height = '.$height.','.'$y = '.$y.','.'$x = '.$x.','.'$smallestSide = '.$smallestSide.',' ); +// +// $ratio = $size->getWidth()/$size->getHeight(); +// +// $height = $h; +// $width = round($height * $ratio); + + $image_p = imagecreatetruecolor($w, $h); + + $image = imagecreatefromjpeg($imageAlias); + imagecopyresampled($image_p, $image, 0, 0, $x, $y, + $w, $h, $smallestSide, $smallestSide); + + imagejpeg($image_p, $imageAliasSave); - $imagine = new Imagine(); - $imagine->open($imageAlias) - ->resize(new Box($width, $h)) - ->save($imageAlias, array('flatten' => false)); - Image::crop($imageAlias, $w, $h,[($width/2)-($w/2),0]) - ->save(Yii::getAlias($imageAliasSave), ['quality' => - 100]); } -// -// private function resizeImg($w, $h, $filepath,$newfilepath){ -// list($width, $height) = getimagesize($filepath); -// if($width > $height) { -// $y = 0; -// $x = ($width - $height) / 2; -// $smallestSide = $height; -// } else { -// $x = 0; -// $y = ($height - $width) / 2; -// $smallestSide = $width; -// } -// -// $image_p = imagecreatetruecolor($w, $h); -// -// $image = imagecreatefromjpeg($filepath); -// -// imagecopyresampled($image_p, $image, 0, 0, $x, $y, -// $w, $h, $smallestSide, $smallestSide); -// -// imagejpeg($image_p, $newfilepath); -// -// -// } + public function actionDownloadPhoto() { @@ -82,7 +79,7 @@ class BaseController extends Controller { $md5_file = md5_file($model->file->tempName); $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension); - $imageLink = '/storage/'.$md5_file.$model->width.'x'.$model->height.'.'.$model->file->extension; + $imageLink = '/storage/'.$md5_file.'/'.$model->width.'x'.$model->height.'.'.$model->file->extension; if(!is_dir($imgDir)) { mkdir($imgDir, 0755, true); diff --git a/backend/components/views/image_sizer.php b/backend/components/views/image_sizer.php index 8a824c2..6ff14ab 100644 --- a/backend/components/views/image_sizer.php +++ b/backend/components/views/image_sizer.php @@ -12,9 +12,11 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl ?> -= $form->field( new \backend\models\ImageSizerForm(), 'file')->fileInput(['id'=>$field, 'data-url'=>"/admin/site/download-photo"]); ?> += $form->field( new \backend\models\ImageSizerForm(), 'file')->fileInput(['id'=>$field, 'data-url'=>"/admin/site/download-photo", 'multiple'=> $multi?'multiple':'' ]); ?> = $form->field($model,$field)->hiddenInput(['id' => "{$field}_picture_link"]) ?> += $form->field(new \backend\models\ImageSizerForm(),'height')->hiddenInput(['value' => $height])->label(false) ?> += $form->field(new \backend\models\ImageSizerForm(), 'width')->hiddenInput(['value' => $width])->label(false) ?>