如何通过php将信息存储在dump中



我想实时存储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); 
?>

然而,我仍然不知道保存图像的最佳位置在哪里。

因此,为了不严格要求学习者,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开始和上面一样,不同的是在保存图像时开始,我们再次使用输出缓冲方法,但这次我们将输出作为base64编码的字符串传递给img标记。讨厌,讨厌:)

/* 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源)

更改

imagejpeg($jpg_image);

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

将图像写入文件,而不输出图像。。。所以…

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

最新更新