Commit 9532b442e1b30e3c7f1aed4f7b3018a845a9121f
1 parent
cc658b4c
"current" function fix
Showing
5 changed files
with
198 additions
and
165 deletions
Show diff stats
common/components/artboximage/ArtboxImageHelper.php
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -namespace common\components\artboximage; | |
| 4 | - | |
| 5 | -use Yii; | |
| 6 | -use yii\base\Object; | |
| 7 | -use yii\helpers\ArrayHelper; | |
| 8 | -use yii\helpers\Html; | |
| 9 | - | |
| 10 | -class ArtboxImageHelper extends Object { | |
| 11 | - /** @var ArtboxImage $imageDriver */ | |
| 12 | - private static $imageDriver; | |
| 13 | - private static $presets; | |
| 14 | - | |
| 15 | - public function getDriver() { | |
| 16 | - if (empty(self::$imageDriver)) { | |
| 17 | - self::$imageDriver = Yii::$app->artboximage; | |
| 2 | + | |
| 3 | + namespace common\components\artboximage; | |
| 4 | + | |
| 5 | + use Yii; | |
| 6 | + use yii\base\Object; | |
| 7 | + use yii\helpers\ArrayHelper; | |
| 8 | + use yii\helpers\Html; | |
| 9 | + | |
| 10 | + class ArtboxImageHelper extends Object | |
| 11 | + { | |
| 12 | + | |
| 13 | + /** @var ArtboxImage $imageDriver */ | |
| 14 | + private static $imageDriver; | |
| 15 | + | |
| 16 | + private static $presets; | |
| 17 | + | |
| 18 | + public static function getDriver() | |
| 19 | + { | |
| 20 | + if(empty( self::$imageDriver )) { | |
| 21 | + self::$imageDriver = Yii::$app->artboximage; | |
| 22 | + } | |
| 23 | + return self::$imageDriver; | |
| 18 | 24 | } |
| 19 | - return self::$imageDriver; | |
| 20 | - } | |
| 21 | - | |
| 22 | - public function getPreset($preset) { | |
| 23 | - | |
| 24 | - if (empty(self::$presets)) { | |
| 25 | - self::$presets = self::getDriver()->presets; | |
| 25 | + | |
| 26 | + public static function getPreset($preset) | |
| 27 | + { | |
| 28 | + | |
| 29 | + if(empty( self::$presets )) { | |
| 30 | + self::$presets = self::getDriver()->presets; | |
| 31 | + } | |
| 32 | + return empty( self::$presets[ $preset ] ) ? NULL : self::$presets[ $preset ]; | |
| 26 | 33 | } |
| 27 | - return empty(self::$presets[$preset]) ? null : self::$presets[$preset]; | |
| 28 | - } | |
| 29 | - | |
| 30 | - public static function getImage($file, $preset, $imgOptions = []) { | |
| 31 | - $preset_alias = is_array($preset) ? array_keys($preset)[0] : null; | |
| 32 | - return Html::img(self::getImageSrc($file, $preset, $preset_alias), $imgOptions); | |
| 33 | - } | |
| 34 | - | |
| 35 | - public static function getImageSrc($file, $preset, $preset_alias = null) { | |
| 36 | - if (is_string($preset)) { | |
| 37 | - $preset_alias = $preset; | |
| 38 | - $preset = self::getPreset($preset); | |
| 34 | + | |
| 35 | + public static function getImage($file, $preset, $imgOptions = []) | |
| 36 | + { | |
| 37 | + $preset_alias = is_array($preset) ? array_keys($preset)[ 0 ] : NULL; | |
| 38 | + return Html::img(self::getImageSrc($file, $preset, $preset_alias), $imgOptions); | |
| 39 | 39 | } |
| 40 | - if (empty($preset) || empty($preset_alias)) { | |
| 41 | - return $file; | |
| 40 | + | |
| 41 | + public static function getImageSrc($file, $preset, $preset_alias = NULL) | |
| 42 | + { | |
| 43 | + if(is_string($preset)) { | |
| 44 | + $preset_alias = $preset; | |
| 45 | + $preset = self::getPreset($preset); | |
| 46 | + } | |
| 47 | + if(empty( $preset ) || empty( $preset_alias )) { | |
| 48 | + return $file; | |
| 49 | + } | |
| 50 | + | |
| 51 | + $filePath = self::getPathFromUrl($file); | |
| 52 | + if(!file_exists($filePath) || !preg_match('#^(.*)\.(' . self::getExtensionsRegexp() . ')$#', $file, $matches)) { | |
| 53 | + return $file; | |
| 54 | + } | |
| 55 | + return self::getPresetUrl($filePath, $preset, $preset_alias); | |
| 42 | 56 | } |
| 43 | - | |
| 44 | - $filePath = self::getPathFromUrl($file); | |
| 45 | - if (!file_exists($filePath) || !preg_match('#^(.*)\.(' . self::getExtensionsRegexp() . ')$#', $file, $matches)) { | |
| 46 | - return $file; | |
| 57 | + | |
| 58 | + public static function fileinputWidget($model, $modelField, $formField = 'fileUpload', $multiple = false) | |
| 59 | + { | |
| 60 | + return Yii::$app->artboximage->fileinputWidget($model, $modelField, $formField, $multiple); | |
| 47 | 61 | } |
| 48 | - return self::getPresetUrl($filePath, $preset, $preset_alias); | |
| 49 | - } | |
| 50 | - | |
| 51 | - public static function fileinputWidget($model, $modelField, $formField = 'fileUpload', $multiple = false) { | |
| 52 | - return Yii::$app->artboximage->fileinputWidget($model, $modelField, $formField, $multiple); | |
| 53 | - } | |
| 54 | - | |
| 55 | - private static function getPathFromUrl($url) { | |
| 56 | - return substr_replace($url, self::getDriver()->rootPath, 0, strlen(self::getDriver()->rootUrl)); | |
| 57 | - } | |
| 58 | - | |
| 59 | - private static function getUrlFromPath($path) { | |
| 60 | - return substr_replace($path, self::getDriver()->rootUrl, 0, strlen(self::getDriver()->rootPath)); | |
| 61 | - } | |
| 62 | - | |
| 63 | - private static function getPresetUrl($filePath, $preset, $preset_alias) { | |
| 64 | - $pathinfo = pathinfo($filePath); | |
| 65 | - $presetPath = $pathinfo['dirname'] .'/styles/'. strtolower($preset_alias); | |
| 66 | - $presetFilePath = $presetPath .'/'. $pathinfo['basename']; | |
| 67 | - $presetUrl = self::getUrlFromPath($presetFilePath); | |
| 68 | - if (file_exists($presetFilePath)) { | |
| 69 | - return $presetUrl; | |
| 62 | + | |
| 63 | + private static function getPathFromUrl($url) | |
| 64 | + { | |
| 65 | + return substr_replace($url, self::getDriver()->rootPath, 0, strlen(self::getDriver()->rootUrl)); | |
| 70 | 66 | } |
| 71 | - if (!file_exists($presetPath)) { | |
| 72 | - @mkdir($presetPath, 0777, true); | |
| 67 | + | |
| 68 | + private static function getUrlFromPath($path) | |
| 69 | + { | |
| 70 | + return substr_replace($path, self::getDriver()->rootUrl, 0, strlen(self::getDriver()->rootPath)); | |
| 73 | 71 | } |
| 74 | - $output = self::createPresetImage($filePath, $preset, $preset_alias); | |
| 75 | - if ( !empty($output) ) { | |
| 76 | - $f = fopen($presetFilePath, 'w'); | |
| 77 | - fwrite($f, $output); | |
| 78 | - fclose($f); | |
| 79 | - return $presetUrl; | |
| 72 | + | |
| 73 | + private static function getPresetUrl($filePath, $preset, $preset_alias) | |
| 74 | + { | |
| 75 | + $pathinfo = pathinfo($filePath); | |
| 76 | + $presetPath = $pathinfo[ 'dirname' ] . '/styles/' . strtolower($preset_alias); | |
| 77 | + $presetFilePath = $presetPath . '/' . $pathinfo[ 'basename' ]; | |
| 78 | + $presetUrl = self::getUrlFromPath($presetFilePath); | |
| 79 | + if(file_exists($presetFilePath)) { | |
| 80 | + return $presetUrl; | |
| 81 | + } | |
| 82 | + if(!file_exists($presetPath)) { | |
| 83 | + @mkdir($presetPath, 0777, true); | |
| 84 | + } | |
| 85 | + $output = self::createPresetImage($filePath, $preset, $preset_alias); | |
| 86 | + if(!empty( $output )) { | |
| 87 | + $f = fopen($presetFilePath, 'w'); | |
| 88 | + fwrite($f, $output); | |
| 89 | + fclose($f); | |
| 90 | + return $presetUrl; | |
| 91 | + } | |
| 92 | + return false; | |
| 80 | 93 | } |
| 81 | - return false; | |
| 82 | - } | |
| 83 | - | |
| 84 | - private static function createPresetImage($filePath, $preset, $preset_alias) | |
| 85 | - { | |
| 86 | - $image = self::getDriver()->load($filePath); | |
| 87 | - foreach ($preset as $action => $data) { | |
| 88 | - switch($action) { | |
| 89 | - case 'resize': | |
| 90 | - $width = empty($data['width']) ? null : $data['width']; | |
| 91 | - $height = empty($data['height']) ? null : $data['height']; | |
| 92 | - $master = empty($data['master']) ? null : $data['master']; | |
| 93 | - $image->resize($width, $height, $master); | |
| 94 | - break; | |
| 95 | - case 'flip': | |
| 96 | - $image->flip(@$data['direction']); | |
| 97 | - break; | |
| 98 | - default: | |
| 99 | - break; | |
| 94 | + | |
| 95 | + private static function createPresetImage($filePath, $preset, $preset_alias) | |
| 96 | + { | |
| 97 | + $image = self::getDriver() | |
| 98 | + ->load($filePath); | |
| 99 | + foreach($preset as $action => $data) { | |
| 100 | + switch($action) { | |
| 101 | + case 'resize': | |
| 102 | + $width = empty( $data[ 'width' ] ) ? NULL : $data[ 'width' ]; | |
| 103 | + $height = empty( $data[ 'height' ] ) ? NULL : $data[ 'height' ]; | |
| 104 | + $master = empty( $data[ 'master' ] ) ? NULL : $data[ 'master' ]; | |
| 105 | + $image->resize($width, $height, $master); | |
| 106 | + break; | |
| 107 | + case 'flip': | |
| 108 | + $image->flip(@$data[ 'direction' ]); | |
| 109 | + break; | |
| 110 | + default: | |
| 111 | + break; | |
| 112 | + } | |
| 100 | 113 | } |
| 114 | + return $image->render(); | |
| 101 | 115 | } |
| 102 | - return $image->render(); | |
| 103 | - } | |
| 104 | - | |
| 105 | - /** | |
| 106 | - * Get extensions regexp | |
| 107 | - * @return string regexp | |
| 108 | - */ | |
| 109 | - private function getExtensionsRegexp() | |
| 110 | - { | |
| 111 | - $keys = array_keys(self::getDriver()->extensions); | |
| 112 | - return '(?i)' . join('|', $keys); | |
| 113 | - } | |
| 114 | - | |
| 115 | - /** | |
| 116 | - * Get size from suffix | |
| 117 | - * @param string $suffix | |
| 118 | - * @return string size | |
| 119 | - */ | |
| 120 | - private function getSizeFromSuffix($suffix) | |
| 121 | - { | |
| 122 | - return array_search($suffix, $this->getSizeSuffixes()); | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * Get suffix from size | |
| 127 | - * @param string $size | |
| 128 | - * @return string suffix | |
| 129 | - */ | |
| 130 | - private function getSufixFromSize($size) | |
| 131 | - { | |
| 132 | - return ArrayHelper::getValue($this->getSizeSuffixes(), $size); | |
| 133 | - } | |
| 134 | - | |
| 135 | - private function getSizeSuffixes() | |
| 136 | - { | |
| 137 | - $suffixes = []; | |
| 138 | - foreach ($this->sizes as $size => $sizeConf) { | |
| 139 | - $suffixes[$size] = ArrayHelper::getValue($this->sizeSuffixes, $size, $this->defaultSizeSuffix . $size); | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * Get extensions regexp | |
| 119 | + * @return string regexp | |
| 120 | + */ | |
| 121 | + private static function getExtensionsRegexp() | |
| 122 | + { | |
| 123 | + $keys = array_keys(self::getDriver()->extensions); | |
| 124 | + return '(?i)' . join('|', $keys); | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Get size from suffix | |
| 129 | + * | |
| 130 | + * @param string $suffix | |
| 131 | + * | |
| 132 | + * @return string size | |
| 133 | + */ | |
| 134 | + private function getSizeFromSuffix($suffix) | |
| 135 | + { | |
| 136 | + return array_search($suffix, $this->getSizeSuffixes()); | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Get suffix from size | |
| 141 | + * | |
| 142 | + * @param string $size | |
| 143 | + * | |
| 144 | + * @return string suffix | |
| 145 | + */ | |
| 146 | + private function getSufixFromSize($size) | |
| 147 | + { | |
| 148 | + return ArrayHelper::getValue($this->getSizeSuffixes(), $size); | |
| 149 | + } | |
| 150 | + | |
| 151 | + private function getSizeSuffixes() | |
| 152 | + { | |
| 153 | + $suffixes = []; | |
| 154 | + foreach($this->sizes as $size => $sizeConf) { | |
| 155 | + $suffixes[ $size ] = ArrayHelper::getValue($this->sizeSuffixes, $size, $this->defaultSizeSuffix . $size); | |
| 156 | + } | |
| 157 | + return $suffixes; | |
| 140 | 158 | } |
| 141 | - return $suffixes; | |
| 142 | - } | |
| 143 | -} | |
| 144 | 159 | \ No newline at end of file |
| 160 | + } | |
| 145 | 161 | \ No newline at end of file | ... | ... |
frontend/views/filter/_brand_item.php
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @var Product $model | |
| 4 | - * @var mixed $key | |
| 5 | - * @var integer $index | |
| 3 | + * @var Product $model | |
| 4 | + * @var mixed $key | |
| 5 | + * @var integer $index | |
| 6 | 6 | * @var ListView $widget |
| 7 | 7 | */ |
| 8 | 8 | use common\components\artboximage\ArtboxImageHelper; |
| ... | ... | @@ -16,7 +16,8 @@ |
| 16 | 16 | /** |
| 17 | 17 | * @var ProductVariant $variant |
| 18 | 18 | */ |
| 19 | - $variant = current($model->variants); | |
| 19 | + $variants = $model->variants; | |
| 20 | + $variant = current($variants); | |
| 20 | 21 | $main_image = ArtboxImageHelper::getImage($variant->getImageUrl(), 'filter_image'); |
| 21 | 22 | unset( $variant ); |
| 22 | 23 | } | ... | ... |
frontend/views/project/_list_item.php
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @var Project $model |
| 4 | - * @var mixed $key | |
| 5 | - * @var integer $index | |
| 4 | + * @var mixed $key | |
| 5 | + * @var integer $index | |
| 6 | 6 | * @var ListView $widget |
| 7 | 7 | */ |
| 8 | 8 | use common\components\artboximage\ArtboxImageHelper; |
| ... | ... | @@ -11,20 +11,28 @@ |
| 11 | 11 | use yii\helpers\Html; |
| 12 | 12 | use yii\helpers\Url; |
| 13 | 13 | use yii\widgets\ListView; |
| 14 | + | |
| 14 | 15 | ?> |
| 15 | 16 | <div class="col-xs-12 col-sm-12 new_articles_data-title"> |
| 16 | 17 | <div class="articles-title"> |
| 17 | 18 | <?php |
| 18 | - echo Html::a($model->title, [ 'project/view', 'id' => $model->project_id ]); | |
| 19 | + echo Html::a($model->title, [ | |
| 20 | + 'project/view', | |
| 21 | + 'id' => $model->project_id, | |
| 22 | + ]); | |
| 19 | 23 | ?> |
| 20 | 24 | </div> |
| 21 | 25 | </div> |
| 22 | 26 | <div class="articles col-xs-12 col-sm-12 col-md-12"> |
| 23 | 27 | <div class="new_article-img"> |
| 24 | 28 | <?php |
| 25 | - if(!empty($model->images)) { | |
| 26 | - $image = current($model->images); | |
| 27 | - echo Html::a(ArtboxImageHelper::getImage($image->getImageUrl(), 'article_list'), [ 'project/view', 'id' => $model->project_id ]); | |
| 29 | + if(!empty( $model->images )) { | |
| 30 | + $images = $model->images; | |
| 31 | + $image = current($images); | |
| 32 | + echo Html::a(ArtboxImageHelper::getImage($image->getImageUrl(), 'article_list'), [ | |
| 33 | + 'project/view', | |
| 34 | + 'id' => $model->project_id, | |
| 35 | + ]); | |
| 28 | 36 | } |
| 29 | 37 | ?> |
| 30 | 38 | <div class="brand-list-desc-txt"> |
| ... | ... | @@ -33,29 +41,35 @@ |
| 33 | 41 | ?> |
| 34 | 42 | <p> |
| 35 | 43 | <?php |
| 36 | - echo Html::a('подробнее', [ 'project/view', 'id' => $model->project_id ]); | |
| 44 | + echo Html::a('подробнее', [ | |
| 45 | + 'project/view', | |
| 46 | + 'id' => $model->project_id, | |
| 47 | + ]); | |
| 37 | 48 | ?> |
| 38 | 49 | </p> |
| 39 | 50 | </div> |
| 40 | 51 | </div> |
| 41 | 52 | <?php |
| 42 | - if(!empty($model->images) && count($model->images) > 1) { | |
| 43 | - $images = array_slice($model->images, 1); | |
| 44 | - ?> | |
| 45 | - <div class="galery-obj-img col-xs-12 col-sm-12"> | |
| 46 | - <a href="<?= Url::to([ 'project/view', 'id' => $model->project_id ]); ?>"> | |
| 47 | - <!----размер картинок макс ширина 165, макс высота 100---> | |
| 48 | - <?php | |
| 49 | - foreach($images as $img) { | |
| 50 | - /** | |
| 51 | - * @var ProjectImage $img | |
| 52 | - */ | |
| 53 | - echo ArtboxImageHelper::getImage($img->getImageUrl(), 'project_list'); | |
| 54 | - } | |
| 55 | - ?> | |
| 56 | - </a> | |
| 57 | - </div> | |
| 58 | - <?php | |
| 59 | - } | |
| 53 | + if(!empty( $model->images ) && count($model->images) > 1) { | |
| 54 | + $images = array_slice($model->images, 1); | |
| 55 | + ?> | |
| 56 | + <div class="galery-obj-img col-xs-12 col-sm-12"> | |
| 57 | + <a href="<?= Url::to([ | |
| 58 | + 'project/view', | |
| 59 | + 'id' => $model->project_id, | |
| 60 | + ]); ?>"> | |
| 61 | + <!----размер картинок макс ширина 165, макс высота 100---> | |
| 62 | + <?php | |
| 63 | + foreach($images as $img) { | |
| 64 | + /** | |
| 65 | + * @var ProjectImage $img | |
| 66 | + */ | |
| 67 | + echo ArtboxImageHelper::getImage($img->getImageUrl(), 'project_list'); | |
| 68 | + } | |
| 69 | + ?> | |
| 70 | + </a> | |
| 71 | + </div> | |
| 72 | + <?php | |
| 73 | + } | |
| 60 | 74 | ?> |
| 61 | 75 | </div> |
| 62 | 76 | \ No newline at end of file | ... | ... |
frontend/views/project/view.php
| ... | ... | @@ -27,7 +27,8 @@ |
| 27 | 27 | <div class="new_article-img"> |
| 28 | 28 | <?php |
| 29 | 29 | if(!empty( $model->images )) { |
| 30 | - $image = current($model->images); | |
| 30 | + $images = $model->images; | |
| 31 | + $image = current($images); | |
| 31 | 32 | echo ArtboxImageHelper::getImage($image->getImageUrl(), 'article_list'); |
| 32 | 33 | } else { |
| 33 | 34 | echo ArtboxImageHelper::getImage('/storage/no-image', 'article_list'); | ... | ... |
frontend/views/search/_search_item.php
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @var Product $model | |
| 4 | - * @var mixed $key | |
| 5 | - * @var integer $index | |
| 3 | + * @var Product $model | |
| 4 | + * @var mixed $key | |
| 5 | + * @var integer $index | |
| 6 | 6 | * @var ListView $widget |
| 7 | 7 | */ |
| 8 | 8 | use common\components\artboximage\ArtboxImageHelper; |
| ... | ... | @@ -13,10 +13,11 @@ |
| 13 | 13 | |
| 14 | 14 | $main_image = ArtboxImageHelper::getImage('/storage/no-image.png', 'filter_image'); |
| 15 | 15 | if(!empty( $model->variants )) { |
| 16 | + $variants = $model->variants; | |
| 16 | 17 | /** |
| 17 | 18 | * @var ProductVariant $variant |
| 18 | 19 | */ |
| 19 | - $variant = current($model->variants); | |
| 20 | + $variant = current($variants); | |
| 20 | 21 | $main_image = ArtboxImageHelper::getImage($variant->getImageUrl(), 'filter_image'); |
| 21 | 22 | unset( $variant ); |
| 22 | 23 | } | ... | ... |