下载图像将在Safari的新窗口中打开

时间:2014-05-13 12:07:35

标签: php safari download

在我的项目Hexactly上,当在Safari中下载调色板时,它似乎是在新窗口中打开,而不是像在Chrome和Firefox等中那样下载。有人知道为什么会这样吗?

以下PHP用于从样本中下载调色板,我需要在Safari中下载它,但它似乎只是在新标签页中打开。

<?php
// Get the c from the $_GET 
$colours = $_GET['c'];

// Explode it by , making it like (blue, green, red, yellow)
$swatches = explode("|", $colours);

// Creates image 
$im = imagecreatetruecolor(120, 30);

// For each of the swatches $key will be $rgb_set
foreach ($swatches as $key => $rgb_set)
{
    // If it's empty then end the program
    if ($rgb_set=="") break;

    // If not let's then again create the list of colour values R G B
    list($r, $g, $b) = explode(",", $rgb_set);

    // Multiply the value of $key by 24
    $x_pos = (24 * $key);

    // Value of swatch is now the $im with R G B values
    $swatch = imagecolorallocate($im, $r, $g, $b);

    // Size of the rectangle with the $swatch colour selected
    imagefilledrectangle($im, $x_pos, 0, $x_pos+24, 30, $swatch);
}

// Sets the content type header
header('Content-Type: image/png');
readfile('path/to/myimage.png');

// Saves the image
imagepng($im); // Creates the Image
imagedestroy($im); // Clear the instance to make it ready for the next image
?>

1 个答案:

答案 0 :(得分:0)

readfile之前添加以下行并尝试

header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"myimage.png\"");