-2

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

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

我在想……当然不能显示,这是一个网址而不是图像……呃?!

一切都执行得很好,我只是得到这个认为 url 是图像的黑页。

这也发生在(一些)其他 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>
4

2 回答 2

0

首先,您不需要 html,因为您的输出是 jpeg。你错过了@on @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;
}

?>
于 2014-09-04T12:53:39.793 回答
0

我在想……当然不能显示,这是一个网址而不是图像……呃?!

有了这条线

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

您是在告诉浏览器,他在此之后收到的数据是 JPEG 图像的数据——因此,如果您打算在那之后发送图像数据,那么该行就没有位置了。

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

于 2014-09-04T14:20:23.537 回答