2015-04-16 25 views
1

我想实时存储由php创建的图像。目前,每个图像在创建后都会被破坏,但我想将图像存储在我网站上的唯一文件夹中。如何通过php存储转储信息

我见过很多使用专用转储文件夹的网站,例如http://www.example.com/dump/4592898349.jpg,我想知道是否有人可以向我解释我该怎么做。

这是我与启动图像生成形式的html代码:

<html> 
    <body> 
     <form action="result.php" method="get"> 
      <input type="text" name="name" id="name"> 
      <input type="button" name="submit" id="submit"> 
     </form> 
    </body> 
</html> 

这里是我的PHP代码,创建与给定文本的图像:

<?php 
    header('Content-type: image/jpeg'); 
    $jpg_image = imagecreatefromjpeg('Desert.jpg'); 
    $white = imagecolorallocate($jpg_image, 73, 41, 236); 
    $font_path = 'OpenSans-Italic.TTF'; 
    $text = $_GET['name'] ; 
    imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text); 
    imagejpeg($jpg_image); 
    imagedestroy($jpg_image); 
?> 

然而,我仍然不知道哪里是保存图像的最佳地点。

+1

rtfm:http://php.net/imagejpeg,特别要注意其第二个(可选)参数... –

+0

请不要给这些链接,因为我不明白我的理解与真实的工作示例 – learner

+7

那么你不会在这个行业中走得很远。要么学会阅读文件,要么找到不同的职业。 –

回答

0

变化

imagejpeg($jpg_image); 

imagejpeg($jpg_image,"imagefolderpath/image.jpg"); 

需要写入图像文件,而不是输出的图像...所以...

$image_url="dump/".rawurlencode(trim($text)).".jpg"; 
imagejpeg($jpg_image,$image_url); 
readfile($image_url); 
+0

如果我想设置图像名称,我们从html输入标记 – learner

+0

我想给图像名称从字符串'$ _GET ['name']' – learner

+0

是的,你有这条线'$ text = $ _GET ['name'];'你把它分配给$ text ....所以这是同样的事情。我urlencoded它,因为我认为你想再次使用它的URL,对吧? – RightClick

1

所以,不为了严格要求学习者,Marc B建议的解决方案更接近于imagejpeg()函数:

bool imagejpeg (resource $image [, string $filename [, int $quality ]]) 

因此,有第二个可选参数$filename,如果给出,它会告诉GD将图像存储在该文件中。

但是它也说,如果你传递一个文件名,图像不会自动输出到浏览器,因为如果你不通过第二个参数就会发生这种情况。

因此,有三个步骤,你将必须做到:

  1. 创建唯一的文件名

  2. 保存图像浏览器中的文件

  3. 显示图像

现在最简单的解决方案是:

/* --- create unique filename --- */ 

// get name passed or set default, this will be prepended to 
// the unique filename to make it more readable 
$prepend_filename = (isset($_GET['name']) ? rawurlencode(trim($_GET['name'])) : 'ing'; 

// set to the path where you want your images stored 
$dirname = "/your/base/path/wherever/"; 
// use a while loop to make sure or new filename does not exist 
while (true) { 
    // now build unique filename 
    $filename = uniqid($prepend_filename, true) . '.jpg'; 
    // if filename is unique (file does not exist) then exit loop 
    if (!file_exists($dirname . $filename)) break; 
} 

/* --- save image to file --- */ 

imagejpeg($jpg_image, $dirname . $filename); 

/* --- output image to browser --- */ 

readfile($dirname . $filename); 

所以这将基本上工作。唯一不太好的解决方案是,首先您要访问磁盘来写入映像,然后您必须重新访问该文件以将其输出到浏览器。还有另一种技术,您可以使用,以减少文件访问:

/* --- output image to browser ---*/ 

// use output buffering to capture outputted image stream 
ob_start(); 
// output the image to output buffer 
imagejpeg($jpg_image); 
// stop capture, flush output to browser and save as string 
$img_data = ob_get_flush(); 

/* --- save image to file --- */ 

$fp = fopen($dirname . $filename, 'w'); 
fwrite($fp, $img_data); 
fclose($fp); 

所以这样做的好处是,你不会有两次访问磁盘。

如果您不想将图像作为原始图像格式返回,但实际上希望立即将其显示在您的html页面中,则有两种方法。

方法1(删除上面重复部分的注释)在这里我们创建图像,保存它的文件并用我们创建的文件名生成html输出。

<?php 

/* --- create unique filename --- */ 

$prepend_filename = (isset($_GET['name']) ? rawurlencode(trim($_GET['name'])) : 'ing'; 
$dirname = "/your/base/path/wherever/"; 
while (true) { 
    $filename = uniqid($prepend_filename, true) . '.jpg'; 
    if (!file_exists($dirname . $filename)) break; 
} 

/* --- save image to file --- */ 

imagejpeg($jpg_image, $dirname . $filename); 

// now the difference starts 

?><html> 
    <head> 
     <title>Your title here</title> 
     <!-- the rest of your head here --> 
    </head> 
    <body> 
     <img src="http://yourserver.com/your/base/path/wherever/<?php 
      echo $filename; 
     ?>" width="100%" /> 
    </body> 
</html> 

方法2开始是与上述相同,差别开始保存图像时,我们再次使用输出缓冲方法,但此时,我们的输出传递给IMG标记为base64编码的字符串。讨厌,讨厌:)

/* Generating the filename is the same as above (not copied here). 
    Output buffering same as prior sample, with one difference: 
    Now we use ob_get_clean() instead of ob_get_flush() because 
    we do not want to output image. 
*/ 
ob_start(); 
imagejpeg($jpg_image); 
$img_data = ob_get_clean(); // NOTE: ob_get_clean() not ob_get_flush() 

/* --- save image to file --- */ 

$fp = fopen($dirname . $filename, 'w'); 
fwrite($fp, $img_data); 
fclose($fp); 

?><html> 
    <head> 
     <title>Your title here</title> 
     <!-- the rest of your head here --> 
    </head> 
    <body> 
     <img src="data:image/jpeg;base64,<?php 
      echo base64_encode($img_data); 
     ?>" width="100%" /> 
    </body> 
</html> 

所以在这里我们使用的缓冲$img_data的文件写入到磁盘和在HTML页面中直接输出,而不必调用从服务器上的文件浏览器(但具有较大的HTML源明显)

+0

我添加了RightClick建议的解决方案,将传递给脚本的rawurlencoded name参数添加到gi已经在文件名随机的字符 –

+0

我想打电话给该图像的网页上,包括HTML' <身体

learner

+0

@learner所以你去了,添加了两个样本,你如何能够直接生成你的新生成的图像包括HTML源代码。 –