我正在上传图像,并且我正在优化的图像,但我会收到错误"否此类文件或目录"。我在下面的代码上遇到错误。
$source_img =basename($_FILES["fileToUpload"]["name"]);
如果我编写$source_img = 'assets/img/login-bg.jpg';
,则它正在工作。我想上传图像然后优化。
我正在遇到错误
Warning: getimagesize(demoimg.JPG): failed to open stream: No such file or directory in C:xampphtdocs... on line 6
Notice: Undefined variable: image in C:xampphtdocs... on line 17
Warning: imagejpeg() expects parameter 1 to be resource, null given in C:xampphtdocs... on line 17
html
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
php
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
if(isset($_POST['submit'])){
$source_img =basename($_FILES["fileToUpload"]["name"]);
$temp = explode(".", $source_img);
$newfilename = round(microtime(true)) . '.' . end($temp);
$destination_img= "assets/$newfilename";
$d = compress($source_img, $destination_img, 90);
}
尝试此PHP代码:
<?php
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
if(isset($_POST['submit'])){
$source_img =$_FILES["fileToUpload"]["tmp_name"];
$source_img_name =basename($_FILES["fileToUpload"]["name"]);
$temp = explode(".", $source_img_name);
$newfilename = round(microtime(true)) . '.' . end($temp);
$destination_img= "assets/$newfilename";
$d = compress($source_img, $destination_img, 90);
}
?>
希望它有帮助。最好!