【问题标题】:Image type becomes not supported after downloading it图片类型下载后不支持
【发布时间】: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


【解决方案1】:

这里有两个错误:

  1. 如果您将imagejpeg()imagepng() 与实际目标路径($file 参数)一起使用,则函数不会向浏览器输出任何内容。
  2. 标题应该放在内容之前。

试试这个:

<?php

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);
    }

    // If the file is created successfully and can be opened.
    if (($fh = fopen($dest, 'r')) !== false) {
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=\"" . basename($dest) . "\";");

        // open the content and stream to browser
        $output = fopen('php://output', 'w');
        stream_copy_to_stream($fh, $output);
    } else {
        throw new \Exception('unable to open the file: ' . $dest);
    }
}


【讨论】:

  • 即使使用此代码,我也会收到与不支持此类文件相同的错误。并且文件是PNG。我不断收到这不是一个有效的位图文件。从我网站的上传文件夹中打开了相同的文件,下载的文件给出了错误
猜你喜欢
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 2017-08-09
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多