Add following lines before readfile
and try,
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"myimage.png\"");
题
On my project Hexactly, when downloading a palette in Safari it appears to be opening in a new window rather than downloading like it does in Chrome and Firefox etc. Hs anybody any idea why this is happening?
The following PHP is used to download a palette from the swatch, I need it to download in Safari also but it appears to only be opening in a new tab.
<?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
?>
解决方案
Add following lines before readfile
and try,
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"myimage.png\"");