5

I am trying to show an image in a popup window using file_get_contents function and an echo statement.

Here is the code I have in the first view

<a href='javascript:window.open("index.php?option=com_jsmdownload&view=request&tmpl=component&layout=showimage&img=icon-36-ldpi.png", "Image View", "toolbar=no, location=no, width=400, height=400")'>Click</a>

Below is the code in my second layout inside the same view

$image = JURI::base().DS.'files'.DS.'images'.DS.'icon.png';
//echo $image;
$content = file_get_contents($image); 
header('Content-Type: image/png');
echo $content; exit();

When I click the 'Click' anchor in the first view it opens the popup window and it shows garbage inside.

enter image description here

The image path is correct and if I brows the image path in an another browser URL it shows the image.

Not sure what I am missing. Can someone please help on this?

5

I agree with Jeremy, you could simply use an <img> tag and insert the filename using PHP.

However, if for some reason you want to read the file using PHP, try using readfile(). In addition, you can use getimagesize() to get the correct mime type and insert it dynamically. That way you're not limited to using one file type:

$image = JURI::base().DS.'files'.DS.'images'.DS.'icon.png';
$imginfo = getimagesize($image);
header("Content-type: ".$imginfo['mime']);
readfile($image);

Hope that helps.

  • It still shows garbage characters on the screen. Updated my question with the output screenshot. Any advice on this? – Malaiselvan Nov 25 '14 at 19:42
  • Can you try the code in a standalone PHP file? If it works, then start adding it partially into your component, first without the popup, until you get a clue as to where it stops working. – johanpw Nov 25 '14 at 20:14
  • There was an empty line which causes the headache. It works fine after removing empty lines. Also exit is mandatory after readfile($image). – Malaiselvan Nov 25 '14 at 20:33
  • Great, I'm glad you solved it. And welcome to Joomla StackExchange! – johanpw Nov 25 '14 at 21:06
  • 2
    @Malaiselvan: I'd be interested in knowing a little more about the context that you're working in. I did a quick test with the code that johanpw wrote, and it works basically as written. The exit() is not something that should ever be needed or used in production code, as that stops all execution of scripts for the current user. If things aren't working for you unless you use exit(), then something else is wrong with the surrounding code that needs to be fixed. – Jeremy Proffitt Nov 26 '14 at 11:06
4

Setting the header and trying to process the image directly is really unnecessary. This will produce the result you're looking for:

Your second code block should be as follows

$input = JFactory::getApplication()->input;
$layout = $input->get('layout');
If (isset($layout) && $layout == 'show image') {
$image = JURI::base().'files/images/icon.png';
printf('<img src="%s" />', $images);

}

  • I understand the img tag will work. But due to some security reason I shouldn't go with that approach. That's the reason trying with get file contents. – Malaiselvan Nov 25 '14 at 12:40
  • Makes sense. The answer given by @johanpw will definitely work. – Jeremy Proffitt Nov 25 '14 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.