Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 16334d95 authored by Nicolas BEJEAN's avatar Nicolas BEJEAN
Browse files

Ajout du traitement de la hauteur et largeur des images affichées

parent 1f7e6648
No related branches found
No related tags found
No related merge requests found
...@@ -40,18 +40,22 @@ class CategoryWidget extends \Magento\Framework\View\Element\Template implements ...@@ -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\Indexer\Category\Flat\State $categoryFlatState
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
* @param \Magento\Theme\Block\Html\Topmenu $topMenu * @param \Magento\Theme\Block\Html\Topmenu $topMenu
* @param \Magento\Framework\Image\AdapterFactory $imageFactory
*/ */
public function __construct( public function __construct(
\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Helper\Category $categoryHelper, \Magento\Catalog\Helper\Category $categoryHelper,
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState, \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
\Magento\Catalog\Model\CategoryFactory $categoryFactory, \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->_categoryHelper = $categoryHelper;
$this->categoryFlatConfig = $categoryFlatState; $this->categoryFlatConfig = $categoryFlatState;
$this->_categoryFactory = $categoryFactory; $this->_categoryFactory = $categoryFactory;
$this->topMenu = $topMenu; $this->topMenu = $topMenu;
$this->_imageFactory = $imageFactory;
parent::__construct($context); parent::__construct($context);
} }
...@@ -118,4 +122,71 @@ class CategoryWidget extends \Magento\Framework\View\Element\Template implements ...@@ -118,4 +122,71 @@ class CategoryWidget extends \Magento\Framework\View\Element\Template implements
{ {
return $this->getData('template'); 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
...@@ -6,16 +6,24 @@ ...@@ -6,16 +6,24 @@
<description translate="true">widget_description</description> <description translate="true">widget_description</description>
<parameters> <parameters>
<parameter name="widget_title" xsi:type="text" visible="true" sort_order="1"> <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>
<parameter name="widget_content" xsi:type="text" visible="true" sort_order="2"> <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>
<parameter name="category_selection" xsi:type="multiselect" visible="true" sort_order="3" source_model="NicolasBejean\CategoryWidget\Model\Config\Source\CategoryList"> <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> <label translate="true">category_selection</label>
<description translate="true">category_selection_description</description> <description translate="true">category_selection_description</description>
</parameter> </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> <label translate="true">template</label>
<options> <options>
<option name="default" value="widget/category_widget.phtml" selected="true"> <option name="default" value="widget/category_widget.phtml" selected="true">
......
"widget_label", "Widget" "widget_label", "Widget: Category Widget"
"widget_description", "CategoryWidget Description" "widget_description", "CategoryWidget Description"
"widget_title", "Widget Title" "widget_title", "Widget Title"
"widget_content", "Widget Content" "widget_content", "Widget Content"
"category_selection", "Select categories" "category_selection", "Select categories"
"category_selection_description", "Each categories must have an image" "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" "template", "Template"
"default_template", "Default Template" "default_template", "Default Template"
"other_template", "Template with backgrounded image" "other_template", "Template with backgrounded image"
"no_model", "WidgetCategory : Please define a model." "no_model", "WidgetCategory: Please define a model."
\ No newline at end of file \ No newline at end of file
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
"content", "Contenu du widget" "content", "Contenu du widget"
"category_selection", "Sélectionner les catégories" "category_selection", "Sélectionner les catégories"
"category_selection_description", "Chaque catégorie doit avoir une image" "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" "template", "Modèle"
"default_template", "Modèle par défaut" "default_template", "Modèle par défaut"
"other_template", "Modèle avec les images en arrière-plan du lien" "other_template", "Modèle avec les images en arrière-plan du lien"
......
...@@ -5,22 +5,18 @@ ...@@ -5,22 +5,18 @@
/** /**
* @var $this \NicolasBejean\CategoryWidget\Block\Widget\CategoryWidget * @var $this \NicolasBejean\CategoryWidget\Block\Widget\CategoryWidget
*/ */
$categories = $this->getCategorySelection(); $categories = $this->getCategorySelection();
$categoryHelper = $this->getCategoryHelper(); $categoryHelper = $this->getCategoryHelper();
?> ?>
<div class="widget block block-static-block category-widget"> <div class="widget block block-static-block category-widget">
<div class="block-content"> <div class="block-content">
<?php if($this->getWidgetTitle() != null || $this->getWidgetContent() != null): ?> <?php if($this->getWidgetTitle() != null || $this->getWidgetContent() != null): ?>
<div class="content-heading"> <div class="content-heading">
<?php if($this->getWidgetTitle() != null): ?> <?php if($this->getWidgetTitle() != null): ?>
<h2 class="title"> <h2 class="title"><?php echo $this->getWidgetTitle(); ?></h2>
<?php echo $this->getWidgetTitle(); ?>
</h2>
<?php endif; ?> <?php endif; ?>
<?php if($this->getWidgetContent() != null): ?> <?php if($this->getWidgetContent() != null): ?>
<p class="content"> <p class="content"><?php echo $this->getWidgetContent(); ?></p>
<?php echo $this->getWidgetContent(); ?>
</p>
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php endif; ?> <?php endif; ?>
...@@ -29,11 +25,12 @@ ...@@ -29,11 +25,12 @@
<?php <?php
foreach($categories as $category): foreach($categories as $category):
$cat = $this->getCategoryModel($category->getId()); $cat = $this->getCategoryModel($category->getId());
$image = $category->getData('image');
if($cat->getImageUrl() != null): ?> if ($image) : ?>
<a class="category" href="<?php echo $cat->getUrl(); ?>"> <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><img src="<?php echo $this->getResizeImage($image); ?>" class="img-responsive category_image" alt="<?php echo $cat->getName(); ?>" /></span>
<span><?php echo $cat->getName(); ?></span> <span class="wrapper"><span class="text"><?php echo $cat->getName(); ?></span></span>
</a> </a>
<?php endif; ?> <?php endif; ?>
<?php endforeach; ?> <?php endforeach; ?>
......
...@@ -13,14 +13,10 @@ $categoryHelper = $this->getCategoryHelper(); ...@@ -13,14 +13,10 @@ $categoryHelper = $this->getCategoryHelper();
<?php if($this->getWidgetTitle() != null || $this->getWidgetContent() != null): ?> <?php if($this->getWidgetTitle() != null || $this->getWidgetContent() != null): ?>
<div class="content-heading"> <div class="content-heading">
<?php if($this->getWidgetTitle() != null): ?> <?php if($this->getWidgetTitle() != null): ?>
<h2 class="title"> <h2 class="title"><?php echo $this->getWidgetTitle(); ?></h2>
<?php echo $this->getWidgetTitle(); ?>
</h2>
<?php endif; ?> <?php endif; ?>
<?php if($this->getWidgetContent() != null): ?> <?php if($this->getWidgetContent() != null): ?>
<p class="content"> <p class="content"><?php echo $this->getWidgetContent(); ?></p>
<?php echo $this->getWidgetContent(); ?>
</p>
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php endif; ?> <?php endif; ?>
...@@ -29,11 +25,11 @@ $categoryHelper = $this->getCategoryHelper(); ...@@ -29,11 +25,11 @@ $categoryHelper = $this->getCategoryHelper();
<?php <?php
foreach($categories as $category): foreach($categories as $category):
$cat = $this->getCategoryModel($category->getId()); $cat = $this->getCategoryModel($category->getId());
$image = $cat->getImageUrl(); $image = $category->getData('image');
if($image != null): ?> if ($image) : ?>
<a class="category" href="<?php echo $cat->getUrl(); ?>" style="background-image: url('<?php echo $image; ?>')"> <a class="category" href="<?php echo $cat->getUrl(); ?>" style="background-image: url('<?php echo $this->getResizeImage($image); ?>');">
<span><?php echo $cat->getName(); ?></span> <span class="wrapper"><span class="text"><?php echo $cat->getName(); ?></span></span>
</a> </a>
<?php endif; ?> <?php endif; ?>
<?php endforeach; ?> <?php endforeach; ?>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment