我正在研究一個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,這是一直阻礙我的障礙之一。也許答案顯而易見,但我當然處於停滯狀態。
所有的輸入讚賞,謝謝!
異常的錯誤消息是什麼? – leepowers
它引發了三類內建的錯誤:'無法打開image_1.jpg.','image_1.jpg看起來不是圖像.'和'無法確定文件的大小「。 – mileaminute