From 16334d959a3977d496b397ee3508e205ee93938d Mon Sep 17 00:00:00 2001 From: Nicolas BEJEAN <nicolas.bejean@api-and-you.com> Date: Sun, 2 Dec 2018 14:44:46 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20traitement=20de=20la=20hauteur=20e?= =?UTF-8?q?t=20largeur=20des=20images=20affich=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Block/Widget/CategoryWidget.php | 73 ++++++++++++++++++- etc/widget.xml | 14 +++- i18n/en_US.csv | 8 +- i18n/fr_FR.csv | 4 + .../templates/widget/category_widget.phtml | 19 ++--- .../category_widget_img_background.phtml | 16 ++-- 6 files changed, 107 insertions(+), 27 deletions(-) diff --git a/Block/Widget/CategoryWidget.php b/Block/Widget/CategoryWidget.php index f4cfb0b..ed905f9 100755 --- a/Block/Widget/CategoryWidget.php +++ b/Block/Widget/CategoryWidget.php @@ -40,18 +40,22 @@ class CategoryWidget extends \Magento\Framework\View\Element\Template implements * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory * @param \Magento\Theme\Block\Html\Topmenu $topMenu + * @param \Magento\Framework\Image\AdapterFactory $imageFactory */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Helper\Category $categoryHelper, \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState, \Magento\Catalog\Model\CategoryFactory $categoryFactory, - \Magento\Theme\Block\Html\Topmenu $topMenu + \Magento\Theme\Block\Html\Topmenu $topMenu, + \Magento\Framework\Image\AdapterFactory $imageFactory ) { $this->_categoryHelper = $categoryHelper; $this->categoryFlatConfig = $categoryFlatState; $this->_categoryFactory = $categoryFactory; $this->topMenu = $topMenu; + $this->_imageFactory = $imageFactory; + parent::__construct($context); } @@ -118,4 +122,71 @@ class CategoryWidget extends \Magento\Framework\View\Element\Template implements { return $this->getData('template'); } + + /** + * Récupère la largeur de l'image du widget + */ + public function getCategoryImageWidth() + { + return $this->getData('category_image_width'); + } + + /** + * Récupère la hauteur de l'image du widget + */ + public function getCategoryImageHeight() + { + return $this->getData('category_image_height'); + } + + /** + * Permet d'optimiser les images de la catégorie + */ + public function getResizeImage($image, $width = null, $height = null) + { + if ($image) : + if (is_string($image)) : + $isRelativeUrl = substr($image, 0, 1) === '/'; + if ($isRelativeUrl) : + $image = ltrim($image, '/media/'); + else : + $image = 'catalog/category/'.$image; + endif; + endif; + endif; + + if(is_null($width)) : + $width = $this->getCategoryImageWidth(); + endif; + + if(is_null($height)): + $height = $this->getCategoryImageHeight(); + endif; + + $absolutePath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('').$image; + + $imageResized = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image; + + /** + * Créé imageFactory + */ + $imageResize = $this->_imageFactory->create(); + $imageResize->open($absolutePath); + $imageResize->constrainOnly(TRUE); + $imageResize->keepTransparency(TRUE); + $imageResize->keepFrame(FALSE); + $imageResize->keepAspectRatio(TRUE); + + $imageResize->resize($width,$height); + + /** + * Enregistre l'image dans le dossier de destination + */ + $destination = $imageResized ; + $imageResize->save($destination); + + $resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image; + + return $resizedURL; + } } \ No newline at end of file diff --git a/etc/widget.xml b/etc/widget.xml index c4203ac..bebb3b3 100755 --- a/etc/widget.xml +++ b/etc/widget.xml @@ -6,16 +6,24 @@ <description translate="true">widget_description</description> <parameters> <parameter name="widget_title" xsi:type="text" visible="true" sort_order="1"> - <label translate="true">title</label> + <label translate="true">widget_title</label> </parameter> <parameter name="widget_content" xsi:type="text" visible="true" sort_order="2"> - <label translate="true">content</label> + <label translate="true">widget_content</label> </parameter> <parameter name="category_selection" xsi:type="multiselect" visible="true" sort_order="3" source_model="NicolasBejean\CategoryWidget\Model\Config\Source\CategoryList"> <label translate="true">category_selection</label> <description translate="true">category_selection_description</description> </parameter> - <parameter name="template" xsi:type="select" required="true" visible="true"> + <parameter name="category_image_width" xsi:type="text" visible="true" sort_order="4"> + <label translate="true">category_image_width</label> + <description translate="true">category_image_width_description</description> + </parameter> + <parameter name="category_image_height" xsi:type="text" visible="true" sort_order="5"> + <label translate="true">category_image_height</label> + <description translate="true">category_image_height_description</description> + </parameter> + <parameter name="template" xsi:type="select" required="true" visible="true" sort_order="6"> <label translate="true">template</label> <options> <option name="default" value="widget/category_widget.phtml" selected="true"> diff --git a/i18n/en_US.csv b/i18n/en_US.csv index 35b0ab8..5c1609a 100755 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -1,10 +1,14 @@ -"widget_label", "Widget" +"widget_label", "Widget: Category Widget" "widget_description", "CategoryWidget Description" "widget_title", "Widget Title" "widget_content", "Widget Content" "category_selection", "Select categories" "category_selection_description", "Each categories must have an image" +"category_image_width", "Image width" +"category_image_width_description", "Numbers in px" +"category_image_height", "Image height" +"category_image_height_description", "Numbers in px" "template", "Template" "default_template", "Default Template" "other_template", "Template with backgrounded image" -"no_model", "WidgetCategory : Please define a model." \ No newline at end of file +"no_model", "WidgetCategory: Please define a model." \ No newline at end of file diff --git a/i18n/fr_FR.csv b/i18n/fr_FR.csv index 001d8a3..203e0ad 100755 --- a/i18n/fr_FR.csv +++ b/i18n/fr_FR.csv @@ -4,6 +4,10 @@ "content", "Contenu du widget" "category_selection", "Sélectionner les catégories" "category_selection_description", "Chaque catégorie doit avoir une image" +"category_image_width", "Largeur de l'image" +"category_image_width_description", "Taille en pixels" +"category_image_height", "Hauteur de l'image" +"category_image_height_description", "Taille en pixels" "template", "Modèle" "default_template", "Modèle par défaut" "other_template", "Modèle avec les images en arrière-plan du lien" diff --git a/view/frontend/templates/widget/category_widget.phtml b/view/frontend/templates/widget/category_widget.phtml index a6c40ab..4342adb 100755 --- a/view/frontend/templates/widget/category_widget.phtml +++ b/view/frontend/templates/widget/category_widget.phtml @@ -5,22 +5,18 @@ /** * @var $this \NicolasBejean\CategoryWidget\Block\Widget\CategoryWidget */ - $categories = $this->getCategorySelection(); - $categoryHelper = $this->getCategoryHelper(); +$categories = $this->getCategorySelection(); +$categoryHelper = $this->getCategoryHelper(); ?> <div class="widget block block-static-block category-widget"> <div class="block-content"> <?php if($this->getWidgetTitle() != null || $this->getWidgetContent() != null): ?> <div class="content-heading"> <?php if($this->getWidgetTitle() != null): ?> - <h2 class="title"> - <?php echo $this->getWidgetTitle(); ?> - </h2> + <h2 class="title"><?php echo $this->getWidgetTitle(); ?></h2> <?php endif; ?> <?php if($this->getWidgetContent() != null): ?> - <p class="content"> - <?php echo $this->getWidgetContent(); ?> - </p> + <p class="content"><?php echo $this->getWidgetContent(); ?></p> <?php endif; ?> </div> <?php endif; ?> @@ -29,11 +25,12 @@ <?php foreach($categories as $category): $cat = $this->getCategoryModel($category->getId()); + $image = $category->getData('image'); - if($cat->getImageUrl() != null): ?> + if ($image) : ?> <a class="category" href="<?php echo $cat->getUrl(); ?>"> - <img src="<?php echo $cat->getImageUrl(); ?>" class="img-responsive category_image" alt="<?php echo $cat->getName(); ?>" /> - <span><?php echo $cat->getName(); ?></span> + <span><img src="<?php echo $this->getResizeImage($image); ?>" class="img-responsive category_image" alt="<?php echo $cat->getName(); ?>" /></span> + <span class="wrapper"><span class="text"><?php echo $cat->getName(); ?></span></span> </a> <?php endif; ?> <?php endforeach; ?> diff --git a/view/frontend/templates/widget/category_widget_img_background.phtml b/view/frontend/templates/widget/category_widget_img_background.phtml index 5c18dd0..c628523 100755 --- a/view/frontend/templates/widget/category_widget_img_background.phtml +++ b/view/frontend/templates/widget/category_widget_img_background.phtml @@ -13,14 +13,10 @@ $categoryHelper = $this->getCategoryHelper(); <?php if($this->getWidgetTitle() != null || $this->getWidgetContent() != null): ?> <div class="content-heading"> <?php if($this->getWidgetTitle() != null): ?> - <h2 class="title"> - <?php echo $this->getWidgetTitle(); ?> - </h2> + <h2 class="title"><?php echo $this->getWidgetTitle(); ?></h2> <?php endif; ?> <?php if($this->getWidgetContent() != null): ?> - <p class="content"> - <?php echo $this->getWidgetContent(); ?> - </p> + <p class="content"><?php echo $this->getWidgetContent(); ?></p> <?php endif; ?> </div> <?php endif; ?> @@ -29,11 +25,11 @@ $categoryHelper = $this->getCategoryHelper(); <?php foreach($categories as $category): $cat = $this->getCategoryModel($category->getId()); - $image = $cat->getImageUrl(); + $image = $category->getData('image'); - if($image != null): ?> - <a class="category" href="<?php echo $cat->getUrl(); ?>" style="background-image: url('<?php echo $image; ?>')"> - <span><?php echo $cat->getName(); ?></span> + if ($image) : ?> + <a class="category" href="<?php echo $cat->getUrl(); ?>" style="background-image: url('<?php echo $this->getResizeImage($image); ?>');"> + <span class="wrapper"><span class="text"><?php echo $cat->getName(); ?></span></span> </a> <?php endif; ?> <?php endforeach; ?> -- GitLab