Welcome Guest, Not a member yet? Register   Sign In
Help with Output image
#1

[eluser]SoulM4n[/eluser]
Hi all,
i'm trying to create a controller in order to hide the path of the images so i can put in the code <img src="mycontroller/showpic/3">.

I'm unable to make it works and i'm going a little crazy trying to solve it so please help me if you can.

Here is the controller :

Code:
public function showpic()
    {
        $idpic = $this->uri->segment(3);
        $this->db->where('id',$idpic);
        foreach($this->db->get('account_gallery')->result() as $row)
        {
            $photo = $row->thumb;    
        }        
        $imagepath=base_url().'gallery/'.$this->session->userdata('nickname').'/thumbs/'.$photo;
                    
        $this->output->set_content_type('jpeg');
        $this->output->set_output(file_get_contents($imagepath));
    }

Can you please tell me what's wrong with this code ? i don't get any error .. just i don't get anything out.

(sorry for my poor english).

Marco
#2

[eluser]toopay[/eluser]
First, give proper validation to check if the file exist using file_exists(). Then you must set proper header (mime type) using output->set_header() before you send the response!
#3

[eluser]SoulM4n[/eluser]
Hi,
thanks for you repy.
I've added the file_exist check (the file really exist in the right path) and also set_header config line. Here is the code :

Code:
public function showpic()
    {
        $idpic = $this->uri->segment(3);
        $this->db->where('id',$idpic);
        foreach($this->db->get('account_gallery')->result() as $row)
        {
            $photo = $row->thumb;    
        }        
        $imagepath='gallery/'.$this->session->userdata('nickname').'/thumbs/'.$photo;
                    
        if (file_exists($imagepath))
        {
            $this->output->set_content_type('jpeg');
            $this->output->set_header('Content-Type: image/jpeg');
            $this->output->set_output(file_get_contents($imagepath));
        }    
    }

I still don't get anything in the output .. what can i do ?

Thanks,

Marco
#4

[eluser]toopay[/eluser]
Do you call it directly on the browser?
#5

[eluser]SoulM4n[/eluser]
I've tried directly on the browser and also in the page as :

Code:
<img src="http://localhost/test/usergallery/showpic/5">

but i can't get it working ..
#6

[eluser]toopay[/eluser]
Code:
// Try this
$imagepath = 'gallery/'.$this->session->userdata('nickname').'/thumbs/'.$photo;
$imagepath = $_SERVER["DOCUMENT_ROOT"].'/'.$imagepath;
// if youe need to see the $imagepath, uncomment below lines
// var_dump($imagepath); exit;
if (file_exists($imagepath))
{
   $this->output->set_content_type('jpeg');
   $this->output->set_header('Content-Type: image/jpeg');
   $this->output->set_output(file_get_contents($imagepath));
}  
else
{
   echo 'Invalid image path';
}
#7

[eluser]SoulM4n[/eluser]
OK tried,
image path seems to be ok because if i change the path i get the message "Invalid image path" .. otherwise i get no messages.

I've tried with your code but nothing Sad

I'm using Codeigniter 2.0 on a Windows 7 64bit OS .. don't know if it helps.
#8

[eluser]SoulM4n[/eluser]
wooops .. sorry i forgot to add a row of your code and indeed i get this error :

"Invalid image path" ... and the path i get is :

Code:
string(58) "C:\inetpub\wwwroot/gallery/nick/thumbs/_MAG0903_thumb.jpg"
#9

[eluser]toopay[/eluser]
modify this line
Code:
//$imagepath = $_SERVER["DOCUMENT_ROOT"].'/'.$imagepath;
$imagepath = str_replace('\','/',$_SERVER["DOCUMENT_ROOT"]).'/'.$imagepath;
#10

[eluser]SoulM4n[/eluser]
[quote author="toopay" date="1304708298"]modify this line
Code:
//$imagepath = $_SERVER["DOCUMENT_ROOT"].'/'.$imagepath;
$imagepath = str_replace('\','/',$_SERVER["DOCUMENT_ROOT"]).'/'.$imagepath;
[/quote]

that code doesn't work .. i had to change it with :

Code:
$imagepath = str_replace(chr(92),'/',$_SERVER["DOCUMENT_ROOT"]).'/'.$imagepath;

but i get the error "Invalid Image path" .. i sort out that i had to add the site dir in the relative path .. like this :

Code:
$imagepath = $_SERVER["DOCUMENT_ROOT"].'test/'.$imagepath;


Anyway .. it doesn't work the same Sad




Theme © iAndrew 2016 - Forum software by © MyBB