1

masgento 2.1 - How to get the full path of the cached image thumbnails in REST

i know that the images are in /pub/media/catalog/product/

i need to know how can i get the cached version: /pub/media/catalog/product/cache/thumbnail/140x140/beff4985b56e3afdbeabfc89641a4582/

any idea using object manager?

1 Answer 1

1

got it solved, for who is looking for the same. the following code runs from external script hat loads magento bootstrap :

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();

$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$appEmulation = $objectManager->get('\Magento\Store\Model\App\Emulation');
$imageFactory = $objectManager->get('\Magento\Catalog\Helper\ImageFactory');
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$storeId = $storeManager->getStore()->getId();

function getImageURL($pid, $attr=['width'=>500,'height'=>500,'watermarked'=>true,'imageId'=>'product_thumbnail_image'])
{
  global $imageFactory,$productRepository;
  $product = $productRepository->getById($pid);
  $image = $imageFactory->create()->init($product, $attr['imageId'])
      ->constrainOnly(true)
      ->keepAspectRatio(true)
      ->keepTransparency(true)
      ->keepFrame(true)
      ->resize($attr['width'], $attr['height']);
  return $image->getUrl();
}

$appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
$imageUrl = getImageURL($PRODUCT_ID_HERE);
$appEmulation->stopEnvironmentEmulation();

echo($imageUrl);

the use of $appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true); to prevent getting http://<MAGENTO_ROOT>/pub/static/version1493842505/frontend/_view/en_US/Magento_Catalog/images/product/placeholder/.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.