我遇到了一个问题,我正在尝试创建一个图像,在尝试运行imagejpeg函数实际将其写入文件之前,一切似乎都很好。
这是我的代码......
$name = $_POST['listeo-name'];
$email = $_POST['listeo-email'];
$uploadUrl = wp_upload_dir();
$dir = $uploadUrl['baseurl']."/";
$max_dimension = 800;
$imgVars = array(
"image" => $_FILES["listeo-image"]["name"],
"image_tmp" => $_FILES["listeo-image"]["tmp_name"],
"size" => $_FILES["listeo-image"]["size"],
"image_max_width" => 800,
"image_max_height" => 800
);
$valid_exts = array("jpg","jpeg","gif","png");
$ext = end(explode(".",strtolower(trim($_FILES["listeo-image"]["name"]))));
if ($imgVars["size"] <= 179200) {
if (in_array($ext,$valid_exts)) {
switch ($ext) {
case "jpg":
$image = imagecreatefromjpeg($imgVars["image_tmp"]);
break;
case "jpeg":
$image = imagecreatefromjpeg($imgVars["image_tmp"]);
break;
case "gif":
$image = imagecreatefromgif($imgVars["image_tmp"]);
break;
case "png":
$image = imagecreatefrompng($imgVars["image_tmp"]);
break;
}
list($width,$height) = getimagesize($imgVars["image_tmp"]);
if ($imgVars["image_max_width"] > $imgVars["image_max_height"]) {
if ($imgVars["image_max_width"] > $max_dimension) {
$newwidth = $max_dimension;
} else {
$newwidth = $imgVars["image_max_width"];
}
$newheight = ($newwidth / $width) * $height;
} else {
if ($imgVars["image_max_height"] > $max_dimension) {
$newheight = $max_dimension;
} else {
$newheight = $imgVars["image_max_height"];
}
$newwidth = ($newheight / $height) * $width;
}
$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
$rand = rand(1000,999999);
$filename = $dir.$rand.$imgVars["image"];
//This is where the problem is
imagejpeg($tmp,$filename,100);
echo $filename;
imagedestroy($image);
imagedestroy($tmp);
}
} else {
echo "File size too large. Max allowed file size is 175kb.";
}
基本上,我只是尝试将图像上传到Wordpress上传目录,以获取我正在创建的插件。该目录具有正确的权限,但是当我评论的那一行运行时,我仍然只是收到“没有这样的文件或目录”错误。
我该如何解决这个问题?
提前致谢!