从php网页获取图像

问题描述 投票:0回答:1

我在php页面上创建了一个简单的图像,代码如下。我需要将此图片的网址作为其他网站的直接链接。我在虚拟主机提供商上尝试了相同的代码,并且一切正常,但是当我在vps上使用相同的代码时,它不能用作直接链接。如果我从网络浏览器检查它们是否相同,那么我会丢失什么?

[我还注意到,例如,如果我在Telegram上使用它,则只有起作用的那个才会生成缩略图。

<?php

header("Content-type: image/png");
$im     = imagecreatefrompng("image/orange.png");
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'font/font.ttf';
$string = "test";
imagettftext($im, 20, 0, 20, 230, $white, $font, $string);
imagepng($im);
imagedestroy($im);

?>

编辑:是否会影响以下事实:访问vps中的链接,我使用机器的IP地址,而对于虚拟主机,则使用其域?

php
1个回答
0
投票

有很多原因可能导致它无法正常工作-太多了,无法在此处列出。但是,我要发布的原因是您似乎并没有尝试自己诊断问题。也许您不知道如何。

您的第一步是确保日志记录在您的平台上正常运行。编写一些带有故意错误的代码。确保您可以在日志中看到这些错误(对于开发环境,只能在浏览器中看到)。如果没有,请找出原因。

然后尝试运行图像脚本-看看它是否引发任何错误。

另一个寻找问题的好地方是Web开发人员工具-非200状态代码应该是一个危险信号,表明您的代码有很大问题。

但是您还需要学习如何进行防御性编码。您的代码应检测到问题并做出明智的响应。特别是当执行线程触及PHP运行时之外的东西(例如文件)时。

<?php

$file="image/orange.png";
$im     = imagecreatefrompng($file);
if ($im===false) {
   bailout("failed to read $file as an image");
}
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'font/font.ttf';
$string = "test";
if (false===imagettftext($im, 20, 0, 20, 230, $white, $font, $string)) {
    bailout("imagettftext fail");
}
header("Content-type: image/png"); // note this moved here
if (false===imagepng($im)) {
    bailout("imagepng failed");
}
imagedestroy($im);
exit;

function bailout($msg)
{
   header("Content-type: text/html", True, 519); // yes I know its not html
   trigger_error($msg); // record specificreason in log
   print $msg;
   exit;
}

您可能会在代码中检查很多其他东西,但是它们大多数会被PHP捕获/报告。

© www.soinside.com 2019 - 2024. All rights reserved.