使用php将图像保存到服务器

问题描述:

嘿,我有下面的脚本,基本上flash文件发送它的一些数据,它会创建一个图像,然后打开另存为用户对话框,以便他们可以保存图像到那里系统。问题出现了,我该如何将图像保存到我的服务器?使用php将图像保存到服务器

<?php 

$to = $_POST['to']; 
$from = $_POST['from']; 
$fname = $_POST['fname']; 
$send = $_POST['send']; 

$data = explode(",", $_POST['img']); 
$width = $_POST['width']; 
$height = $_POST['height']; 
$image=imagecreatetruecolor($width ,$height); 
$background = imagecolorallocate($image ,0 , 0 , 0); 
//Copy pixels 
$i = 0; 
for($x=0; $x<=$width; $x++){ 
    for($y=0; $y<=$height; $y++){ 
     $int = hexdec($data[$i++]); 
     $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int); 
     imagesetpixel ($image , $x , $y , $color); 
    } 
} 
//Output image and clean 
#header("Content-Disposition: attachment; filename=test.jpg"); 
header("Content-type: image/jpeg"); 

ImageJPEG($image); 
imagedestroy($image); 
?> 

帮助将不胜感激!

imagejpeg($image, $filename); 

将图像保存为$filename。所以基本上可以做

imagejpeg($image, $filename); 
imagedestroy($image); 
readfile($filename); 

,而不是仅仅调用

imagejpeg($image); 

略带华丽的方式等重复工作节省会是这样的,

/* start output buffering to save output */ 
ob_start(); 
/* output image as JPEG */ 
imagejpeg($image); 
/* save output as file */ 
ob_flush(); 
file_put_contents($filename, ob_get_contents()); 
+0

注意,就需要约。使用这种方法的内存是两倍,因为您将在内存中映像两次。 – 2010-03-12 12:01:48

+0

你会有未压缩和压缩,考虑到未压缩>>压缩它没有太大的问题。在IO受限环境中,在磁盘之前输出到客户端会更好。 – 2010-03-12 13:20:04

+0

嗨,感谢您的帮助!我将如何去指定文件应该保存的目录? – salmon 2010-03-18 08:21:44