我是OOP和I'm using this tutorial to resize images的新手,但我无法让它发挥作用。我正在使用DropzoneJS上传图片。
我的代码如下所示:
include_once '../core/init.php';
$username = $user['username'];
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '../users';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds . $storeFolder . $ds . $username . $ds;
$tempName = $_FILES['file']['name'];
$kaboom = explode(".", $tempName);
$fileExt = end($kaboom);
$filename = date("DMj-Y-G-i-s-")."".rand(1000,9999).".".$fileExt;
$targetFile = $targetPath. $filename;
move_uploaded_file($tempFile,$targetFile);
include("image_resize.php");
$resizeObj = new resize($targetPath . $targetFile);
$resizeObj -> resizeImage(550, 550, 'crop');
$resizeObj -> saveImage($targetPath.'medium-'.$targetFile, 100);
$users->upload_image($username, $filename, $time, $ip);
}
什么有效:图片已成功上传并移至正确的目录。 $user->upload_image
工作并将新图像数据插入我的数据库。
什么行不通: image_resize
不起作用。本教程中的演示工作正常,但当我为sample.jpg
和targetPath
更改targetFile
时,没有任何反应。我正在尝试创建一个550x550像素的复制图像,并在文件名之前添加了medium-
,但没有发生任何事情。
答案 0 :(得分:1)
您的文件路径中存在一些不一致之处,此处您将$targetPath
添加到$targetFile
:
$targetFile = $targetPath. $filename;
然后使用$targetFile
上传文件 - 该文件已包含$targetPath
:
move_uploaded_file($tempFile,$targetFile);
那么当你尝试调整它的大小时,你就会在$targetPath
变量之前再次:
$resizeObj = new resize($targetPath . $targetFile);
尝试不使用$targetPath
$resizeObj = new resize($targetFile);
编辑注意到您在保存缩略图时再次使用$targetPath
,因此在这种情况下,您实际上需要将这两个变量分开,只在必要时合并它们 - 因为当您保存时,您将$targetPath
,然后是medium-
,然后是已包含$targetPath
的文件名。
保持路径和文件名分开,然后尝试:
move_uploaded_file($tempFile, $targetPath . $filename);
include("image_resize.php");
$resizeObj = new resize($targetPath . $filename);
$resizeObj -> resizeImage(550, 550, 'crop');
$resizeObj -> saveImage($targetPath . 'medium-' . $filename, 100);