PHP链接到根文件夹之外的图像[a href,而不是img src]

发布于 2024-11-06 19:32:17 字数 566 浏览 6 评论 0原文

这是我的情况:图像存储在根文件夹之外。我可以使用外部 php 文件(应该如此)访问它们,然后 file_get_contents 然后 echo 然后 img src 它然后它会完美显示。

我安装了厚盒,我想要发生的是当用户单击图像时,它将在放大的厚盒中显示

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

我正在尝试在单击时创建一个图库。发生的情况是厚盒显示但图像没有显示。乱码/垃圾代码不是显示图像,而是带有带问号的黑色菱形。我想这是图像的原始代码。

我如何将其输出为图像而不是

<a href> 's

添加中的原始代码:我只是多玩了一下。当我删除thickbox类时,a href实际上起作用了。它通常在下一页上显示图像。不幸的是,当我附加厚盒类时,它显示了厚盒,但它显示了原始代码

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 image normally on the next page. Unforunately when I attach the thickbox classes, it shows the thickbox, but it shows the raw code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嗫嚅 2024-11-13 19:32:17

尝试:

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

如果您将原始图像数据视为文本,则您的浏览器可能无法将输出识别为图像。您可以使用上面的标题行来表示正确的 mime 类型。

也有可能发生错误并且错误消息使图像文件不可读(因为流以错误消息开头而不是标头)。在这种情况下,您应该在字符之前看到错误消息。

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.

私野 2024-11-13 19:32:17

在您的示例中,您直接使用 jpg 文件,它应该类似于:

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

即读取隐藏文件夹并返回图像的 php 页面。

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.

盛夏已如深秋| 2024-11-13 19:32:17

Thickbox 识别由文件扩展名链接的资源类型,以确定它是否是要显示的图像。在确定扩展名时,它还会忽略查询字符串。因此,您有以下选择:

  • 使用一些自定义 URL 重写并使您的脚本正常工作,例如。使用像 my_image_processing/img.jpg 这样的 URL 而不是 my_image_processing.php?img=img.jpg (搜索有关“mod_rewrite”的更多信息),
  • 重写 ThickBox 的代码(或搜索选项参数,允许您在不更改代码的情况下更改识别机制 - 我没有找到任何),或
  • 实现其他库,允许您实现您需要的内容(并允许您以您想要的方式使用外部脚本) ,

一件事是让 Thickbox 认为它加载图像,而不是加载到框架中的页面,第二件事是 - 正如 vbence 指出的 - 将正确的 mime 类型添加到您随脚本返回的图像中:您应该添加带有“Content-Type”信息的适当标头。

编辑:

负责显示图像的代码开始如下[原文如此!]:

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

如果您选择自己修改脚本,您可能希望添加另一种类型('php'),但您应该注意的后果。

EDIT2:

如果您选择“更改插件”选项,还有一些替代方案(ColorBox),这使您无需深入代码即可决定 *.php 链接是否应被视为图像。只需在文档中查找photo选项(当设置photo= true 应该如您所期望的那样运行)。

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).

水染的天色ゝ 2024-11-13 19:32:17

您可能忽略了用于显示图像的 http 标头。

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

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

header("Content-type: image/jpeg");
header("Content-Disposition: attachment;"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文