【问题标题】:How to store and get image from a image variable (SESSION)?如何从图像变量(SESSION)存储和获取图像?
【发布时间】: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


【解决方案1】:

imagecreate() 返回代表给定图像的 resource。 PHP 的会话不能存储资源类型变量(更准确地说 - PHP 无法在脚本结束时序列化它们),请参阅http://php.net/manual/en/function.session-register.php

注意:目前无法将资源变量注册到 会议。 ...

您可以将图像序列化为字符串并将此字符串存储到会话中(未测试):

test1.php:

...

ob_start();
imagejpeg($image);
$contents = ob_get_contents();
ob_end_clean();
$_SESSION['image'] = $contents;

test2.php:

header('Content-type: image/jpeg');
die($_SESSION['image']);

【讨论】:

    【解决方案2】:

    在不了解上下文的情况下,你不能做类似的事情

    session_start();
    
    $_SESSION['randomValue'] = mt_rand(1000,9999);
    
    if(someValueIsEntered){
         $_SESSION['randomValue'] = mt_rand(1000,9999);
    }
    
    echo "<img src='test.php?random=".$_SESSION['randomValue']."'/>";
    

    Test.php

    $randomValue = filter_input(INPUT_GET, 'random');
    
    header('Content-type: image/jpeg');
    
    $text = $randomValue;
    $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);
    
    imagejpeg($image);
    

    多参数示例:

    将有关图像的信息存储在数组中。

    session_start();
    
    if(!isset($_SESSION['imageData']){
        $_SESSION['imageData'] = array(
                                       "random" => mt_rand(1000,9999),
                                       "x1" => mt_rand(0,10),
                                       "x2" => mt_rand(0,10)
                                       );
    }
    
    if(someValueIsEntered){
        //Randomize array again.
    }
    
    $imageString = "test.php";
    foreach ($_SESSION['imageData'] as $key => $value) {
        $index = current($array);
    
        if($index == 0) {
           $seperator = "?";
        } else {
           $seperator = "&";
        }
    
        $imageString .= $seperator.$key."=".$value;
    }
    
    echo "<img src='".$imageString."'/>";
    

    然后在 test.php 中调用它们。

    【讨论】:

    • 谢谢,我用的是类似的方法。我使用 session 来存储随机数,并将其传输到 test1.php。它可以工作,但是在我将 imageline 添加到其中之后,我发现我必须将这么多数据传递给它。所以我正在寻找更好的方法。
    • 我不太清楚你的意思。这在传递的数据量上有什么不同?
    • 例如:imageline()函数需要$x1 $y1 $x2 $y2,那么我需要创建4个会话变量来保存随机值。为什么我不能使用一个会话?
    • 您也可以使用其他方法。但是你当然可以在会话变量中使用一个数组。检查我更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多