GD2 URL显示为图像

时间:2014-09-04 10:20:21

标签: php gd2

当我运行此代码时,我收到以下错误:

图像" http://siteprevue.net/flipit.php"无法显示,因为它包含错误。

我在想...当然它无法显示,它不是一个图像的网址......呃?!

一切都很好,我只是得到这个黑色页面,认为网址是一个图像。

这也发生在(某些)其他GD2编码页面上,但不是全部。

有没有人知道发生了什么?

<?php
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg';
$new_img = '../../Uploads/Gallery/copy_bg_200.jpg';

$image = imagecreatefromjpeg($src);
$image = flip($image,1,0); // flips horizontal
//$image = flip($image,0,1); // flips vertical
//$image = flip($image,1,1); // flips both

header("Content-type: image/jpeg");
imagejpeg($image, $new_img, 80);
imagedestroy($image);

function flip($i,$h=1,$v=0) {
$width = imagesx($i);
$height = imagesy($i);
$temp = imagecreatetruecolor($width,$height);
imagecopy($temp,$i,0,0,0,0,$width,$height);
if ($h==1) {
for ($x=0 ; $x<$width ; $x++) {
imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height);
}
imagecopy($temp,$i,0,0,0,0,$width,$height);
}
if($v==1) {
for ($x=0; $x<$height ; $x++) {
imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1);
}
}
return $i;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org   /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

首先你不需要html,因为你的输出是一个jpeg。 您错过了@上的@imagecreatefromjpeg。 但是您的问题是输出文件名与header不起作用。图像将成功创建,但您会收到错误。

imagejpeg 文件名

The path to save the file to. If not set or NULL, 
the raw image stream will be outputted directly.

您可以使用header保存文件或打印文件。所以你应该插入null。这适用于我,但它不会存储您的更改:

<?php
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg';

$image = imagecreatefromjpeg($src);
$image = flip($image,1,0); // flips horizontal
//$image = flip($image,0,1); // flips vertical
//$image = flip($image,1,1); // flips both

header("Content-type: image/jpeg");
imagejpeg($image, null, 80);
imagedestroy($image);

function flip($i,$h=1,$v=0) {
    $width = imagesx($i);
    $height = imagesy($i);
    $temp = imagecreatetruecolor($width,$height);
    imagecopy($temp,$i,0,0,0,0,$width,$height);
    if ($h==1) {
        for ($x=0 ; $x<$width ; $x++) {
            imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height);
        }
        imagecopy($temp,$i,0,0,0,0,$width,$height);
    }
    if($v==1) {
        for ($x=0; $x<$height ; $x++) {
            imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1);
        }
    }
    return $i;
}

?>

答案 1 :(得分:0)

  

我在想...当然它无法显示,它是一个网址而不是图像...呃?!

使用此行

header("Content-type: image/jpeg");

你告诉浏览器他之后收到的数据是JPEG图像的数据 - 所以如果你打算在那之后发送图像数据,那么那条线就没有位置了

只需删除它,以便PHP再次发送其默认的Content-Type text/html

相关问题