diff --git a/Block/Widget/CategoryWidget.php b/Block/Widget/CategoryWidget.php index f4cfb0bfaa7e58a73d26746e28f54a9d15ef0863..ed905f939cba577875e4eb457e84b4c3c56f6b77 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 c4203ac00f4a9c0b15e4c24831a72c86beee29a6..bebb3b3f3ffef5553f0b401bf2b86b3043fc4071 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 35b0ab8920dd1725a5cb7d676de37c3826a7f73f..5c1609a86b8131abb5b9a66721a45d1cc2e8a32d 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 001d8a30f45cf25d748f71c59be153f618b6ec39..203e0adc1b8ad2dd61c4312d87c3e2ac832cdf5f 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 a6c40ab2896df3be6e08cdd0ffd7e14cdb2e2e46..4342adb3bc8909fe72169ff864e7b09b8f05d8b7 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 5c18dd0bdf94a862d632ccfa80d5d0bbcb36050c..c6285239e8af14a47be0f8bc87d2d5b2f8582c39 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; ?>