php getimagesize of dynamically created picture

眉间皱痕 提交于 2019-12-13 04:34:27

问题


I am trying to determine if image.php is valid picture file. It may be or may be not because if DB has information about resized picture it is going to be valid - image.php will resize original picture and print it to the screen. There is also alternative - there is no filename information in database and it can result error message or error picture. I'd like to get dimensions of image and if it is width = 0 and height = 0 then print error message. I was trying to do it with getimagesize but it produces error:

[function.getimagesize]: failed to open stream: No such file or directory in ...

Path is 100% correct. I've read that it is impossible to getimagesize of dynamically made picture via php. I always can check my database but i am curious if there is any hack to get image size that is dynamically created via php.

Code:

$file = "image.php?img=1";
$a = getimagesize($file);
echo 'aaaaaaaaa' . $a;

EDIT:

I did it another way because none of the solutions worked for me - i just check database for correct filename existance.

Thanks for help.


回答1:


You'd have to use a full-blown absolute url for PHP to realize you're requesting a url. image.php?img=1 is just a local file-system-only request

$file = 'http://example.com/image.php?img=1'; // full-blown HTTP request
$file = 'image.php'; // local-only request

But this is somewhat inefficient, since you're doing a full-blown http round-trip request. if that url is password/session protected, you'll have to "log in" first, etc...

If that image is coming from a local database, then you should recreate the fetching logic or make it usable via function call so that it doesn't do the HTTP request, and simply works "internally" within your script, and then pass the data to GD's imagecreatefromstring(), which'll accept any known image type, then use the imagesx() and imagesy() functions to get the height/width.




回答2:


You need file_get_contents since image.php?img=1 isn't a real file on your system

$cont = file_get_contents($file);
$r = imagecreatefromstring($cont);
imagesx($r);
imagerx($r);

Of course you need full URL: http://etc



来源:https://stackoverflow.com/questions/10971313/php-getimagesize-of-dynamically-created-picture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!