【问题标题】:PHP imagejpeg - Trigger DownloadPHP imagejpeg - 触发器下载
【发布时间】:2018-07-14 16:37:56
【问题描述】:

我正在使用 PHP imagejpeg 创建一个像这样的简单图像..

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);

这是有效的,但我希望它在浏览器内自动触发下载而不是显示图像。

我该怎么做,我需要设置标题吗?

【问题讨论】:

标签: php imagejpeg


【解决方案1】:

我相信你至少需要 3 个标题:

header("Content-Type: image/jpeg"); // Content type
header("Content-Disposition: attachment; filename=file-name.jpg"); // Content deposition
header("Content-Length: $fileSize"); // YOUR FILE SIZE

否则,下载将无法在 Firefox 中进行。

【讨论】:

    【解决方案2】:

    您所要做的就是设置一个额外的标题:

    // Create a blank image and add some text
    $im = imagecreatetruecolor(120, 20);
    $text_color = imagecolorallocate($im, 233, 14, 91);
    imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
    
    // Set the content type header - in this case image/jpeg
    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename=file-name.jpg'); // This will tell the browser to download it
    
    // Output the image
    imagejpeg($im);
    
    // Free up memory
    imagedestroy($im);
    

    Here 是文档

    【讨论】:

    • 对我没有任何作用
    • 你用的是什么浏览器?我用你的代码测试了它,它工作正常,我用我测试过的代码更新了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多