Intervention / image

PHP Image Processing
https://image.intervention.io
MIT License
13.91k stars 1.49k forks source link

Problem with multiple database connections #1057

Closed TataMata closed 3 years ago

TataMata commented 3 years ago

I have a strange problem if I add config/database.php file to my Lumen installation. Even if I don't use any database connection my service returns an invalid image. I have route:

$router->get('photo', function () use ($router) {
  // create a new empty image resource
  Image::configure(array('driver' => 'imagick'));
  $img = Image::canvas(200, 200, '#ff0000');
  return $img->response('png');
});

in my basic lumen installation and everything works as expected. When I try to add config/database.php file to tailor my database connections (I need to connect to 2 databases at once), even if it is an empty file or copy of vendor/laravel/lumen-framework/config/database.php my route return an invalid base64 encoded image and I can't display it anymore.

It is easy reproducable. Just create new lumen project, require intervention/image and register ImageServiceProvider in the bootstrap/app.php and add route.

I'm using php 7.4 with Apache on Arch Linux (updated). My composer.json:

{
    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^7.3|^8.0",
        "intervention/image": "^2.5",
        "laravel/lumen-framework": "^8.0"
    },
    "require-dev": {
        "fzaninotto/faker": "^1.9.1",
        "mockery/mockery": "^1.3.1",
        "phpunit/phpunit": "^9.3"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ]
    }
}
mfn commented 3 years ago

returns an invalid image.

Any errors? Exceptions? Anything in the logfile?

TataMata commented 3 years ago

Nothing at all. I'm, using Firefox's extension RESTClient to test my services and, without database.php file it displays the image as it should, but just with the

<?php
?>

code in the config/database.php file, response changes from: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQMAAACXljzdAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABlBMVEX/AAD///9BHTQRAAAAAWJLR0QB/wIt3gAAABxJREFUWMPtwYEAAAAAw6D5U1/hAFUBAAAAAHwGFFAAAQCfIxUAAAAASUVORK5CYII= to: data:image/png;base64,DQqJUE5HDQoaCgAAAA1JSERSAAAAyAAAAMgBAwAAAJeWPN0AAAAEZ0FNQQAAsY8L/GEFAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAGUExURf8AAP///0EdNBEAAAABYktHRAH/Ai3eAAAAHElEQVRYw+3BgQAAAADDoPlTX+EAVQEAAAAAfAYUUAABAJ8jFQAAAABJRU5ErkJggg== Very strange.

TataMata commented 3 years ago

Here is my bootstrap/app.php file content just in case:

<?php

require_once __DIR__.'/../vendor/autoload.php';

(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();

date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);

$app->withFacades();

$app->withEloquent();

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

/*
|--------------------------------------------------------------------------
| Register Config Files
|--------------------------------------------------------------------------
|
| Now we will register the "app" configuration file. If the file exists in
| your configuration directory it will be loaded; otherwise, we'll load
| the default version. You may register other files below as needed.
|
*/

$app->configure('app');
$app->configure('database');

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

// $app->middleware([
//     App\Http\Middleware\ExampleMiddleware::class
// ]);

// $app->routeMiddleware([
//     'auth' => App\Http\Middleware\Authenticate::class,
// ]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
$app->register(Intervention\Image\ImageServiceProvider::class);

if (!class_exists('Image')) {
    class_alias('Intervention\Image\Facades\Image', 'Image');
}

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});

return $app;
TataMata commented 3 years ago

I'm sorry, I just accidentally closed it.

TataMata commented 3 years ago

I have just discovered that if I remove first blank line from the config/database.php file everything works fine. I have no idea why this is happening and I find it very odd. I have lost few days trying to figure it out. I'm closing this thread.