CodeIgniter Forums
Loading an image into a View. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Loading an image into a View. (/showthread.php?tid=84752)



Loading an image into a View. - davecoventry - 11-06-2022

I have some image files in ROOTPATH."writable/users/ which I am trying to display in the user's browser.
My Routes.php contains the line:$routes->get('/loadthumbs/(:any)', 'ThumbController::loadthumbimage/$1');
PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;

class 
CrudController extends BaseController {
 public function 
loadthumbimage($img){
    $uri service('uri');
 
$segments=$uri->getSegments();
    $previewpath=ROOTPATH."writable/users/".$segments[1]."/".$segments[2];
    $header="Content-Type: image/".substr($segments[2],-3);
    header($header);
        header("Content-Length: " filesize($previewpath));
    $fp fopen($previewpath'rb');
    fpassthru($fp);
    exit;
    }
}
?>
 In this instance, the image is a .BMP file, so the header is $header="Content-Type: image/bmp";
The image file is fine and opens locally with no problem, but it doesn't display in the bowser.
Instead I get a message 
Code:
The image "https://example.com/loadthumbs/8/1.bmp" cannot be displayed because it contains errors.
If I google this error, I get advice about making sure not to have a whitespace between the '<?php' marker at the start of the file and the header directives, but clearly this is not possible here.
Can anyone suggest a solution?


RE: Loading an image into a View. - davecoventry - 11-11-2022

See my post under the "Best Practices" subforum.