0

I am aware that this is probably a very trivial problem which I just can't seem to get right although I have spent the last few hours crawling the web..

I have a file image_get.php which is supposed to display an image file "keepcalm.png" that is located in the same directory as the php file.

I am using a class to handle all the image related actions.

image_get.php

<?php
   include "classes/image.class.php"; 
   include "classes/database.class.php";
   $database_sso = new database('SSO'); //will be needed later and is dummy for now
   $testimage = new image($database_sso, "1"); //parameters are dummy for now
   $testimage->show_image();
?>

image.class.php

<?php

class image{
   private $database_image;     //Image Database containing all images
   private $imageid;            //ID of Image in Database
   private $filepath;           //Path to image file


//PUBLIC FUNCTIONS  
   public function __construct($database, $imageid){
     $this->database_image = $database;     //Right now this is just a placeholder
     $this->imageid = $imageid;         //Right now this is just a placeholder

   }

    public function show_image(){

    $remoteImage = $this->get_local_link_from_id($this->imageid);  // Right now this returns "keepcalm.png"

    header('Content-Type: image/x-png')
    $returnstring = readfile($remoteImage);

}
 //PRIVATE FUNCTIONS
private function get_local_link_from_id($imageid){

    $local_link = "keepcalm.png"; //dummy for now
    return $local_link;
}


}



?>

The output I get is complet gibberish as can be seen here -> http://niederrad.no-ip.org/portal/image_get.php

What am I missing? I have tried lots of iterations of the above and am completely clueless as to how I should proceed..

  • 2
    Your header with content-type is not sent to browser. This is function, not a string!! `header('Content-Type: image/x-png')`. Should be, at least, `$returnstring = readfile($remoteImage); header('Content-Type: image/x-png');` – Cheery Oct 30 '14 at 17:51
  • How would you send it? $returnstring = header('Content-Type: image/x-png').readfile($remoteImage); return $returnstring; that won't work... – Alexander Langanke Oct 30 '14 at 17:54
  • I wrote it! `$returnstring = readfile($remoteImage); header('Content-Type: image/x-png');` You should run the function and it will generate http response headers, not return it. – Cheery Oct 30 '14 at 17:54
  • Hm. If I do it in that order I get a "headers already sent" error. If I put the header('Content-Type: image/x-png'); first I get only an empty image icon.. see http://niederrad.no-ip.org/portal/image_get.php – Alexander Langanke Oct 30 '14 at 17:59
  • It means that your code is a mess, read this (there should be no output to the browser before the headers are sent) - http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php And are you going to return `$returnstring` from function??? If you do not return the content of the file or not send it to browser from within the function - it will not be shown. – Cheery Oct 30 '14 at 18:00

2 Answers2

0

Try this:

public function show_image() {

    // this returns "keepcalm.png"
    $remoteImage = $this->get_local_link_from_id($this->imageid);

    // Set Header
    header("Content-Type: image/png"); // More info: http://stackoverflow.com/a/2086406/2332336

    // Output the contents of image
    readfile($remoteImage);
    exit;
}

If this doesn't work for you, it means PHP is not able to locate the local image keepcalm.png Possibly because it doesn't exist here: http://niederrad.no-ip.org/portal/keepcalm.png (i.e. from the script (image_get.php) execution location http://niederrad.no-ip.org/portal/)

If this is the case, you might have to tell your readfile() command where the image is located on your server. By that I mean specify the absolute path to file (on linux server /home/USERNAME/public_html/images/ for example)

If the image is on a remote server (as your variable name suggests), then your code should look like this:

    // Output the contents of image
    exit(file_get_contents('http://remote-site.com/path/to/'. $remoteImage));

Assuming your PHP config has allow_url_fopen set to true.

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • Thank you for the answer. Unfortunately your suggestion is the first thing I initially tried and the file is here http://niederrad.no-ip.org/portal/keepcalm.png -> please try the URL. – Alexander Langanke Oct 30 '14 at 18:08
  • I have checked the file location with file_exists() and that is not the problem.. – Alexander Langanke Oct 30 '14 at 18:32
  • Alexander (below) makes a good point about `BOM` on the file. Have you tried opening the file in `Notepad++` save it using `Encoding -> Encode in ANSI` and then save the file and try again? – Latheesan Oct 30 '14 at 19:19
0

The Problem was with BOM which I find very strange as I was sure I had checked to not encode with BOM...

How to fix "Headers already sent" error in PHP

A very good suggestion from Cheery.

Community
  • 1
  • 1