【发布时间】:2022-01-18 07:33:27
【问题描述】:
我正在用 php.ini 创建一个图像压缩页面。我允许用户上传他们的图像,并且允许用户设置首选输出类型(jpg 或 png)和 0 到 100 之间的质量。使用此功能压缩图像
function compress_image($source_url,$dest,$quality,$type){
$info=getimagesize($source_url);
if ($info['mime'] == 'image/jpeg')
{
$image = imagecreatefromjpeg($source_url);
}
elseif ($info['mime'] == 'image/gif')
{
$image = imagecreatefromgif($source_url);
}
elseif ($info['mime'] == 'image/png')
{
$image = imagecreatefrompng($source_url);
}
if($type=="jpg"){
imagejpeg($image, $dest, $quality);
}else{
$quality=$quality/10;
imagepng($image,$dest,$quality);
}
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".basename($dest)."\";" );
}
这是获取图像并将其提供给 compress_image 函数的代码。
if(isset($_REQUEST['submit_form'])){
foreach($_FILES['image_file']['tmp_name'] as $key=> $value){
$file_name=$_FILES['image_file']['name'][$key];
$file_type=$_FILES['image_file']['type'][$key];
$temp_name=$_FILES['image_file']['tmp_name'][$key];
$file_size=$_FILES['image_file']['size'][$key];
// $quality=$_REQUEST['quality'];
$quality=50;
// $type=$_REQUEST['type']==1?"png":"jpg";
$type="jpg";
compress_image($temp_name,'./upload/'.$file_name,$quality,$type);
}
}
当我在 htdocs/website/upload 文件夹中看到图像时,它实际上被压缩了。但是,当我使用标头函数从同一目录下载图像时,我收到了it appears that we do not support this format 的错误。使用相同的软件打开相同的图像并且尺寸也减小了。我该怎么做才能使图像可下载且有用。
【问题讨论】:
-
图片内容在标题之后。
-
我试过了,但还是同样的问题
-
如果
$dest不是null,则没有图像发送到浏览器。 -
我对标头及其概念完全陌生,您能详细说明一下吗?
-
我当然会这样做
标签: php http-headers