0

注意:在你受到任何负面反应的诱惑之前,请好好把握问题和我的努力。如果我的方法在任何地方有错误,请给我一个纠正它的机会。

我在 wordpress 上运行的网站上有一个小 php 脚本。它基本上调整上传图像的大小,并强制提供调整大小的图像作为下载提示。请注意,我在相关页面上没有任何下载链接。该脚本运行良好,可以调整大小并创建图像和所有内容,除了在 Windows 照片查看器等通用 Windows 软件中打开下载的图像时显示“Windows 照片查看器无法打开此图片,因为它不支持此文件格式”,但是当我在 Photoshop 中打开它时,它显示得非常好。我也在 Firefox 浏览器 24.0 中打开了下载的图像,其中显示“无法显示图像文件 /path/to/file/”,因为它包含错误。

这是相关的 php 片段,以便了解发生了什么。如果需要,我将编写整个代码。如果您能在我进行自己的研究工作时指出代码中的错误,那就太好了。

// create a new image resource to be utilized by imagejpeg    
$new = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

         $filename = uniqid();
         $file = 'uploads/' .$filename. '.jpeg';    
         imagejpeg($new, $file, 100);
         $size = filesize($file); 

         header("Content-Type: application/force-download; name=\"" . basename($file). "\""); 
         header("Content-Transfer-Encoding: binary"); 
         header("Content-Length: $size"); 
         header("Content-Disposition: attachment; filename=\"" . basename($file) . "\""); 
         header("Expires: 0"); 
         header("Cache-Control: no-cache, must-revalidate"); 
         header("Pragma: no-cache"); 
         readfile($file);

努力更新

第一次更新:我阅读了这个线程,并根据它的建议尝试在ob_end_clean()之前添加readfile($file);并注释掉 $size,仍然得到损坏的图像。

第二次更新:我也尝试了下面给出的代码,正如这里的解决方案中所建议的那样。除此之外,请记住,在阅读了这里的其他众多主题后,我也尝试了一些小的更正,我仍在寻找。

header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

最后的努力让我觉得我在原始代码中分配图像扩展名的方式可能有问题。目前我正在分配它$file = 'uploads/' .$filename. '.jpeg';可能是错误的。如果我是我应该如何纠正它?

4

1 回答 1

0

以防万一有人无法找到隐藏在评论中的解决方案。这是我解决问题的方法。只需从包含下载代码的 php 页面中删除所有和任何类型的 html。

于 2013-11-03T14:12:50.953 回答