这里我使用php在数据库中上传图像。我想在将其保存到各自的文件夹中之前实现压缩。
问题:
我面对的是它适用于目录中已存在的图像,而不是目录外的任何随机图像(images /)
任何帮助纠正代码表示赞赏。
<?php
if(isset($_POST['submit']))
{
if (isset ($_FILES['new_image']))
{
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "images/".$imagename;
$imagepath = $imagename;
$source_url=$source;
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg')
{
$image = imagecreatefromjpeg($source_url);
imagejpeg($image, $destination_url, $quality);
}elseif ($info['mime'] == 'image/gif'){
$image = imagecreatefromgif($source_url);
imagegif($image, $destination_url, $quality);
}
elseif ($info['mime'] == 'image/png')
{$image = imagecreatefrompng($source_url);
imagepng($image, $destination_url, $quality);
}
return $destination_url;
}
$source_photo = "images/" . $imagepath;
$dest_photo = ""images/sml_" . $imagepath;
$d = compress_image($source_photo, $dest_photo,70);
echo '
<div style="float:left;margin-right:10px">
<img src="'.$source_photo.'" alt="" />
<br />'.filesize($source_photo).' Bytes
</div>
<div style="float:left;">
<img src="'.$dest_photo.'" alt="" />
<br />'.filesize($dest_photo).' Bytes
</div>
';
}}
?>
答案 0 :(得分:0)
您的代码似乎没问题。它说文件夹不存在,
答案 1 :(得分:0)
这里有功能 move_uploaded_file($ source,$ target); 可以使用它将文件传输到相应的文件夹,问题解决了。