php用gd库生成图片后下载问题
是这样的,我在用GD库生成图片后,不保存图片能直接下载吗? 试了一个早上还是不行…
image.php
//创建一个真彩画布
$image = imagecreatetruecolor(400,190);
//背景创建颜色
$green = imagecolorallocate($image,255,255,255);
//填充画布颜色
imagefill($image,0,0,$green);
//输出图片
header("Content-Type: image/jpeg");
imagejpeg($image);
//销毁资源
imagedestroy($image);
index.php
imagedown.php
if(isset($_GET['filename'])){
//var_dump(getimagesize($_GET['filename']));
download($_GET['filename']);
}
function download($fileName){
header("Content-Type: image/jpeg");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('Content-Length: '.filesize($fileName));
readfile($fileName);
}
------解决方案--------------------
不可以的,必须经过图片落地这一过程。php的gd库貌似不支持内存直接读取生成的图片内容,只能保存到本地再读取。
建议改写gd库,提供获取图片内容函数。
------解决方案--------------------
这样写download('x.jpg');
function image() {
//创建一个真彩画布
$image = imagecreatetruecolor(400,190);
//背景创建颜色
$green = imagecolorallocate($image,255,255,0);
//填充画布颜色
imagefill($image,0,0,$green);
//输出图片
//header("Content-Type: image/jpeg");
imagejpeg($image);
//销毁资源
imagedestroy($image);
}
function download($fileName){
ob_start();
image();
$s = ob_get_clean();
header("Content-Type: image/jpeg");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('Content-Length: '.strlen($s));
echo $s;
}