4rn0 / statamic-v2-image-optimizer

Statamic v2 Addon to optimizes new Asset and Glide images
https://statamic.com/marketplace/addons/imageoptimizer
4 stars 2 forks source link

mime_content_type(): Can only process string or stream arguments error #5

Closed madsem closed 5 years ago

madsem commented 5 years ago

It seems the addon breaks Statamic in several locations, all Glide images on the site are broken (src is empty), as well as the Statamic php please generate-presets command breaks. All with the same error though:

[ErrorException]                                                  
  mime_content_type(): Can only process string or stream arguments  

Not entirely sure if that is your addons fault though, or if that is a Statamic/Glide bug.

PS: System: PHP 7.2 Ubuntu 16.04 LTS Statamic 2.11.3

madsem commented 5 years ago

here is a stack trace when run through console, refreshing the web browser doesn't write a stack trace in Statamics log for some reason, only CLI.

Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'mime_content_ty...', '/Users/XXXXXXXXXXXX...', 97, Array)
#1 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/site/addons/ImageOptimizer/ImageOptimizer.php(97): mime_content_type(false)
#2 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/site/addons/ImageOptimizer/ImageOptimizer.php(82): Statamic\Addons\ImageOptimizer\ImageOptimizer->attemptOptimization(false)
#3 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/site/addons/ImageOptimizer/ImageOptimizerListener.php(125): Statamic\Addons\ImageOptimizer\ImageOptimizer->optimizePath(false)
#4 [internal function]: Statamic\Addons\ImageOptimizer\ImageOptimizerListener->handleGlide('/Users/XXXXXXXXXXXX...', Array)
#5 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(221): call_user_func_array(Array, Array)
#6 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(355): Illuminate\Events\Dispatcher->fire('glide.generated', Array, false)
#7 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/core/Imaging/ImageGenerator.php(139): event('glide.generated', Array)
#8 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/core/Imaging/ImageGenerator.php(113): Statamic\Imaging\ImageGenerator->generate('175x175bb.jpg')
#9 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/core/Imaging/PresetGenerator.php(43): Statamic\Imaging\ImageGenerator->generateByAsset(Object(Statamic\Assets\Asset), Array)
#10 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/core/Console/Commands/Assets/AssetsGeneratePresetsCommand.php(102): Statamic\Imaging\PresetGenerator->generate(Object(Statamic\Assets\Asset), 'taxonomy-full-w')
#11 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/core/Console/Commands/Assets/AssetsGeneratePresetsCommand.php(69): Statamic\Console\Commands\Assets\AssetsGeneratePresetsCommand->generatePresets(Array)
#12 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/core/Console/Commands/Assets/AssetsGeneratePresetsCommand.php(51): Statamic\Console\Commands\Assets\AssetsGeneratePresetsCommand->generateUserPresets()
#13 [internal function]: Statamic\Console\Commands\Assets\AssetsGeneratePresetsCommand->fire(Object(Statamic\Imaging\ImageGenerator))
#14 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#15 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/laravel/framework/src/Illuminate/Console/Command.php(150): Illuminate\Container\Container->call(Array)
#16 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/symfony/console/Command/Command.php(263): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/core/Console/EnhancesCommands.php(21): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/symfony/console/Application.php(853): Statamic\Console\Commands\AbstractCommand->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/symfony/console/Application.php(200): Symfony\Component\Console\Application->doRunCommand(Object(Statamic\Console\Commands\Assets\AssetsGeneratePresetsCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/symfony/console/Application.php(124): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/statamic/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 /Users/XXXXXXXXXXXXen/Business/Code/theproject.dev/please(48): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 {main}  
4rn0 commented 5 years ago

There was a small typo in the console command. I fixed in the new release!

madsem commented 5 years ago

Sorry, but this is still happening.

Tested v2.0.3

4rn0 commented 5 years ago

That's weird: it's working fine here ... Same error as before?

madsem commented 5 years ago

Yeah, I'm going through the code too atm, pretty sure it's because it checks $filetype = mime_content_type($path); without testing if it's a file, and sometimes $path seems to be bool. Trying to find the cause though, why it's bool, from where it's coming

madsem commented 5 years ago

Ok so there is some issue with paths that don't excist (or are built wrong). $path = realpath($path); realpath() returns false on failure, which is then passed to attemptOptimization() which then errors out.

But the root cause is because sometimes the paths do not exist for some reason

4rn0 commented 5 years ago

Any special setup on the asset containers? I'm basically just listening to the glide.generated event, which should receive the full path to the file ... https://docs.statamic.com/addons/events/glide-generated

I can add an extra check to see if the file exists, but I don't understand how this event could pass an invalid path at all.

madsem commented 5 years ago

Nothing special per se, custom set up though. Multiple asset containers where each asset container also has subfolders.

screen shot 2018-12-31 at 1 20 54 pm

Still reading through the code trying to find the cause

4rn0 commented 5 years ago

In your stacktrace the path is mentioned as '/Users/XXXXXXXXXXXX...' Did you edit that or did it just come out like that?

madsem commented 5 years ago

No that was just me to black out private details sorry :)

madsem commented 5 years ago

That looks wrong, I just dump the path inside optimizePath() before the realpath() and after, before it's:

"/Users/xxxxx/Business/Code/xxxx/statamic/../local/cache/glide/containers/apps/icons/175x175bb.jpg/3bae5ddd14dd4634d52c2e2929910a52.jpg"

After realpath: false

madsem commented 5 years ago

This path is passed to handleGlide() already though, so it comes from Statamic core as it seems. It's just a normal Statamic setup, default, not subfolder or outside the webroot. Just an exact copy, running ImageOptimizer and in progress of building out custom theme, the only adjustments were the asset folders

4rn0 commented 5 years ago

Thanks! That helps a lot! Looking at the paths and your screenshot I think the path should actually be:

/Users/xxxxx/Business/Code/xxxx/statamic/../local/cache/glide/containers/assets/apps/icons/175x175bb.jpg/3bae5ddd14dd4634d52c2e2929910a52.jpg

And after the realpath call:

/Users/xxxxx/Business/Code/xxxx/local/cache/glide/containers/assets/apps/icons/175x175bb.jpg/3bae5ddd14dd4634d52c2e2929910a52.jpg

Could you verify the structure in your local/cache/glide/containers folder?

4rn0 commented 5 years ago

Looking at your screenshot and your comment again I think you have setup multiple asset containers within the default container? assets/apps, assets/buckets, etc.

Still no luck reproducing this locally though ...

madsem commented 5 years ago

I accidentally deleted the contents of my /local/cache folder, and for some reason it won't regenerate, trying to so that I can verify the adjusted path...

And the folders are all different containers, not nested screen shot 2018-12-31 at 1 56 27 pm

madsem commented 5 years ago

All asset containers have yaml configs like this one:

title: Apps
path: assets/apps
url: /assets/apps
assets:
  screenshots/_2.png:
    focus: 47-6
  screenshots/unnamed.jpg:
    title: sdgdfgdf
    alt: fggfdgdfg
    focus: 51-38
    imageoptimizer:
      original_size: 116279
      current_size: 100282
  icons/unnamed-5.png:
    focus: 47-18
  screenshots/img_1157.png:
    focus: 15-6
    imageoptimizer:
      original_size: 3834103
      current_size: 781345
  icons/175x175bb.jpg:
    imageoptimizer:
      original_size: 5744
      current_size: 4825
  icons/shareit-connect-transfer-logo-fit-wx-150.png:
    imageoptimizer:
      original_size: 9180
      current_size: 9168
  screenshots/img_1157-1545637128.png:
    imageoptimizer:
      original_size: 3834103
      current_size: 781345
  screenshots/img_1159-1545637130.png:
    imageoptimizer:
      original_size: 2892704
      current_size: 613182
  screenshots/img_1159.png:
    imageoptimizer:
      original_size: 2892704
      current_size: 613182

here is also my assets.yaml incl presets:

image_manipulation_route: /img
image_manipulation_secure: true
auto_crop: true
image_manipulation_driver: gd
image_manipulation_cached: true
image_manipulation_cached_path: img
image_manipulation_presets:
  taxonomy-full-w:
    w: 1200
    h: 295
    q: 75
    fit: crop
    border: '20,3000,overlay'
    format: pjpg
  md-taxonomy-full-w:
    w: 700
    h: 180
    q: 75
    fit: crop
    border: '20,3000,overlay'
    format: pjpg
  sm-taxonomy-full-w:
    w: 350
    h: 150
    q: 75
    fit: crop
    border: '20,3000,overlay'
    format: pjpg
  mosaic:
    w: 290
    h: 290
    q: 75
    fit: crop
    border: '20,3000,overlay'
    format: pjpg
  listing-landscape:
    w: 360
    h: 220
    q: 75
    fit: crop
    format: pjpg
thumbnail_max_width: 6000
thumbnail_max_height: 6000
4rn0 commented 5 years ago

Thanks for the feedback. I'll try to copy your setup and do some more testing later. But first I need to get some preparations done for New Years Eve !! 🎆

madsem commented 5 years ago

Now I'm thinking also, why is it looking inside the local folder anyways? Shouldn't it look inside the /img folder ?

Yeah, see you on the other side! haha

madsem commented 5 years ago

Okay I believe this is a Statamic bug @jackmcdade, @jasonvarga (not sure if I can mention outside repos/users)

Anyways, when I switch serve cached assets directly to off, it works and I receive no error... screen shot 2018-12-31 at 2 30 45 pm

If I toggle it on, the error comes back.

Because, interestingly enough, the $path that Statamic passes to the handle() method on the glide.generated event, doesn't change... It always looks inside the local directory, even though when serving cached versions directly it should look in the img folder (or whichever one is set), right?

That way this addon can generate assets, the CLI still works too

PS: Have a great new years eve y'all 🎆

PSPS: Could we get a Statamic 2.11.4 with this fixed asap'ish? :)

4rn0 commented 5 years ago

Since I can't change the way Statamic handles this, I added a file_exists check to the path to make sure there are no errors. You should submit your findings to the Statamic issue tracker: https://github.com/statamic/v2-hub/issues/new?template=Bug_Report.md

If the gents fix this, than I can have another look to make sure that both scenarios work as intended!