下载PHP渲染图像-无法在Android手机上打开

下载PHP渲染图像-无法在Android手机上打开,php,image,image-processing,jpeg,image-rendering,Php,Image,Image Processing,Jpeg,Image Rendering,我正在使用脚本: 1.在我的服务器上渲染/保存图像。 2.将保存的图像下载到浏览器 因此,图像不会打开,而是由浏览器下载。工作完美 但当我在安卓手机上下载图像时,下载后我无法从浏览器/文件查看器打开图像。但是,在我的图库中,图像是存在的,我可以打开它。看起来浏览器(chrome)无法将下载的文件识别为图像 希望有人有想法。也许是有文件头的 我使用SubmitForm按钮调用脚本,然后文件被下载,而您保持在同一页面上 <?php // ####################### Pa

我正在使用脚本: 1.在我的服务器上渲染/保存图像。 2.将保存的图像下载到浏览器

因此,图像不会打开,而是由浏览器下载。工作完美

但当我在安卓手机上下载图像时,下载后我无法从浏览器/文件查看器打开图像。但是,在我的图库中,图像是存在的,我可以打开它。看起来浏览器(chrome)无法将下载的文件识别为图像

希望有人有想法。也许是有文件头的

我使用SubmitForm按钮调用脚本,然后文件被下载,而您保持在同一页面上

<?php

// #######################  Part 1, Render the image #######################

//content type
 header('Content-type: image/jpeg'); 
//font
$font = 'assets/medium.otf';
//font size
$font_size = 16;
//image width
$width = 400;
//text margin
$margin = 15;

//text
 $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';

//explode text by words
$text_a = explode(' ', $text);
$text_new = '';
foreach($text_a as $word){
    //Create a new text, add the word, and calculate the parameters of the text
    $box = imagettfbbox($font_size, 0, $font, $text_new.' '.$word);
    //if the line fits to the specified width, then add the word with a space, if not then add word with new line
    if($box[2] > $width - $margin*2){
        $text_new .= "\n".$word;
    } else {
        $text_new .= " ".$word;
    }
}
//trip spaces
$text_new = trim($text_new);
//new text box parameters
$box = imagettfbbox($font_size, 0, $font, $text_new);
//new text height
$height = $box[1] + $font_size + $margin * 2;

//create image
$im = imagecreatetruecolor($width, $height);

//create colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
//color image
imagefilledrectangle($im, 0, 0, $width, $height, $white);

//add text to image
imagettftext($im, $font_size, 0, $margin, $font_size+$margin, $black, $font, $text_new);

//return image
//imagejpeg($im);
  imagejpeg($im,'images/text/name.jpg');
//frees any memory associated with image
imagedestroy($im);



// #######################  Part 2, download the image #######################

$file = 'images/text/name.jpg';

header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$ext = pathinfo($file, PATHINFO_EXTENSION);
$basename = pathinfo($file, PATHINFO_BASENAME);

header("Content-type: application/".$ext);
// tell file size
header('Content-length: '.filesize($file));
// set file name
header("Content-Disposition: attachment; filename=\"$basename\"");
readfile($file);

?>

在脚本开始时,您说内容类型是
image/jpeg
,然后在脚本结束时,您说内容类型是
application/“$ext

出于您的目的,您需要将
内容类型
设置为
image/$etc
,以便浏览器将其解释为正确的MIME类型

此外,根据您从何处获取此图像文件,我会比只检查扩展名更仔细。至少交叉检查文件内部并将其与扩展名交叉引用。使用此选项将从用户处获取的图像复制到空白图像中可能是一个好主意。您可以使用此选项删除无关的元数据ta并防止某些类型的漏洞攻击


谢谢!这就是问题所在
header("Content-type: application/".$ext);