2017-04-11 67 views
0

我正在研究一個php教程,其中縮略圖生成頁面允許我從服務器上的目錄下拉列表中進行選擇,並且擊中submit按鈕,給定大小的縮略圖是使用自定義縮略圖類創建的(縮略圖會覆蓋原始圖像,這對我現在所做的事情來說很好)。這是基本的東西,並按預期工作。

頁代碼:

<?php 
$folder = '../images/'; 

use ClassFiles\Image\Thumbnail; 

if (isset($_POST['create'])) { 
    require_once('ClassFiles/Image/Thumbnail.php'); 
    try { 
     $thumb = new Thumbnail($_POST['pix']); 
     $thumb->setDestination('../images/'); 
     $thumb->setMaxSize(400); 
     $thumb->create(); 
     $messages = $thumb->getMessages(); 
    } catch (Exception $e) { 
     echo $e->getMessage(); 
    } 
} 
?> 
<!DOCTYPE HTML> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Thumb</title> 
</head> 

<body> 
<?php 
if (isset($messages) && !empty($messages)) { 
    echo '<ul>'; 
    foreach ($messages as $message) { 
     echo "<li>$message</li>"; 
    } 
    echo '</ul>'; 
} 
?> 

<form method="post" action=""> 
    <p> 
     <select name="pix" id="pix"> 
      <option value="">Select an image</option> 
      <?php 
      $files = new FilesystemIterator('../images/'); 
      $images = new RegexIterator($files, '/\.(?:jpg|png|gif)$/i'); 
      foreach ($images as $image) { 
       $filename = $image->getFilename(); 
       ?> 
       <option value="<?= $folder . $filename; ?>"><?= $filename; ?></option> 
      <?php } ?> 
     </select> 
    </p> 
    <p> 
     <input type="submit" name="create" value="Create Thumbnail"> 
    </p> 
</form> 
</body> 
</html> 

自定義縮略圖類是漫長的,爲簡潔起見,我不會在這裏張貼,除非要求,因爲它工作正常。

所以這裏的問題:

我決定把圖像路徑和圖像文件名信息從我一直工作在上傳頁面,並將它們存儲在可採取的縮略圖生成頁面會話變量。在縮略圖生成網頁代碼進行了修改,如下所示:

<?php 
require_once('includes/session_admin.php'); 

$folder = $_SESSION['image_path']; 

use ClassFiles\Image\Thumbnail; 

$getSize = getimagesize($_SESSION['image_path'] . $_SESSION['image_filename']); 

$imagePath = $_SESSION['image_path']; 
$imageFilename = $_SESSION['image_filename']; 

if ($getSize[0] > 400) { 
    require_once('ClassFiles/Image/Thumbnail.php'); 
    try { 
     $thumb = new Thumbnail($imageFilename); 
     $thumb->setDestination($imagePath); 
     $thumb->setMaxSize(400); 
     $thumb->create(); 
     $messages = $thumb->getMessages(); 
    } catch (Exception $e) { 
     echo $e->getMessage(); 
    } 
} else { 
    echo "Image is " . $getSize[0] . "px wide and is OK!"; 
} 
?> 
<!DOCTYPE HTML> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Thumb</title> 
</head> 

<body> 
<?php 
if (isset($messages) && !empty($messages)) { 
    echo '<ul>'; 
    foreach ($messages as $message) { 
     echo "<li>$message</li>"; 
    } 
    echo '</ul>'; 
} 

// this was just to test that the session variables were correct 
echo $_SESSION['image_path'] . $_SESSION['image_filename']; 
echo '<br>'; 
print_r(getimagesize($_SESSION['image_path'] . $_SESSION['image_filename'])); 
?> 
<!-- 
Removed the form... 
--> 
</body> 
</html> 

現在,而不是條件語句檢查,看看$_POST提交的代碼(我認爲)會自動檢查是否圖像,給出完整路徑和文件名大於400像素,如果是,則使用自定義縮略圖類調整圖像大小。

但是,這會從縮略圖類中引發錯誤,這是類似於教程中原始縮略圖生成頁代碼的相同類。 這工作在原來的教程代碼:

$thumb = new Thumbnail($_POST['pix']);

但不是當修改,以代替會話變量:

$thumb = new Thumbnail($imageFilename);

我看了又看任何建議,$_POST是在這裏需要,我檢查了會話變量傳遞了正確的信息,他們是。但是從$_POST切換到使用會話變量可以防止這種情況發揮作用。

正如你所看到的,我仍然在學習php,這是一直阻礙我的障礙之一。也許答案顯而易見,但我當然處於停滯狀態。

所有的輸入讚賞,謝謝!

+0

異常的錯誤消息是什麼? – leepowers

+0

它引發了三類內建的錯誤:'無法打開image_1.jpg.','image_1.jpg看起來不是圖像.'和'無法確定文件的大小「。 – mileaminute

回答

0

在設置類的對象之前嘗試此操作 $ _POST ['pix'] = $ _ SESSION ['image_filename']; 因此,您手動設置POST變量並將其用於縮略圖類假設它

+0

我想完全省略'$ _POST'。我只是不確定爲什麼代碼不會與會話變量一起工作。 – mileaminute

+0

也許是因爲縮略圖類本身處理帖子類型變量 – Osama

+0

可能,但我不確信,也沒有專業知識來解決這個問題。 – mileaminute