Magento 2.2.5 Warning: getimagesize Failed to Open Stream – Fix

imagemagento2

I'm doing resize image, and there is an issue here:

1 exception(s):
Exception #0 (Exception): Warning: getimagesize(C:/xampp/htdocs/magento/pub/media/Aht_BannerSlider/images/slide_1.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\magento\vendor\magento\framework\Image\Adapter\AbstractAdapter.php on line 304

I've search a lot, and try delete var, delete gerenate, delete public/static ,clear cache, setup:upgrade, even static-content:deploy, but none solution work.
So here is the code:

<?php

namespace Aht\BannerSlider\Helper;

use Magento\Framework\App\Filesystem\DirectoryList;

class Image extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * Custom directory relative to the "media" folder
     */
    const DIRECTORY = 'Aht_BannerSlider/images';

    /**
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface
     */
    protected $_mediaDirectory;

    /**
     * @var \Magento\Framework\Image\Factory
     */
    protected $_imageFactory;

    protected $_storeManager;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Framework\Filesystem $filesystem
     * @param \Magento\Framework\Image\Factory $imageFactory
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Image\AdapterFactory $imageFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        $this->_imageFactory = $imageFactory;
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }

    /**
     * First check this file on FS
     *
     * @param string $filename
     * @return bool
     */
    protected function _fileExists($filename)
    {
        if ($this->_mediaDirectory->isFile($filename)) {
            return true;
        }
        return false;
    }

    /**
     * Resize image
     * @return string
     */
    public function resize($image, $width = null, $height = null)
    {
        $mediaFolder = self::DIRECTORY;

        $path = $mediaFolder . '/cache';

        $absolutePath = $this->_mediaDirectory->getAbsolutePath($mediaFolder) .'/'. $image;
        $imageResized = $this->_mediaDirectory->getAbsolutePath($path).'/'. $image;

        if (!$this->_fileExists($path . $image)) {
            $imageFactory = $this->_imageFactory->create();
            $imageFactory->open($absolutePath);
            $imageFactory->constrainOnly(true);
            $imageFactory->keepTransparency(true);
            $imageFactory->keepFrame(true);
            // background Color nhằm không hiển thị khoảng đen sau khi resize.
            $imageFactory->backgroundColor(array(255,255,255));
            $imageFactory->keepAspectRatio(true);
            $imageFactory->resize($width, $height);
            $imageFactory->save($imageResized);
        }

        return $this->_storeManager
                ->getStore()
                ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $path . $image;
    }
}

Best Answer

Oh, what silly me, Okay, so i was think that the web/images will be render to pub/media just like what code render to pub/static.
So i was looking for nothing all the time.

And the Solution is i need to put the images in the folder pub/media/[my module]/images

So sorry for wasted your time. :(

Related Topic