2016-08-15 58 views
1

這裏是我的代碼,test1.php的作品,test2.php不起作用。如何存儲和獲取圖像變量(SESSION)的圖像?

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語句。

例如:

,如果用戶點擊提交按鈕和輸入任何內容(無答案),圖像將仍然是相同的,他們必須回答同樣的問題。如果失敗,圖像將會改變。

我不想使用JavaScript來防止用戶輸入任何內容並將圖像存儲在磁盤中。

所以,我認爲我需要一個變量來存儲可以再次使用的圖像。 但我發現我不能使用上面的方法。

我該如何做到這一點?

+0

會存儲工作嗎? \t imagejpeg($ image_session,「/home/abc/a.jpg」); – owaishanif786

+0

雅,但我不想存儲它,因爲我需要一個可以寫入的文件夾,並且它可以佔用太多的磁盤空間。 – Frank

+0

HD比RAM更大,因爲所有會話通常都存儲在RAM中。 – owaishanif786

回答

1

imagecreate()返回代表給定圖像的資源。 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']); 
+0

我不能相信它的作品。謝謝! – Frank

0

不知道有關上下文太多,你不能這樣做

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中調用它們。

+0

謝謝,我正在使用類似的方法。我使用會話來存儲隨機數,並將其傳送到test1.php。它可以工作,但是在我添加圖像線之後,我發現我必須將大量數據傳遞給它。所以我正在尋找更好的方法。 – Frank

+0

我不完全確定你的意思。這在數據傳遞量上有何不同? – Imbue

+0

例如:imageline()函數需要$ x1 $ y1 $ x2 $ y2,那麼我需要創建4個會話變量來保存隨機值。爲什麼我不能使用一個會話? – Frank

相關問題