Skip to content

Commit

Permalink
Merge pull request Smile-SA#82 from Elastic-Suite/fix-esp170-visual-s…
Browse files Browse the repository at this point in the history
…earch

Fix esp170 visual search
  • Loading branch information
romainruaud authored Oct 7, 2021
2 parents f1f1fa9 + 183e39d commit bb9b3cf
Show file tree
Hide file tree
Showing 17 changed files with 477 additions and 100 deletions.
61 changes: 61 additions & 0 deletions src/module-elasticsuite-visual-search/Block/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteVisualSearch
* @author Pierre Gauthier <[email protected]>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/
namespace Smile\ElasticsuiteVisualSearch\Block;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Smile\ElasticsuiteVisualSearch\Model\SearchByImageManager;

/**
* Product visual search image preview block
*
* @category Smile
* @package Smile\ElasticsuiteVisualSearch
* @author Pierre Gauthier <[email protected]>
*/
class Image extends Template
{
/**
* @var SearchByImageManager
*/
protected $searchByImageManager;

/**
* Result constructor.
* @param Context $context Context.
* @param SearchByImageManager $searchByImageManager Search by image manager.
* @param array $data Data.
*/
public function __construct(
Context $context,
SearchByImageManager $searchByImageManager,
array $data = []
) {
parent::__construct($context, $data);
$this->searchByImageManager = $searchByImageManager;
}

/**
* Get search image url.
*
* @return string
* @throws NoSuchEntityException
*/
public function getSearchImageUrl(): string
{
return $this->searchByImageManager->getImageUrl();
}
}
33 changes: 30 additions & 3 deletions src/module-elasticsuite-visual-search/Block/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
*/
namespace Smile\ElasticsuiteVisualSearch\Block;

use Magento\Catalog\Block\Product\ListProduct;
use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
use Magento\CatalogSearch\Helper\Data;
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;
use Magento\Framework\View\Element\Template\Context;
use Magento\Search\Model\QueryFactory;
use Smile\ElasticsuiteVisualSearch\Model\SearchByImageManager;

/**
* Product visual search result block
Expand All @@ -30,12 +29,40 @@
*/
class Result extends \Magento\CatalogSearch\Block\Result
{
/**
* @var SearchByImageManager
*/
protected $searchByImageManager;

/**
* Result constructor.
* @param Context $context Context.
* @param LayerResolver $layerResolver Layer Resolver.
* @param Data $catalogSearchData Catalog search data.
* @param QueryFactory $queryFactory Query factory.
* @param SearchByImageManager $searchByImageManager Search by image manager.
* @param array $data Data.
*/
public function __construct(
Context $context,
LayerResolver $layerResolver,
Data $catalogSearchData,
QueryFactory $queryFactory,
SearchByImageManager $searchByImageManager,
array $data = []
) {
parent::__construct($context, $layerResolver, $catalogSearchData, $queryFactory, $data);
$this->searchByImageManager = $searchByImageManager;
}

/**
* {@inheritDoc}
*/
public function getSearchQueryText()
{
return __("Search results corresponding to your image");
$imageAnalysis = $this->searchByImageManager->search();

return __("Search results corresponding to %1", implode(', ', array_column($imageAnalysis, 'name')));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,6 @@
*/
class ByImage extends Action
{
/**
* @var UploaderFactory
*/
protected $uploaderFactory;

/**
* @var AdapterFactory
*/
protected $adapterFactory;

/**
* @var Filesystem
*/
protected $filesystem;

/**
* @var Config
*/
protected $config;

/**
* @var SearchByImageManager
*/
Expand All @@ -76,29 +56,17 @@ class ByImage extends Action
* Constructor.
*
* @param Context $context Controller action context.
* @param UploaderFactory $uploaderFactory Uploader factory.
* @param AdapterFactory $adapterFactory Image adapter factory.
* @param Filesystem $filesystem Filesystem.
* @param Config $config Config.
* @param SearchByImageManager $searchByImageManager Search by image manager.
* @param Resolver $layerResolver Catalog Layer Resolver.
* @param Session $session Session.
*/
public function __construct(
Context $context,
UploaderFactory $uploaderFactory,
AdapterFactory $adapterFactory,
Filesystem $filesystem,
Config $config,
SearchByImageManager $searchByImageManager,
Resolver $layerResolver,
Session $session
) {
parent::__construct($context);
$this->uploaderFactory = $uploaderFactory;
$this->adapterFactory = $adapterFactory;
$this->filesystem = $filesystem;
$this->config = $config;
$this->searchByImageManager = $searchByImageManager;
$this->layerResolver = $layerResolver;
$this->session = $session;
Expand All @@ -114,11 +82,6 @@ public function execute()
$result = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
try {
$imagePath = $this->session->getData('visual_search_image_path');
if (!empty($_FILES)) {
$imageData = $this->uploadImage();
$imagePath = $imageData['path'] . $imageData['file'];
}

if (!$imagePath) {
$this->messageManager->addErrorMessage(__('An error occurred while uploading your image'));

Expand All @@ -137,34 +100,4 @@ public function execute()

return $result;
}

/**
* Upload posted image.
*
* @return array
* @throws LocalizedException
* @throws Exception
*/
private function uploadImage(): array
{
$imageAdapter = $this->adapterFactory->create();

$uploaderFactory = $this->uploaderFactory->create(['fileId' => 'image']);
$uploaderFactory->setAllowedExtensions($this->config->getImagesAllowedExtensions());
$uploaderFactory->addValidateCallback('search', $imageAdapter, 'validateUploadFile');
$uploaderFactory->setAllowRenameFiles(true);
$uploaderFactory->setFilesDispersion(true);

$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$destinationPath = $mediaDirectory->getAbsolutePath('search');
$result = $uploaderFactory->save($destinationPath);

if (!$result) {
throw new LocalizedException(
__('File cannot be saved to path: $1', $destinationPath)
);
}

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteVisualSearch
* @author Pierre Gauthier <[email protected]>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/
namespace Smile\ElasticsuiteVisualSearch\Controller\Search;

use Magento\Catalog\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteVisualSearch\Model\ImageUploader;

/**
* Upload image.
*
* @category Smile
* @package Smile\ElasticsuiteVisualSearch
* @author Pierre Gauthier <[email protected]>
* @see https://qastack.fr/magento/211985/how-to-recreate-magento-2-backend-image-upload-in-frontend
*/
class UploadImage extends Action
{
/**
* @var ImageUploader
*/
protected $imageFileUploader;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @param Context $context Contexte.
* @param StoreManagerInterface $storeManager Store Manger.
* @param ImageUploader $imageFileUploader Image File Uploader.
* @param Session $session Session.
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
ImageUploader $imageFileUploader,
Session $session
) {
parent::__construct($context);
$this->storeManager = $storeManager;
$this->imageFileUploader = $imageFileUploader;
$this->session = $session;
}

/**
* Image upload action
*
* @return ResultInterface
*/
public function execute()
{
$result = $this->imageFileUploader->saveImageToMediaFolder('search-image');
$this->session->setData('visual_search_image_path', $result['file']);

return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
1 change: 1 addition & 0 deletions src/module-elasticsuite-visual-search/Model/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function recognize(string $imagePath): array
$imageResizer->keepAspectRatio(true);
$imageResizer->open($imagePath);
$imageResizer->resize($this->config->getImagesOptimalSize());
$imagePath = "{$pathParts['dirname']}/{$pathParts['filename']}.jpg";
$imageResizer->save($imagePath);

// Check file size.
Expand Down
Loading

0 comments on commit bb9b3cf

Please sign in to comment.