保存裁剪后的图像时屏幕上出现垃圾
Getting junk on screen when saving cropped image
我的代码正在运行并保存裁剪后的图像,但是,我只需要它来显示回显中的内容。
现在,如果我保留 header 行,它只会在屏幕上显示图像,而不会显示回显中的内容。
如果我删除 header 行,它会在屏幕上显示大量垃圾,然后显示回显。
我怎样才能不显示垃圾信息或只显示图像并显示回显中的内容?
这是我的代码...
$src = 'images/'.$imagename;
$w=250;
$h=300;
$x=$_POST['x'];
$y=$_POST['y'];
$png_quality = 0;
// if I remove this header I get junk and nothing from the echo below
header('Content-type: image/png');
$image = imagecreatefrompng($src);
if (!$src)
exit("not a valid image");
$crop = imagecreatetruecolor($w,$h);
$new = imagecopy ($crop, $image, 0, 0, $x, $y, $w, $h);
imagepng($crop);
imagepng($crop,$src,$png_quality))
echo<<<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Image </title>
</head>
<body>
Here is your image:<br /><br />
<img src="images/$imagename">
</body>
</html>
END;
稍微清理一下您的代码。看起来你们都在保存裁剪后的 png 并使用 imagepng($crop);
将其输出到浏览器。为什么?
imagepng — Output a PNG image to either the browser or a file
从整体上看,我假设您想裁剪和更新磁盘上的文件,然后通过将其用作 img
属性的 src
来呈现更新后的文件。
只有 header 才有意义,如果你想直接输出图像。
<?php
$src = 'images/' . $imagename;
$w = 250;
$h = 300;
$x = $_POST['x'];
$y = $_POST['y'];
$png_quality = 0;
// if I remove this header I get junk and nothing from the echo below
//header('Content-type: image/png');
$image = imagecreatefrompng($src);
if (!is_resource($image)) exit("not a valid image");
$crop = imagecreatetruecolor($w, $h);
$new = imagecopy($crop, $image, 0, 0, $x, $y, $w, $h);
//imagepng($crop); Is this desired?
imagepng($crop, $src, $png_quality);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Image </title>
</head>
<body>
Here is your image:<br /><br />
<img src="images/<?=$imagename?>">
</body>
</html>
最后,假设共享的代码是准确的,您应该得到了一个错误。确保您有错误报告并正在检查日志。我猜你将图像渲染到浏览器(header 可能会有所帮助),然后甚至在点击你的 HTML.
之前就出现了脚本错误
我的代码正在运行并保存裁剪后的图像,但是,我只需要它来显示回显中的内容。
现在,如果我保留 header 行,它只会在屏幕上显示图像,而不会显示回显中的内容。
如果我删除 header 行,它会在屏幕上显示大量垃圾,然后显示回显。
我怎样才能不显示垃圾信息或只显示图像并显示回显中的内容?
这是我的代码...
$src = 'images/'.$imagename;
$w=250;
$h=300;
$x=$_POST['x'];
$y=$_POST['y'];
$png_quality = 0;
// if I remove this header I get junk and nothing from the echo below
header('Content-type: image/png');
$image = imagecreatefrompng($src);
if (!$src)
exit("not a valid image");
$crop = imagecreatetruecolor($w,$h);
$new = imagecopy ($crop, $image, 0, 0, $x, $y, $w, $h);
imagepng($crop);
imagepng($crop,$src,$png_quality))
echo<<<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Image </title>
</head>
<body>
Here is your image:<br /><br />
<img src="images/$imagename">
</body>
</html>
END;
稍微清理一下您的代码。看起来你们都在保存裁剪后的 png 并使用 imagepng($crop);
将其输出到浏览器。为什么?
imagepng — Output a PNG image to either the browser or a file
从整体上看,我假设您想裁剪和更新磁盘上的文件,然后通过将其用作 img
属性的 src
来呈现更新后的文件。
只有 header 才有意义,如果你想直接输出图像。
<?php
$src = 'images/' . $imagename;
$w = 250;
$h = 300;
$x = $_POST['x'];
$y = $_POST['y'];
$png_quality = 0;
// if I remove this header I get junk and nothing from the echo below
//header('Content-type: image/png');
$image = imagecreatefrompng($src);
if (!is_resource($image)) exit("not a valid image");
$crop = imagecreatetruecolor($w, $h);
$new = imagecopy($crop, $image, 0, 0, $x, $y, $w, $h);
//imagepng($crop); Is this desired?
imagepng($crop, $src, $png_quality);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Image </title>
</head>
<body>
Here is your image:<br /><br />
<img src="images/<?=$imagename?>">
</body>
</html>
最后,假设共享的代码是准确的,您应该得到了一个错误。确保您有错误报告并正在检查日志。我猜你将图像渲染到浏览器(header 可能会有所帮助),然后甚至在点击你的 HTML.
之前就出现了脚本错误