drh19790711 2015-03-12 11:41
浏览 43

获取使用GD Lib PHP创建的图像的路径

This should be so simple but I'm a bit stumped as to the best way to approach this.

I am building a PHP plugin so I can't hard code any paths. I also need to consider http protocols (http / https). I use GD Lib to create and save an image. My directory structure looks like this:

 public_html > php_plugin > php_classes > php_class.php
 public_html > php_plugin > saved_images

The plugin is installed in public_html. I run code to save the image in php_class.php. Here I save the image like so: '../saved_images/imagename.jpg'. This works fine and the image is saved in the correct directory.

I then return this path via AJAX to display the image. And, obviously this path fails to resolve and I get a broken image link: http://www.website.com/saved_images/imagename.jpg

It should look like: http://www.website.com/php_plugin/saved_images/imagename.jpg

So in my PHP class where I set up the save path how do I build up a path that I can return with AJAX, bearing in mind this is a plugin and could be used on many different sites.

EDIT:

For the record I know I can use $_SERVER['SERVER_NAME'] and $_SERVER['SERVER_PROTOCOL'] but based on these SO questions (PHP $_SERVER['HTTP_HOST'] vs. $_SERVER['SERVER_NAME'], am I understanding the man pages correctly? and PHP Get Site URL Protocol - http vs https) I think this approach will be quite unreliable as the server configs where the plugin will be hosted will be an unknown.

  • 写回答

1条回答 默认 最新

  • dqyl2374 2015-03-13 13:28
    关注

    You are conflating two different types of path; the server filesystem path and the URL path. In your case they are similar, but depending on the server configuration these might be wildly different.

    Since you write that you return a path to the image via AJAX, presumably your AJAX code must know the URL to the PHP script to be able to call it (something like http://example.com/php_plugin/index.php?). In this case you could just have PHP return only the filename and provide the rest of the path in your javascript.

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题