如何通过PHP获取image/png内容
这是我在 Stack Overflow 上发表的第一篇文章,所以如果我使用了不正确的格式或约定,请原谅我。我正在尝试编写一个测试场景,该场景从网页 POST(多部分/表单数据)接收 png 图像,删除已发布的图像并将其作为内容类型转发给第三方:image/png。
我有一个 php 文件(catcher.php),它是转发图像的接收者。 Post.php,将上传的图像发布到 catcher.php 的 php 文件如下:
<?php
$img = imagecreatefrompng($_FILES['myfile']['tmp_name']);
imagepng($img);
$opts = array(
'http'=>array(
'method'=>"POST",
'content'=>$img,
'header'=>"Content-Type: image/png\r\n"
)
);
$context = stream_context_create($opts);
file_get_contents( "http://localhost/catcher.php", false, $context);
?>
Post.php 从将其发布为 multipart/form-data 的网页中很好地获取文件,但是我不确定如何访问图像/ catcher.php 中的 png 内容。
我的问题是,在catcher.php中,如何访问图像内容?我尝试过 $_POST['content'] 并收到以下错误:“未定义索引:内容”。所以我知道我只是没有寻找正确的数据。是否有一个特定的超级全局变量,例如 $_POST 或 $_REQUEST,我可以使用它来访问发布的图像内容,或者还有其他解决方案吗?
解决方案 我能够使用以下 catcher.php 代码找到我正在寻找的结果:
$input = fopen("php://input","+r");
$destination = fopen($target_path, 'w');
stream_copy_to_stream($input, $destination);
fclose($input);
fclose($destination);
?>
感谢马克和布伦登的及时回复!
This is my first post to Stack Overflow so please forgive me if I have used incorrect formatting or conventions. I am trying to write a test scenario which receives a png image from a webpage POST (multipart/form-data), strips out the image which was posted and forwards that on to a 3rd party as Content-Type: image/png.
I have a php file (catcher.php) which is the recipient of the forwarded image. Post.php, the php file that posts the uploaded image to catcher.php is below:
<?php
$img = imagecreatefrompng($_FILES['myfile']['tmp_name']);
imagepng($img);
$opts = array(
'http'=>array(
'method'=>"POST",
'content'=>$img,
'header'=>"Content-Type: image/png\r\n"
)
);
$context = stream_context_create($opts);
file_get_contents( "http://localhost/catcher.php", false, $context);
?>
Post.php gets the file just fine from the webpage that posts it as multipart/form-data, however I am unsure how to access the image/png content in catcher.php.
My question is, in catcher.php, how do I access the image content? I have tried $_POST['content'] and I obtain the following error: "Undefined index: content". So I know I am just not looking for the correct data. Is there a specific superglobal variable such as $_POST or $_REQUEST that I can use to access the posted image content, or is there some other solution?
RESOLUTION
I was able to find the result I was looking for with the following code for catcher.php:
$input = fopen("php://input","+r");
$destination = fopen($target_path, 'w');
stream_copy_to_stream($input, $destination);
fclose($input);
fclose($destination);
?>
Thank you both Marc and Brenden for your expedient responses!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
imagepng($img)
立即输出包含 PNG 的二进制垃圾。它没有被捕获到变量中。您实际发布的字符串可能是“GD Resource #7”或类似的内容。无论如何,整个 imagepng() 位都是无用的 - 您正在将 PNG 解码为内存中的表示形式,然后尝试再次重新编码为 PNG。由于文件已经在磁盘上,因此毫无意义地浪费内存。您可以通过以下方式完成整个操作:
请注意添加检查上传是否成功 - 假设上传成功会给您带来痛苦。
话虽如此,您也没有进行正确的 POST 文件上传。为此,您必须实际模拟一个完整的 HTML 表单,其中包含
和
enctype="multipart/form-data"< /code> 等等。您所做的就是将原始 PNG 发送到服务器。
您可以通过在捕手脚本中读取
php://input
来检索它。imagepng($img)
does an immediate output of the binary garbage comprising the PNG. it's not captured into a variable. What you're actually posting is a string that probably says "GD Resource #7" or something similar.The whole imagepng() bit is useless anyways - you're decoding a PNG into an in-memory representation, then trying to re-encode to PNG again. A pointless waste of memory, since the file is already on disk. You could do the whole thing with:
Note the addition of checking for upload success - assumingn an upload succeeded will cause you grief down the line.
That being said, you're also not doing a proper POST file upload. For that to work, you have to actually simulate a full-blown HTML form with
<input type="file">
present, andenctype="multipart/form-data"
and whatnot. All you're doing is sending a raw PNG to the server.You can probably retrieve it by reading from
php://input
in your catcher script.问题是您的数据没有编码为 multipart/form-data,而是只是一个PNG。
您有两个选择:
$_POST
或$_REQUEST
读取数据。post.php
不变,并从 读取原始数据catcher.php
中的 php://input。对于选项 1,看起来 HttpRequest 会为您处理表单编码。
The problem is that your data isn't encoded as multipart/form-data, it's just a PNG.
You have two options:
$_POST
or$_REQUEST
.post.php
how it is and read the raw data from php://input incatcher.php
.For option 1, it looks like HttpRequest handles form encoding for you.