From 55bd0bb9f0db81fb629eed7df86c1e19c95bdd35 Mon Sep 17 00:00:00 2001 From: yarik Date: Wed, 31 Aug 2016 14:59:08 +0300 Subject: [PATCH] Commit --- common/behaviors/ShowImage.php | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- common/models/Portfolio.php | 3 +++ frontend/views/company/portfolio-view.php | 4 ++-- frontend/views/performer/blog-view.php | 22 ++++++++++++++++++++-- frontend/views/performer/portfolio-view.php | 6 +++--- frontend/web/css/style.css | 13 +++++++++++++ frontend/web/images/ico-google.png | Bin 0 -> 2580 bytes frontend/web/images/watermark.png | Bin 0 -> 4374 bytes storage/.htaccess | 12 ++++++------ 9 files changed, 118 insertions(+), 40 deletions(-) create mode 100644 frontend/web/images/ico-google.png create mode 100755 frontend/web/images/watermark.png diff --git a/common/behaviors/ShowImage.php b/common/behaviors/ShowImage.php index 540268d..b861ecb 100755 --- a/common/behaviors/ShowImage.php +++ b/common/behaviors/ShowImage.php @@ -1,14 +1,15 @@ resizeImg($width, $height, $original, $filename); } else { - - $imageNotFound = yii\helpers\Url::to('@storage')."/imageNotFound".$width."x".$height.".jpg"; - if(!file_exists($imageNotFound)){ - $resizer->resizeImg($width, $height, yii\helpers\Url::to('@storage')."/imageNotFound.jpg", $imageNotFound ); + $imageNotFound = yii\helpers\Url::to('@storage') . "/imageNotFound" . $width . "x" . $height . ".jpg"; + if(!file_exists($imageNotFound)) { + $resizer->resizeImg($width, $height, yii\helpers\Url::to('@storage') . "/imageNotFound.jpg", $imageNotFound); } - return "/storage/imageNotFound".$width."x".$height.".jpg"; - - + return "/storage/imageNotFound" . $width . "x" . $height . ".jpg"; } - + } - + } - + } - return $dir; - } - + /** * @param string $array String to split * @@ -73,23 +69,71 @@ */ function ShowGallery($array) { - + $gallery = explode(',', $array); if(is_array($gallery)) { array_splice($gallery, -1); return $gallery; } else { - return [ ]; + return []; } - + } - - public function ShowAvatar($dir, $width, $height = NULL) { - if(empty($dir)) { + + public function ShowAvatar($dir, $width, $height = NULL) + { + if(empty( $dir )) { return '/images/avatar-bg.png'; } else { return $this->minImg($dir, $width, $height); } } - + + /** + * Add watermark to image + * + * @param string $path image path + * + * @return yii\base\Component + */ + public function watermark($path) + { + $watermark_path = Yii::getAlias('@webroot/images/watermark.png'); + $image = Image::getImagine() + ->open($path); + $watermark = Image::getImagine() + ->open($watermark_path); + $image_size = $image->getSize(); + $watermark_size = $watermark->getSize(); + if($image_size->getWidth() < ($watermark_size->getWidth() + 20) || ($image_size->getHeight() + 20) < $watermark_size->getHeight()) { + return $this->owner; + } + $position_left = $image_size->getWidth() - ($watermark_size->getWidth() + 10); + Image::watermark($path, $watermark_path, [$position_left, 10])->save(preg_replace('/^(.*)(\.\w+)$/', '${1}_watermark${2}', $path)); + return $this->owner; + } + + function getWatermark($path, $width, $height = NULL) + { + if($width == 'original') { + $path_original = $this->minImg($path, 'original'); + $path_watermark = str_replace('original', 'original_watermark', $path_original); + if(!file_exists(Yii::getAlias('@documentRoot').$path_watermark)) { + $this->watermark(Yii::getAlias('@documentRoot').$path_original); + } + return $path_watermark; + } else { + $path_original = $this->minImg($path, $width, $height); + preg_match('/^.*(\.\w+)$/', $path_original, $extension); + $path_watermark = $path_original; + if(isset($extension[1])) { + $path_watermark = str_replace($extension[1], '_watermark'.$extension[1], $path_original); + } + if(!file_exists(Yii::getAlias('@documentRoot').$path_watermark)) { + $this->watermark(Yii::getAlias('@documentRoot').$path_original); + } + return $path_watermark; + } + } + } \ No newline at end of file diff --git a/common/models/Portfolio.php b/common/models/Portfolio.php index c88409c..f40481e 100755 --- a/common/models/Portfolio.php +++ b/common/models/Portfolio.php @@ -10,6 +10,7 @@ use yii\behaviors\TimestampBehavior; use yii\db\ActiveQuery; use yii\db\Expression; + use yii\base\Component; /** * This is the model class for table "portfolio". @@ -34,6 +35,8 @@ * @property Rating[] $rating * @property string $ratingValue * @method string minImg( string $dir, $width, $height = NULL ) Resizes image + * @method Component watermark(string $path) Add watermark to image + * @method string getWatermark(string $path, mixed $width, mixed $height) Get watermarked image * @method array ShowGallery( string $array ) Splits string to image paths array */ class Portfolio extends \yii\db\ActiveRecord diff --git a/frontend/views/company/portfolio-view.php b/frontend/views/company/portfolio-view.php index 2b2a81a..185b542 100755 --- a/frontend/views/company/portfolio-view.php +++ b/frontend/views/company/portfolio-view.php @@ -40,7 +40,7 @@ if(!empty( $portfolio_user )) { echo Html::img($portfolio->minImg(ArrayHelper::getValue($portfolio_user_gallery, 0, ''), '720', '280')); } else { - echo Html::img($portfolio->minImg($portfolio->cover, '720', '280')); + echo Html::img($portfolio->getWatermark($portfolio->cover, '720', '280')); } ?> @@ -122,7 +122,7 @@ foreach($portfolio->ShowGallery($portfolio->gallery->photo) as $one_photo) { ?>
  • - +
  • params[ 'user' ] = $user; - + $this->title = $article->name; ?>
    @@ -29,6 +31,22 @@
    description ?>
    +
    minImg(ArrayHelper::getValue($portfolio_user_gallery, 0, ''), '720', '280')); } else { - echo Html::img($portfolio->minImg($portfolio->cover, '720', '280')); + echo Html::img($portfolio->getWatermark($portfolio->cover, '720', '280')); } ?> @@ -124,7 +125,7 @@ foreach($portfolio->ShowGallery($portfolio->gallery->photo) as $one_photo) { ?>
  • - +
  • -