zxbodya / yii2-gallery-manager

93 stars 61 forks source link

imagejpeg(/1/4/original.jpg): failed to open stream: Is a directory #63

Open amismailz opened 5 years ago

amismailz commented 5 years ago

I am facing a strange behaviour of the extension! original.jpg is being created as a directory and I got the error below when trying to upload

screenshot from 2018-11-18 11-01-47

davarresc commented 5 years ago

The GalleryBehavior class has been refactorized and createFolders method has a bug. Due to the normalizePath method, the createDirectory method understands the file name as a folder and create the folders with the 'file/to/path/image.jpg' path instead of 'file/to/path'.

If you override createFolders method from GalleryBehavior class, it works fine. For example:

class GalleryBehavior extends \zxbodya\yii2\galleryManager\GalleryBehavior
{

    private function createFolders($filePath)
    {
        return FileHelper::createDirectory(FileHelper::normalizePath(dirname($filePath)), 0777);
    }

    /**
     * Replace existing image by specified file
     *
     * @param $imageId
     * @param $path
     */
    public function replaceImage($imageId, $path)
    {
        $this->createFolders($this->getFilePath($imageId, 'original'));

        $originalImage = Image::getImagine()->open($path);
        //save image in original size

        //create image preview for gallery manager
        foreach ($this->versions as $version => $fn) {
            /** @var ImageInterface $image */

            $image = call_user_func($fn, $originalImage);
            if (is_array($image)) {
                list($image, $options) = $image;
            } else {
                $options = [];
            }

            $image
                ->save($this->getFilePath($imageId, $version), $options);
        }
    }
}
zxbodya commented 5 years ago

Hi, @davarresc thanks for checking this - indeed there was PR merged recently, changing this method #62

can you create pr with your fix?

davarresc commented 5 years ago

Hi, Yes of course, I just did the PR. Regards.

zxbodya commented 5 years ago

thanks, merged it 👍 @amismailz can you check is it fixed you issue?