开发者

PHP Link to image outside root folder [a href, not img src]

Here's my situation: Images are stored outside root folder. I can access them with an external php file (as it should be) then file_get_contents then echo then img src it then it will show perfectly.

I have thickbox installed and what I want to happen is when a user clicks on the image, it will show in the thickbox enlarged

<a href="img.jpg" class="thickbox"><img src="my_image_processing.php?img=img.jpg" />

I'm trying to create a gallery upon clicking. What happens is the thickbox shows up but the image doesn't. Instead of the image, the gibberish/garbage codes show up with black diamonds with a questionmark. I suppose this is the raw code of the image.

How can I output that as image and not raw code in

<a href> 's

ADDED: I just played around with it a bit more. When I remove the thickbox class, the a href actually works. It displays the开发者_StackOverflow社区 image normally on the next page. Unforunately when I attach the thickbox classes, it shows the thickbox, but it shows the raw code


Try:

header ("Content-type: image/jpeg");

If you see the raw image data as text, probably your browser does not recognize the output as an image. You can signal the right mime type with the header line above.

It is also possible that an error occures and error messages make the image file unreadable (because instead of the headers the stream starts with an error message). In that case you should see the error message before the characters.


In your example you are using the jpg file directly, it should be something like:

<a href="my_image_processing.php?img=img.jpg" class="thickbox"><img src="my_image_processing.php?img=img.jpg" />

That is, the php page that read the hidden folder and return the image.


Thickbox recognizes the type of the resource linked by the file extension to determine, whether it is an image to be displayed, or not. It also ignores the query string when determining extension. Thus, you have the following options:

  • use some custom URL rewriting and make your script work eg. using URL like my_image_processing/img.jpg instead of my_image_processing.php?img=img.jpg (search for more info on "mod_rewrite"),
  • rewrite the code of ThickBox (or search for option parameters allowing you to change that recognition mechanism without changing the code - I did not find any), or
  • implement other library allowing you to implement what you need (and allows you to use the external script in a way you intended),

One thing is making Thickbox think it loads image, not a page into the frame, and the second one is - as vbence pointed - adding proper mime type to the image you return with your script: you should add appropriate header with "Content-Type" information.

EDIT:

The code responsible for displaying images begins as follows [sic!]:

var baseURL;
 if(url.indexOf("?")!==-1){ //ff there is a query string involved
  baseURL = url.substr(0, url.indexOf("?"));
 }else{ 
     baseURL = url;
 }

 var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
 var urlType = baseURL.toLowerCase().match(urlString);
if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images

You may wish to add another type ('php'), if you choose to modify the script yourself, but you should be aware of the consequences.

EDIT2:

If you go with the 'change plugin' option, there is some alternative (ColorBox), that makes you able to decide without the need to dig into the code, whether the *.php link should be treated as image. Just look for the photo option within the documentation (when set photo=true should behave as you would expect).


You've probably neglected the http headers for displaying an image.

header("Content-type: image/jpeg");
header("Content-Disposition: attachment;"); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜