如何通过 php 在转储中存储信息
how to store information in dump through 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);
?>
但是,我还是不知道保存图片的最佳位置。
改变
imagejpeg($jpg_image);
至
imagejpeg($jpg_image,"imagefolderpath/image.jpg");
这会将图像写入文件,而不是输出图像...所以...
$image_url="dump/".rawurlencode(trim($text)).".jpg";
imagejpeg($jpg_image,$image_url);
readfile($image_url);
所以,不要对学习者太严格,Marc B 建议的解决方案是仔细研究 imagejpeg()
函数:
bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
所以有第二个可选参数$filename
,当给定时,它将告诉 GD 将图像存储在该文件中。
但是它还说,如果你传递一个文件名,图像不会自动输出到浏览器,如果你不传递第二个参数就会发生这种情况。
所以您必须完成三个步骤:
创建唯一的文件名
将图像保存到文件
在浏览器中显示图像
现在最简单的解决方案是:
/* --- 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);
所以这样做的好处是,您不必访问磁盘两次。
如果您不想return将图像作为原始图像格式,但实际上想立即在您的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>
方法二 开始同上,不同的是保存图片时开始,我们再次使用输出缓冲的方式,只是这次我们将输出传给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 显然来源)
我想实时存储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);
?>
但是,我还是不知道保存图片的最佳位置。
改变
imagejpeg($jpg_image);
至
imagejpeg($jpg_image,"imagefolderpath/image.jpg");
这会将图像写入文件,而不是输出图像...所以...
$image_url="dump/".rawurlencode(trim($text)).".jpg";
imagejpeg($jpg_image,$image_url);
readfile($image_url);
所以,不要对学习者太严格,Marc B 建议的解决方案是仔细研究 imagejpeg()
函数:
bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
所以有第二个可选参数$filename
,当给定时,它将告诉 GD 将图像存储在该文件中。
但是它还说,如果你传递一个文件名,图像不会自动输出到浏览器,如果你不传递第二个参数就会发生这种情况。
所以您必须完成三个步骤:
创建唯一的文件名
将图像保存到文件
在浏览器中显示图像
现在最简单的解决方案是:
/* --- 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);
所以这样做的好处是,您不必访问磁盘两次。
如果您不想return将图像作为原始图像格式,但实际上想立即在您的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>
方法二 开始同上,不同的是保存图片时开始,我们再次使用输出缓冲的方式,只是这次我们将输出传给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 显然来源)