vlucas / bulletphp

A resource-oriented micro PHP framework
http://bulletphp.com
BSD 3-Clause "New" or "Revised" License
418 stars 50 forks source link

Set what format when echo image content in route #78

Closed tablecell closed 7 years ago

tablecell commented 7 years ago
    $app->path('/captcha',function($request) use($app){

    $this->get(function($request) use($app){

            $captcha=new  Captcha();

       header('Content-Type', 'image/jpeg');

            return $captcha->output_image();

    });
    });

but http header Content-Type still is "text/html; charset=UTF-8"

vlucas commented 7 years ago

You should return a new Response object:

$app->path('/captcha',function($request) use($app){

    $this->get(function($request) use($app){

            $captcha=new  Captcha();

               return $app->response(200);
                ->header('Content-Type', 'image/jpeg')
                ->content($captcha->output_image());

    });
});
tablecell commented 7 years ago

above code works fine with index.php $app->run(new Bullet\Request()); but output nothing with template route

at index.php

  $app->run(new Bullet\Request());

and

echo   $app->run(new Bullet\Request());

behavior different when template route and image route full code is :

<?php
require   '../vendor/autoload.php';
error_reporting(E_ALL ^E_NOTICE);
$config=array(

    'template' => array(
        'path' => __DIR__ . '/templates/',
        'path_layouts' => __DIR__ . '/templates/layout/',
        'auto_layout' => 'application'
    )
);

$app = new Bullet\App($config);
$app->path('/captcha',function($request) use($app){

    $this->get(function($request) use($app){

            $captcha=new Captcha();

               return $app->response(200)
                ->header('Content-Type', 'image/jpeg')
                ->content($captcha->output_image());
        // works fine  with "  $app->run(new Bullet\Request());" 
        // works wrong   with " echo  $app->run(new Bullet\Request());" 
    });
});
$app->path('/tpl', function($request) use($app) {
    $this->format('html', function() use($app, $page) {

                    return $app->template('hello', array('name' => 'World'));
            });
        // works fine  with "  echo   $app->run(new Bullet\Request());"     
               // works wrong   with "    $app->run(new Bullet\Request());"     
});

});

      $app->run(new Bullet\Request());  //  http://localhost/captcha and http://localhost/tpl   behavior different 
// echo   $app->run(new Bullet\Request());     try this look at  http://localhost/captcha and http://localhost/tpl

      exit;
netom commented 7 years ago

If output_image() does what it says, i.e. outputs and image, then there's your problem.

$app->run() merely returns a Response, and echoing/printing it first converts it into a string. If output_image prints the image, but returns null, and output bufering is on, the code above looks like it's working, but it really just creates an empty response (with the correct content type), the output buffer catches the image output and PHP takes care of the rest. By accident.

The Captcha class should return the image as a string.

In general, try to avoid any printing and leave it to the framework.