调用 move_uploaded_file() 后无法使用 getImageSize
Can't use getImageSize after calling move_uploaded_file()
我试图在将全尺寸图像移动到特定文件夹后创建缩略图。如果在 if($this->moveImages($rawData, $uniqueID))
之前调用该函数,则它可以正常工作,但是当我将它放在 if 语句中时,它只是 returns 以下错误:
Warning: getimagesize(/Applications/MAMP/tmp/php/phpax7wRA): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 66
Warning: Division by zero in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 70
Warning: imagecreatetruecolor(): Invalid image dimensions in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 77
Warning: imagecreatefromjpeg(/Applications/MAMP/tmp/php/phpax7wRA): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 83
Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 91
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 92
当返回 var_dump
传递给它的值时 returns 看起来像是正确的数据,但它似乎无法访问它。我将post下面有问题的功能,感谢您的帮助。
提交函数(提交表单/启动函数时调用):
public function submitCoil ($data) {
$db = Database::getInstance();
$uniqueID = $this->hash();
$rawData = $this->interpretData($data, $uniqueID);
$queryData = $this->interpretData($data, $uniqueID)['queryData'];
$imageData = $this->interpretData($data, $uniqueID)['imageData'];
if ($this->nullCheck($data)) {
mkdir('coils/' . $uniqueID);
if($this->moveImages($rawData, $uniqueID)) {
//SUPRESS SLEEP ON UPLOADsleep(5);
for($i = 0; $i < count($data['image_name']); $i++) {
$this->createThumbnail($imageData['image_name'][$i], $imageData['image_tmp_name'][$i], $imageData['image_type'][$i], $uniqueID, $i);
}
$db->insertFew("coils", array_keys($queryData), array_values($queryData));
echo "<script>alert('Your coil was uploaded!');</script>";
}
} else {
echo "Please fill in all fields";
}
}
移动全尺寸图片:
private function moveImages ($data, $uniqueID) {
$queryData = $data['queryData'];
$data = $data['imageData'];
$return = true;
$allowedTypes = array('image/jpg', 'image/png', 'image/jpeg');
if ($data['image_name'][0] != "") {
for($i = 0; $i < count($data['image_name']); $i++) {
if (!in_array($data['image_type'][$i], $allowedTypes)) {
$return = false;
echo "<script>alert('Files must be either jpeg or png');</script>";
} else {
$ext = pathinfo($data['image_name'][$i], PATHINFO_EXTENSION);
move_uploaded_file($data['image_tmp_name'][$i], $queryData['images'] . $i . '.' . $ext);
}
}
} else {
$return = true;
}
return $return;
}
Create/move缩略图函数:
public function createThumbnail ($fileName, $tmpName, $fileType, $location, $imageNumber) {
$name = $fileName;
$image = $tmpName;
$file_type = $fileType;
var_dump($name);
var_dump($image);
$image_size = getimagesize($image);
$image_width = $image_size[0] . "<br>";
$image_height = $image_size[1];
$new_size = ($image_width + $image_height) / ($image_width * ($image_height / 1000));
$new_width = $image_width * $new_size . "<br>";
$new_height = $image_height * $new_size;
$allowed = array("image/jpeg", "image/png");
if(in_array($file_type, $allowed)) {
$new_image = imagecreatetruecolor($new_width, $new_height);
//Switch on filetype
switch($file_type) {
case "image/jpeg":
$old_image = imagecreatefromjpeg($image);
break;
case "image/png":
$old_image = imagecreatefrompng($image);
break;
}
imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($new_image, 'coils/' . $location . '/' . 'thumb-' . $imageNumber . '.jpg');
}
}
我的建议:
在 moveImages()
中的 move_uploaded_file() 之前调用 createThumbnail()
我认为是因为 move_uploaded 文件移动并删除了临时文件,所以您遇到了错误
我试图在将全尺寸图像移动到特定文件夹后创建缩略图。如果在 if($this->moveImages($rawData, $uniqueID))
之前调用该函数,则它可以正常工作,但是当我将它放在 if 语句中时,它只是 returns 以下错误:
Warning: getimagesize(/Applications/MAMP/tmp/php/phpax7wRA): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 66
Warning: Division by zero in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 70
Warning: imagecreatetruecolor(): Invalid image dimensions in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 77
Warning: imagecreatefromjpeg(/Applications/MAMP/tmp/php/phpax7wRA): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 83
Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 91
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 92
当返回 var_dump
传递给它的值时 returns 看起来像是正确的数据,但它似乎无法访问它。我将post下面有问题的功能,感谢您的帮助。
提交函数(提交表单/启动函数时调用):
public function submitCoil ($data) {
$db = Database::getInstance();
$uniqueID = $this->hash();
$rawData = $this->interpretData($data, $uniqueID);
$queryData = $this->interpretData($data, $uniqueID)['queryData'];
$imageData = $this->interpretData($data, $uniqueID)['imageData'];
if ($this->nullCheck($data)) {
mkdir('coils/' . $uniqueID);
if($this->moveImages($rawData, $uniqueID)) {
//SUPRESS SLEEP ON UPLOADsleep(5);
for($i = 0; $i < count($data['image_name']); $i++) {
$this->createThumbnail($imageData['image_name'][$i], $imageData['image_tmp_name'][$i], $imageData['image_type'][$i], $uniqueID, $i);
}
$db->insertFew("coils", array_keys($queryData), array_values($queryData));
echo "<script>alert('Your coil was uploaded!');</script>";
}
} else {
echo "Please fill in all fields";
}
}
移动全尺寸图片:
private function moveImages ($data, $uniqueID) {
$queryData = $data['queryData'];
$data = $data['imageData'];
$return = true;
$allowedTypes = array('image/jpg', 'image/png', 'image/jpeg');
if ($data['image_name'][0] != "") {
for($i = 0; $i < count($data['image_name']); $i++) {
if (!in_array($data['image_type'][$i], $allowedTypes)) {
$return = false;
echo "<script>alert('Files must be either jpeg or png');</script>";
} else {
$ext = pathinfo($data['image_name'][$i], PATHINFO_EXTENSION);
move_uploaded_file($data['image_tmp_name'][$i], $queryData['images'] . $i . '.' . $ext);
}
}
} else {
$return = true;
}
return $return;
}
Create/move缩略图函数:
public function createThumbnail ($fileName, $tmpName, $fileType, $location, $imageNumber) {
$name = $fileName;
$image = $tmpName;
$file_type = $fileType;
var_dump($name);
var_dump($image);
$image_size = getimagesize($image);
$image_width = $image_size[0] . "<br>";
$image_height = $image_size[1];
$new_size = ($image_width + $image_height) / ($image_width * ($image_height / 1000));
$new_width = $image_width * $new_size . "<br>";
$new_height = $image_height * $new_size;
$allowed = array("image/jpeg", "image/png");
if(in_array($file_type, $allowed)) {
$new_image = imagecreatetruecolor($new_width, $new_height);
//Switch on filetype
switch($file_type) {
case "image/jpeg":
$old_image = imagecreatefromjpeg($image);
break;
case "image/png":
$old_image = imagecreatefrompng($image);
break;
}
imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($new_image, 'coils/' . $location . '/' . 'thumb-' . $imageNumber . '.jpg');
}
}
我的建议:
在 moveImages()
中的 move_uploaded_file() 之前调用 createThumbnail()我认为是因为 move_uploaded 文件移动并删除了临时文件,所以您遇到了错误