所以,不为了严格要求学习者,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);
所以这样做的好处是,你不会有两次访问磁盘。
如果您不想将图像作为原始图像格式返回,但实际上希望立即将其显示在您的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源明显)
rtfm:http://php.net/imagejpeg,特别要注意其第二个(可选)参数... –
请不要给这些链接,因为我不明白我的理解与真实的工作示例 – learner
那么你不会在这个行业中走得很远。要么学会阅读文件,要么找到不同的职业。 –