Home  >  Article  >  Backend Development  >  php用gd库生成图片后上载有关问题

php用gd库生成图片后上载有关问题

WBOY
WBOYOriginal
2016-06-13 13:00:01830browse

php用gd库生成图片后下载问题
是这样的,我在用GD库生成图片后,不保存图片能直接下载吗? 试了一个早上还是不行…

image.php

//创建一个真彩画布<br />
$image = imagecreatetruecolor(400,190);<br />
//背景创建颜色<br />
$green = imagecolorallocate($image,255,255,255);<br />
//填充画布颜色<br />
imagefill($image,0,0,$green);<br />
//输出图片<br />
header("Content-Type: image/jpeg");<br />
imagejpeg($image);<br />
//销毁资源<br />
imagedestroy($image);


index.php

<a href="imagedown.php?filename=image.php"><img  src="image.php" / alt=" php用gd库生成图片后上载有关问题 " ></a>


imagedown.php

<br />
if(isset($_GET['filename'])){<br />
????????//var_dump(getimagesize($_GET['filename']));<br />
????????download($_GET['filename']);<br />
????}<br />
????function download($fileName){<br />
????????header("Content-Type: image/jpeg");<br />
????????header('Content-Disposition: attachment; filename="'.$fileName.'"');<br />
????????header('Content-Length: '.filesize($fileName));<br />
????????readfile($fileName);<br />
????}<br />

------解决方案--------------------
不可以的,必须经过图片落地这一过程。php的gd库貌似不支持内存直接读取生成的图片内容,只能保存到本地再读取。
建议改写gd库,提供获取图片内容函数。
------解决方案--------------------
这样写
download('x.jpg');<br />
<br />
function image() {<br />
  //创建一个真彩画布<br />
  $image = imagecreatetruecolor(400,190);<br />
  //背景创建颜色<br />
  $green = imagecolorallocate($image,255,255,0);<br />
  //填充画布颜色<br />
  imagefill($image,0,0,$green);<br />
  //输出图片<br />
  //header("Content-Type: image/jpeg");<br />
  imagejpeg($image);<br />
  //销毁资源<br />
  imagedestroy($image);<br />
}<br />
<br />
function download($fileName){<br />
  ob_start();<br />
  image();<br />
  $s = ob_get_clean();<br />
  header("Content-Type: image/jpeg");<br />
  header('Content-Disposition: attachment; filename="'.$fileName.'"');<br />
  header('Content-Length: '.strlen($s));<br />
  echo $s;<br />
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn