我要做的是将图像上传并调整大小到我的网络服务器,这是一个相当简单的过程,我从未想过会遇到问题,我的网站图片文件夹结构:
site.com/services/fs/
site.com/services/th/
以上结构是我的文件夹在我的网站文件中的方式,“fullsize”和“thumbnail”我可以上传完整尺寸的图片,没有任何问题,但我在缩略图文件夹中保存缩略图时遇到了问题:
上传代码:
<?php
if (isset($_POST['submitNewService'])) {
// TRY/CATCH //
try {
// IMAGE UPLOAD / RESIZE //
$original = $_FILES['service_image']['name'];
$maxwidth = '500';
$maxheight = '500';
$random_number = rand(0, 999999);
$allowed_image_types = [
'image/gif',
'image/jpg',
'image/x-png',
'image/jpeg',
'image/pjpeg',
'image/bmp'
];
$imageRestrictions = @getimagesize($_FILES['service_image']['tmp_name']);
$renamedImage = "service-" . $random_number . '.' . substr($_FILES['service_image']['name'], strtolower(strlen($_FILES['service_image']['name'])) - 3, 3);
if (($imageRestrictions[0] >= $maxwidth) || ($imageRestrictions[1] >= $maxheight)) {
stderr(sprintf('Sorry that <b>image</b> is bigger than <b>%s</b>x<b>%s</b>!', $maxwidth, $maxheight));
}
if (!empty($_FILES['service_image']['name'])) {
if (!in_array($_FILES['service_image']['type'], $allowed_image_types)) {
stderr('The <b>uploaded</b> image does not appear to be a valid file!');
}
$uploadpath = '../services/fs/' . $renamedImage;
move_uploaded_file($_FILES['service_image']['tmp_name'], $uploadpath);
}
$imgResized = resize_image($uploadpath, $renamedImage);
// IMAGE UPLOAD / RESIZE //
DB::getInstance()->insert(
'services',
[
'service_user_id' => $globId,
'service_name' => $_POST['service_name'],
'service_body' => nl2br($_POST['service_body']),
'service_cost' => $_POST['service_cost'],
'service_category' => $_POST['service_category'],
'service_image' => $renamedImage,
'service_thumb_image' => $imgResized,
'service_date' => date('Y-m-d H:i:s')
]);
stdmsg("Your service [ <b>{$_POST['service_name']}</b> ] has been added successfully!");
} catch (Exception $e) {
stderr($e->getMessage());
}
}
?>
调整功能:
<?php
function resize_image($uploadDirectory, $newFileName) {
$original_image = $uploadDirectory;
$ext = substr($original_image, strrpos($original_image, '.') + 1);
$canvas_width = 480;
$canvas_height = 250;
$canvas = imagecreatetruecolor($canvas_width, $canvas_height);
$white_background = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $white_background);
list($image_width, $image_height) = getimagesize($uploadDirectory);
$ratio = $image_width / $image_height;
if ($ratio > 1) {
$new_image_width = 65;
$new_image_height = 65 / $ratio;
} else {
$new_image_width = (float) 65 * $ratio;
$new_image_height = 65;
}
if ($ext == "jpg") {
$original_image = imagecreatefromjpeg($original_image);
}
if ($ext == "gif") {
$original_image = imagecreatefromgif($original_image);
}
imagecopyresampled($canvas, $original_image, 0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height);
$new_thumbnail_name = "thumb-$newFileName";
if ($ext == "jpg") {
imagejpeg($canvas, "../services/th/{$new_thumbnail_name}", 100);
return $new_thumbnail_name;
}
if ($ext == "gif") {
imagegif($canvas, "../services/th/{$new_thumbnail_name}", 100);
return $new_thumbnail_name;
}
imagedestroy($original_image);
imagedestroy($canvas);
}
?>
调整大小代码我从几年前工作得很好的另一个项目中获取,我只是无法将缩略图保存在缩略图文件夹中,是否有一些我错过的小文件,文件路径对我来说很好,任何帮助将不胜感激。