Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matlok's avatar
Level 3

App feature works when served with "php artisan serve" but not with Herd

I'm trying to integrate Spatie's larval-image-optimizer package into an existing project so when I upload an image through Nova it is optimized. To test the package I spun up a barebones project using this example, then added Nova to it and used the ImageOptimizer Facade. My example project works correctly (image file size reduced >50%) ONLY IF it is served using php artisan serve. If I serve the example project with Herd (my usual dev server), the file merely uploads and no optimization is performed. Since it works correctly using serve I'm inclined to think it's an environment or server issue and not a code issue. Both Herd's php version and my system PHP version are the same. I'm simply not sure where to go from here.

I'm a non-professional neophyte so thanks in advance for any help or insight.

0 likes
3 replies
LaryAI's avatar
Level 58

It sounds like the issue you're encountering is related to the environment configuration differences between php artisan serve and Herd. Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Herd's Configuration: Ensure that Herd is configured correctly and has all the necessary extensions and permissions to run the spatie/laravel-image-optimizer package. Sometimes, different servers have different configurations which can lead to such issues.

  2. Check for Required Binaries: The spatie/laravel-image-optimizer package relies on external binaries like jpegoptim, optipng, pngquant, etc. Make sure these binaries are installed and accessible in the environment where Herd is running.

  3. Log Errors: Add logging to your image optimization process to capture any errors or issues that might be occurring when using Herd. This can help you pinpoint the problem.

    use Spatie\ImageOptimizer\OptimizerChainFactory;
    use Illuminate\Support\Facades\Log;
    
    public function optimizeImage($path)
    {
        try {
            $optimizerChain = OptimizerChainFactory::create();
            $optimizerChain->optimize($path);
            Log::info('Image optimized successfully: ' . $path);
        } catch (\Exception $e) {
            Log::error('Image optimization failed: ' . $e->getMessage());
        }
    }
    
  4. Compare PHP Configurations: Compare the PHP configurations (phpinfo()) between php artisan serve and Herd. Look for differences in settings, extensions, and environment variables that might affect the image optimization process.

  5. Check File Permissions: Ensure that the directories and files involved in the upload and optimization process have the correct permissions. Sometimes, different servers handle file permissions differently.

  6. Environment Variables: Ensure that any environment variables required by the spatie/laravel-image-optimizer package are set correctly in Herd's environment.

  7. Run Optimization Manually: Try running the optimization commands manually in the Herd environment to see if they work outside of the Laravel context. This can help you determine if the issue is with the server configuration or the Laravel integration.

    jpegoptim /path/to/image.jpg
    optipng /path/to/image.png
    
  8. Check Herd Logs: Review Herd's logs for any errors or warnings that might provide more insight into why the optimization is not working.

By following these steps, you should be able to identify the root cause of the issue and resolve it. If you still encounter problems, consider reaching out to the Herd community or the maintainers of the spatie/laravel-image-optimizer package for further assistance.

Snapey's avatar

perhaps you are missing one of the image manipulation extensions?

matlok's avatar
Level 3

@Snapey Thanks for the quick reply! Both Optipng and Pngquant are working directly from the command line.

Code:

	Log::info('Image size before: ' . $request->photo->getSize() . ' bytes');
	
    try {
		$request->merge(['photo' => ImageOptimizer::optimize($request->photo)]);
    } catch (\Exception $e) {
	    Log::error('Image optimization failed: ' . $e->getMessage());
    }
	
	Log::info('Image size after: ' . $request->photo->getSize() . ' bytes');

844 KB "test.png" uploaded...

Log when served with php artisan serve :

	[2024-06-24 12:29:31] local.INFO: Image size before: 844107 bytes  
	[2024-06-24 12:29:34] local.INFO: Image size after: 442045 bytes 

Log when served with Herd:

	[2024-06-24 12:31:25] local.INFO: Image size before: 844107 bytes  
	[2024-06-24 12:31:25] local.INFO: Image size after: 844107 bytes

Maybe it's a permission issue with the optimizers?

Please or to participate in this conversation.