0

I installed the Gregwar/Image package ( https://github.com/Gregwar/Image ) to laravel, I changed the caching directory to $cacheDir = '/public/cache/images/';, I can generate cached image in php artisan tinker, but when I use it in blade template or controller I get the http://localhost/testsite/public/cache/images//f/a/l/l/b/fallback.jpg error url instead, I have narrowed down the problem to: Gregwar\Image\Adapter\GD and this function:

    protected function openJpeg($file)
    {
        if (file_exists($file) && filesize($file)) {
            $this->resource = @imagecreatefromjpeg($file);
        } else {
            $this->resource = false;
        }
    }

code used in blade:

<div class="faceted_objects thumbnail_container" data-url="/pictures/album/test_324716/sorted/new/page/1/">

    <?php

    use Gregwar\Image\Image;

    echo Image::open('public/storage/uploads/images/picture/1/bacon-broccoli-egg-bites21.jpg', 'transparent', 'center', 'center')
        ->zoomCrop(100, 100)
        ->jpeg();

    ?>

    {{--{{ gregwar_image( 100, 100, 'zoomCrop', 'picture/1/bacon-broccoli-egg-bites21.jpg' ) }}--}}

    {!! $image->getOtherImages() !!}

</div>

the code inside if (file_exists($file) && filesize($file)) { never runs and filesize() returns NOTHING using print_r or dd(), I have no idea how to fix this, the weird thing is it works in php artisan tinker but NOT on the actual site

works in <code>php artisan tinker</code> so why does not work in blade template?

1 Answers1

1

You need to use the real path to avoid any issues. Use realpath($imagePath).

When you access your project from CLI, the path of execution is your project root folder.

When you access it from the WEB (fpm), the path of execution is {project}/public folder, the one declared in the vhost configuration.

N69S
  • 16,110
  • 3
  • 22
  • 36
  • I used the path `'public/storage/uploads/images/picture/1/bacon-broccoli-egg-bites21.jpg'` but it still returns `cache/images/f/a/l/l/b/fallback.jpg` –  Aug 14 '19 at 09:09
  • @yeln try to dump `$file` right before `filesize($file)` and post the result – N69S Aug 14 '19 at 09:17
  • `var_dump($file)` gives `string(70) "public/storage/uploads/images/picture/1/bacon-broccoli-egg-bites21.jpg"` –  Aug 14 '19 at 09:23
  • remove the `public/` at the start of the image path CLI – N69S Aug 14 '19 at 09:28
  • I see, it seems to be working now without `public/`, why is `public/` the problem though? –  Aug 14 '19 at 09:35