PHP强制下载在MIUI浏览器中不起作用

时间:2018-10-27 13:50:39

标签: php php-gd

我在MI移动默认浏览器中有问题。我编写了一个代码,该代码使用png函数生成php-gd图像,然后强制下载它。 该代码适用于chrome,Mozilla,但无法在Mi浏览器中运行。 在MI浏览器中单击“下载”按钮时,将下载具有生成图像的文件名的文件(即downloadKey.php),而不保存为.png扩展名。

我使用的代码是:

//Image init
$im = @imagecreatetruecolor(960, 1280)or die('Cannot Initialize new GD image stream');

//Colors init
//Background color
$white = imagecolorallocate($im, 255, 255, 255);

//$black = imagecolorallocate($im, 0, 0, 0);
//$blue = imagecolorallocate($im, 0, 0, 255);
$red = imagecolorallocate($im, 231, 76, 60);

//Foreground color
$color = $red;
//$color = imagecolorallocate($im, 231, 76, 60);

//Font init
$dir = $_SERVER['DOCUMENT_ROOT'] . '/myproject';
$font = $dir . '/fonts/timesbd.ttf';

//Project title
$heading = "My Project";

$height = 0;
$l = 0;
$m = 1;


$encoded = $_SESSION['encodedKey'];
$decryptedArray = unserialize(urldecode($encoded));

//Image creation
imagefilledrectangle($im, 0, 0, 959, 1279, $white);
imagettftext($im, 70, 0, 275, 150, $color, $font, $heading);

$arrayIndex = 0;
for ($i = 0; $i < 8; $i++) {

    for ($j = 0, $width = 0; $j < 8; $j++, $width = $width + 100) {
        $emoji = $key->getPattern($decryptedArray[$arrayIndex]);
        $png = imagecreatefrompng($emoji);
        imagecopyresampled($im, $png, 84 + $width, 250 + $height, 0, 0, 64, 64, 64, 64);
        $arrayIndex = $arrayIndex + 1;
    }

    for ($k = 0, $width = 0; $k < 8; $k++, $width = $width + 100) {
        imagettftext($im, 24, 0, 100 + $width, 340 + $height, $color, $font, sprintf("%02d", $m++));
    }

    $height += 120;
}

header("Content-Type: image/png");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: Attachment; filename="test.png"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');   

imagepng($im);
imagedestroy($im);

文件名是downloadKey.php

0 个答案:

没有答案