将图像作为 PHP POST 响应发送到浏览器。

huangapple go评论111阅读模式
英文:

Send image to browser as a PHP post response

问题

所以,我正在使用CURL进行POST请求:

  1. <?php
  2. $url = 'http://url/to/site';
  3. $data = array(
  4. "tk" => $_GET["tk"]
  5. );
  6. $ch = curl_init($url);
  7. // 设置请求选项
  8. curl_setopt($ch, CURLOPT_POST, 1);
  9. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  11. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  12. $response = curl_exec($ch);
  13. if (curl_errno($ch)) {
  14. $response = curl_error($ch);
  15. }
  16. curl_close($ch);
  17. echo $response;

在另一个网站上,我使用tk进行数据库搜索,然后发送响应:

  1. $bs = new lecRed; // lecRed是我之前创建和使用过的一个类
  2. $tk = $_POST["tk"];
  3. $rspta = $bs->searchByTK($tk)->fetchObject(); // 在这里从数据库中检索信息
  4. $arr = array(
  5. "id" => $rspta->ide,
  6. "name" => $rspta->name,
  7. "adsc" => $rspta->adsc,
  8. "pic" => $rspta->pic
  9. );
  10. echo "<h1>ID: " . $arr["id"] . "</h1>";
  11. echo "<h1>NAME: " . $arr["name"] . "</h1>";
  12. echo "<h1>ADSC: " . $arr["adsc"] . "</h1>";

到目前为止,一切正常。但现在我想要"echo"图片。我在服务器上有这张图片(与上一个PHP文件相同的路径)。

我尝试了以下代码:

  1. echo "<img src='".$arr["pic"]."' />";

但它没有起作用。我还尝试了以下代码:

  1. $file = 'img/' . $arr["pic"];
  2. $type = 'image/jpeg';
  3. header('Content-Type:' . $type);
  4. header('Content-Length: ' . filesize($file));
  5. readfile($file);

但它只显示了一些奇怪的字符:

将图像作为 PHP POST 响应发送到浏览器。

我尝试了以下代码:

  1. $file = 'img/' . $arr["pic"];
  2. if (file_exists($file)) {
  3. header('Content-Description: File Transfer');
  4. header('Content-Type: application/octet-stream');
  5. header('Content-Disposition: attachment; filename=' . basename($file));
  6. header('Content-Transfer-Encoding: binary');
  7. header('Expires: 0');
  8. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  9. header('Pragma: public');
  10. header('Content-Length: ' . filesize($file));
  11. ob_clean();
  12. flush();
  13. readfile($file);
  14. exit;
  15. }

但结果还是一样。

我还尝试使用GD库:

  1. header('content-type: image/jpeg');
  2. $image_file_path = "img/" . $arr["pic"];
  3. switch (pathinfo($image_file_path)['extension']) {
  4. case 'png':
  5. $image = imagecreatefrompng($image_file_path);
  6. break;
  7. case 'gif':
  8. $image = imagecreatefromgif($image_file_path);
  9. break;
  10. default:
  11. $image = imagecreatefromjpeg($image_file_path);
  12. }
  13. echo imagejpeg($image);
  14. imagedestroy($image);

但仍然没有起作用。我已经没有其他选择了。有人有任何想法吗?

英文:

So, I'm making a post request with CURL:

  1. &lt;?php
  2. $url = &#39;http://url/to/site&#39;;
  3. $data = array(
  4. &quot;tk&quot; =&gt; $_GET[&quot;tk&quot;]
  5. );
  6. $ch = curl_init($url);
  7. //seteamos la petici&#243;n
  8. curl_setopt($ch, CURLOPT_POST, 1);
  9. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  11. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  12. $response = curl_exec($ch);
  13. if (curl_errno($ch)) {
  14. $response = curl_error($ch);
  15. }
  16. curl_close($ch);
  17. echo $response;

and in the other site I'm doing a database search with the tk, and then sending a response:

  1. $bs = new lecRed; //lecRed it&#39;s a class I created and used before
  2. $tk = $_POST[&quot;tk&quot;];
  3. $rspta = $bs-&gt;searchByTK($tk)-&gt;fetchObject();//here I retrieve the info from the DB
  4. $arr = array(
  5. &quot;id&quot; =&gt; $rspta-&gt;ide,
  6. &quot;name&quot; =&gt; $rspta-&gt;name,
  7. &quot;adsc&quot; =&gt; $rspta-&gt;adsc,
  8. &quot;pic&quot; =&gt; $rspta-&gt;pic
  9. );
  10. echo &quot;&lt;h1&gt;ID: &quot; . $arr[&quot;id&quot;] . &quot;&lt;/h1&gt;&quot;;
  11. echo &quot;&lt;h1&gt;NAME: &quot; . $arr[&quot;name&quot;] . &quot;&lt;/h1&gt;&quot;;
  12. echo &quot;&lt;h1&gt;ADSC: &quot; . $arr[&quot;adsc&quot;] . &quot;&lt;/h1&gt;&quot;;

So far it is working fine. But now I want to "echo" the image. I have the image in the server (in the same route as the last php file).

I tried doing:

  1. echo &quot;&lt;img src=&#39;&quot;.$arr[&quot;pic&quot;].&quot;&#39; /&gt;&quot;;

but it didn't worked. I also tried:

  1. $file = &#39;img/&#39; . $arr[&quot;pic&quot;];
  2. $type = &#39;image/jpeg&#39;;
  3. header(&#39;Content-Type:&#39; . $type);
  4. header(&#39;Content-Length: &#39; . filesize($file));
  5. readfile($file);

but it only shows this weird characters:

将图像作为 PHP POST 响应发送到浏览器。

I tried this:

  1. $file = &#39;img/&#39; . $arr[&quot;pic&quot;];
  2. if (file_exists($file)) {
  3. header(&#39;Content-Description: File Transfer&#39;);
  4. header(&#39;Content-Type: application/octet-stream&#39;);
  5. header(&#39;Content-Disposition: attachment; filename=&#39; . basename($file));
  6. header(&#39;Content-Transfer-Encoding: binary&#39;);
  7. header(&#39;Expires: 0&#39;);
  8. header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0&#39;);
  9. header(&#39;Pragma: public&#39;);
  10. header(&#39;Content-Length: &#39; . filesize($file));
  11. ob_clean();
  12. flush();
  13. readfile($file);
  14. exit;
  15. }

but it's more of the same.

I also tried using de gd library:

  1. header(&#39;content-type: image/jpeg&#39;);
  2. $image_file_path = &quot;img/&quot; . $arr[&quot;pic&quot;];
  3. switch (pathinfo($image_file_path)[&#39;extension&#39;]) {
  4. case &#39;png&#39;:
  5. $image = imagecreatefrompng($image_file_path);
  6. break;
  7. case &#39;gif&#39;:
  8. $image = imagecreatefromgif($image_file_path);
  9. break;
  10. default:
  11. $image = imagecreatefromjpeg($image_file_path);
  12. }
  13. echo imagejpeg($image);
  14. imagedestroy($image);

But nothing worked. I ran out of options. Anyone have any idea?

答案1

得分: 2

一种解决方案是使用“数据URL”:<img src="data:..."

https://www.w3docs.com/snippets/html/how-to-display-base64-images-in-html.html

使用Base64编码的图像可以通过使用<img>标签嵌入到HTML中。这可以通过减少浏览器发出的额外HTTP请求来提高较小图像的页面加载时间。

Base64编码和数据URL是相辅相成的,因为数据URL减少了浏览器显示HTML文档所需的HTTP请求数量。

<img>标签具有一个src属性,其中包含图像的数据URL。数据URL由两部分组成,用逗号分隔。第一部分指定了Base64编码的图像,第二部分指定了图像的Base64编码字符串。

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />

此链接提供了几个示例,使用PHP的base64_encode()函数:

英文:

One solution is to use a "Data URL": &lt;img src=&quot;data:...

> https://www.w3docs.com/snippets/html/how-to-display-base64-images-in-html.html
>
> Images encoded with Base64 can be embedded in HTML by using the <img>
> tag. This can help to increase the page load time for smaller images
> by saving the browser from making additional HTTP requests.
>
> Base64 encoding and Data URL go hand-in-hand, as Data URLs reduce the
> number of HTTP requests that are needed for the browser to display an
> HTML document.
>
> The &lt;img&gt; tag has a src attribute and contains the Data URL of the
> image. A Data URL is composed of two parts, which are separated by a
> comma. The first part specifies a Base64 encoded image, and the second
> part specifies the Base64 encoded string of the image.
>
> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4
> //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />

This link has several examples, using PHP base64_encode():

huangapple
  • 本文由 发表于 2023年8月9日 02:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定