Php 上载和调整图像大小问题
我试图做的是将图像上传到我的web服务器并调整其大小,这是一个相当简单的过程,我从未想过我会遇到问题,我的站点图像文件夹结构:Php 上载和调整图像大小问题,php,Php,我试图做的是将图像上传到我的web服务器并调整其大小,这是一个相当简单的过程,我从未想过我会遇到问题,我的站点图像文件夹结构: site.com/services/fs/ site.com/services/th/ 上面的结构是我的文件夹在我的站点文件中的方式,“fullsize”和“thumbnail”我可以上传完整大小的图像,没有问题,但在将缩略图保存到thumbnail文件夹中时遇到问题: 上传代码: <?php if (isset($_POST['submitNewServic
site.com/services/fs/
site.com/services/th/
上面的结构是我的文件夹在我的站点文件中的方式,“fullsize”和“thumbnail”我可以上传完整大小的图像,没有问题,但在将缩略图保存到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);
}
?>
我猜您发布的代码片段在不同的文件中?它们是否位于同一文件夹中?您好,Magnus,resize函数位于:site.com/includes/functions.php,上载代码位于:site.com/add-new-service.phpha您是否尝试使用上载文件夹的绝对路径而不是相对路径?您还应该进行一些调试,并查看调整大小代码失败的地方。检查不同步骤中包含的变量以及不同函数实际返回的结果。谢谢Magnus,似乎不需要添加“./”,我不明白为什么它位于目录中,但尽管如此:)相对路径与包含文件的顶部文件是相对的。如果包含包含文件等的文件,则使用相对路径很容易有点混乱。始终建议使用魔法常量\uuuuu DIR\uuuu
使用绝对路径。该常量将返回其写入的文件的绝对路径。