Quand je génère une image, je voudrais que PHP force le download de cette image générée...
Voici mon code (qui ne force pas le download mais qui la génère...) :
<?php
header ("Content-type: image/png");
$mot_de_passe = $_GET['password'];
$save ='./cartes/'.$mot_de_passe.'.png';
if( !file_exists('./cartes/'.$mot_de_passe.'.png') ){
$image = imagecreatefrompng('./cartes/carte_vierge.png');
$rouge = imagecolorallocate($image, 255, 0, 0);
imagettftext($image, 25, 0, 195, 140, $rouge, 'corbel.ttf', $mot_de_passe);
imagepng($image);
imagepng($image, $save);
imagedestroy($image);
} else {
readfile('./cartes/'.$mot_de_passe.'.png');
}
header("Content-disposition: attachment; filename=$save");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: image/png\n"); // Surtout ne pas enlever le \n
header("Content-Length: ".filesize($save));
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
header("Expires: 0");
?>
Merci à vous !EDIT : en bidouillant encore, j'ai fini par trouver...
<?php
header ("Content-type: image/png");
$mot_de_passe = $_GET['password'];
$save ='./cartes/'.$mot_de_passe.'.png';
// Hearders pour le téléchargement
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($save) );
header('Accept-Ranges: bytes');
if( !file_exists('./cartes/'.$mot_de_passe.'.png') ){
$image = imagecreatefrompng('./cartes/carte_vierge.png');
$rouge = imagecolorallocate($image, 255, 0, 0);
imagettftext($image, 25, 0, 195, 140, $rouge, 'corbel.ttf', $mot_de_passe);
imagepng($image);
imagepng($image, $save);
imagedestroy($image);
} else {
readfile('./cartes/'.$mot_de_passe.'.png');
}
// Telechargement
header('Content-Length: '.filesize($save) );
?>