当放置在 HTML 中时,PHP 脚本在浏览器中抛出垃圾

标签 php html

所以我使用了创建此页面的 PHP 代码 here , 并将其放入一个新的 .php 文件中,然后上传该文件 here .

我只是在屏幕上看到一大堆垃圾字符。有什么想法吗?

下面是完整的 PHP:

<html>
<head>
    <title></title>
</head>
<body>

<?

// gif is more appropriate than jpg for this kind of image
header("Content-type: image/gif");

// both coupon.jpg and Helvetica.ttf must be located in the same directory as
// dynamic.php or these variables must be updated.
$imgname    = "./coupon.jpg";
$fontname   = "./Helvetica.ttf";

// read image from disk
$im = imagecreatefromjpeg( $imgname )
    or die("Source coupon image has been moved or renamed! Expected coupon.jpg");

// variable allocation
$size       = 11;
$textheight = 250;
$imwidth    = imagesx( $im );
$black      = imagecolorallocate( $im, 0,0,0 );

// create the string containing tomorrow's date
$text       = "Offer expires " .
    date('l\, F j\, Y', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y")))."."; 

// learn the width of the newly allocated string
$bbox       = imagettfbbox( $size, 0, $fontname, $text );
$width      = $bbox[2] - $bbox[0];

// update, display, and clear from memory the newly modified image
imagettftext($im,$size,0,$imwidth/2 - $width/2,$textheight,$black,$fontname,$text);
imagegif($im);
imagedestroy($im);

?>

</body>
</html>

已添加更新: 我的目标是在新页面/PHP 生成的图像上使用 Google 分析转换脚本,这可能吗?

最佳答案

你期待什么?你正在输出 HTML,然后 after 一些 Content-type: text/plain 输出(即 HTML),说 Content-type: image/gif

移除 PHP 周围的 HTML,只保留 PHP。

关于当放置在 HTML 中时,PHP 脚本在浏览器中抛出垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15237873/

相关文章:

javascript - 滚动到一个链接并激活它

php - 在 Woocommerce 中删除重复的导入订单

html - 如何让我的菜单栏仅在图标滚动时出现?

javascript - 如何在javascript中的两个分隔符之间拆分字符串?

javascript - 强制在新选项卡中打开链接仍然是不礼貌的行为吗?

html - 溢出 : auto is messing up the width of my window

PHP - 在字符串中搜索电话号码和电子邮件

php - 如果是 iPad/iPhone,则不显示此信息

javascript - 按值对 JSON 进行排序

php - 如何根据用户点击 Jquery Galleria 幻灯片在开始时设置自定义图像