【发布时间】:2016-08-15 10:38:11
【问题描述】:
这是我的代码,test1.php 有效,test2.php 无效。
test1.php:
<?php
session_start();
header('Content-type: image/jpeg');
$text = rand(1000,9999);
$font_size = 5;
$image_width = imagefontwidth($font_size) * strlen($text);
$image_height = imagefontheight($font_size);
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $text, $text_color);
$_SESSION['image'] = $image;
$image_session = $_SESSION['image'];
imagejpeg($image_session);
?>
test2.php:
<?php
session_start();
header('Content-type: image/jpeg');
$image_session = $_SESSION['image'];
imagejpeg($image_session);
?>
如您所见,test1.php 创建了一个随机图像。 我可以使用:
<img src="test1.php">
在任何页面中显示来自 test1.php 的图像。 但是,我想在其他 php 文件中使用 if else 语句。
例如:
如果用户点击提交按钮并没有输入任何内容(没有答案),图像仍然是相同的,他们必须回答相同的问题。如果失败,图像将会改变。
我不想使用 javascript 来阻止用户输入任何内容并将图像存储在磁盘中。
所以,我认为我需要一个变量来存储可以再次使用的图像。 但我发现我不能使用上述方法。
我怎样才能做到这一点?
【问题讨论】:
-
将存储工作? imagejpeg($image_session, "/home/abc/a.jpg");
-
是的,但我不想存储它,因为我需要一个可以写入的文件夹,它会占用太多磁盘空间。
-
HD 比 RAM 大,因为所有会话通常都存储在 RAM 中。
-
是的,但是为什么会话不能存储图像?方法不对?
标签: php