【问题标题】:GD library Showing Special Characters显示特殊字符的 GD 库
【发布时间】:2021-02-11 21:06:25
【问题描述】:

如您所见,我已经安装了 GD 库。(图片) 但是当我尝试创建图像时,它会显示特殊字符而不是图像。(图像) 我正在使用 laravel 和 Xampp。

有人知道是什么问题吗?

<?php
  

// create a blank image
$image = imagecreatetruecolor(400, 300);

// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);

// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);

// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);

// output the picture
header("Content-type: image/png");
imagepng($image); ?>

【问题讨论】:

  • 输出(在图像中)看起来像 var_dump,而不是该脚本的输出。 (那些“特殊字符”你的形象)
  • 我知道那是我的形象,但为什么会这样显示?它必须是 laravel 的东西,因为我已经在一个干净的 index.php 上进行了测试并且可以工作,但是在 laravel 视图中它不起作用

标签: php laravel gd


【解决方案1】:

如果您希望图像显示为图像,则需要在发送header 之前删除所有输出。从您的代码中删除 var_dump(gd_info()); 以查看实际图像:

<?php

// create a blank image
$image = imagecreatetruecolor(400, 300);

// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);

// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);

// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);

// output the picture
header("Content-type: image/png");
imagepng($image); ?>

来自https://www.php.net/manual/en/function.header.php

"请记住,必须在发送任何实际输出之前调用 header()"

【讨论】:

  • 这正是我的代码的样子,我使用 var 转储只是为了显示我的 gd 库,正如我告诉你的,它必须是 laravel 的东西,因为它不接受标题(“内容类型: 图像/png");
  • 不是你的代码是这样的,你有var_dump在那里。如果 var_dump 与您的问题/代码无关,请将其删除。你如何调用/访问该图像?
  • 我的控制器中有一个函数 public function image() { return View('image'); } 和代码是我的看法“image.blade.php”
  • 这不是它的工作原理。当使用刀片视图时,内容已经发送,因此header 将不起作用。如果该脚本位于单独的.php 脚本中(例如image.php),您将使用&lt;img src="image.php"&gt; 访问图像。
  • 我刚刚发现,在我的函数中,我返回了一个带有视图的响应,并且标题返回 response() ->view('image') ->header('Content-Type', 'image /png');感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多