在php中优化图像时未找到此类文件或目录
我上传的图像和图像我正在优化,但我得到的错误没有这样的文件或目录。我得到下面的代码错误在php中优化图像时未找到此类文件或目录,php,image-optimization,Php,Image Optimization,我上传的图像和图像我正在优化,但我得到的错误没有这样的文件或目录。我得到下面的代码错误 $source_img =basename($_FILES["fileToUpload"]["name"]); 如果我写$source_img='assets/img/login bg.jpg';然后它就开始工作了。我想上传图像,然后优化它 我发现了错误 Warning: getimagesize(demoimg.JPG): failed to open stream: No such file o
$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:\xampp\htdocs\... on line 6
Notice: Undefined variable: image in C:\xampp\htdocs\... on line 17
Warning: imagejpeg() expects parameter 1 to be resource, null given in C:\xampp\htdocs\... on line 17
HTML
尝试以下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);
}
?>
希望能有帮助。祝你一切顺利 这可能是因为源中没有包含路径,只有basename。您可能需要将文件的整个路径发送到您的函数中,而不仅仅是basename。感谢您回复Rasclatt先生。实际上,我正在从HTML上传图像,并且我正在发送图像的唯一名称。如何发送路径?感谢您回复TutorialBee先生,为我工作。我还需要一个帮助,我们可以将图像大小设置为不超过2MB吗?请查看这些链接:谢谢你的帮助,TutorialsBee先生。向上投我的一票side@NarendraVerma非常感谢:
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
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);
}
?>