【问题标题】:magento 2.1 - How to get the full path of the cached image thumbnails in REST or from external scriptmagento 2.1 - 如何在 REST 或外部脚本中获取缓存图像缩略图的完整路径
【发布时间】:2017-05-03 18:00:02
【问题描述】:

magento 2.1 - 如何在 REST API 调用中获取缓存图像缩略图的完整路径

我知道图片在 /pub/media/catalog/product/

我需要知道如何获取缓存版本:/pub/media/catalog/product/cache/thumbnail/140x140/beff4985b56e3afdbeabfc89641a4582/

使用对象管理器有什么想法吗?

【问题讨论】:

    标签: image thumbnails magento2


    【解决方案1】:

    解决了,谁在找同样的问题。以下代码从加载 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);
    

    使用$appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);防止得到http://<MAGENTO_ROOT>/pub/static/version1493842505/frontend/_view/en_US/Magento_Catalog/images/product/placeholder/.jpg

    【讨论】:

    • 您好,感谢您的回答,它在 magento2 根文件夹中运行良好,如何在 vendor/magento/module-catalog/Model/Product/Gallery/Entry.php 文件中使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 2013-01-01
    • 1970-01-01
    相关资源
    最近更新 更多