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); } return "/storage/imageNotFound" . $width . "x" . $height . ".jpg"; } } } } return $dir; } /** * @param string $array String to split * * @return array Array of image paths */ function ShowGallery($array) { $gallery = explode(',', $array); if(is_array($gallery)) { array_splice($gallery, -1); return $gallery; } else { return []; } } 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; } } }