这里我试图将文本转换为png图像,用户在输入框中输入的文本和提交按钮我正在转换图像,图像转换正常工作但我无法下载转换后的图像png文件。下载下载转换.php文件的强制下载.png图像。
当我使用唯一header("Content-type: image/png");
时
而不是
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=").$im.("png ");
header("Content-Transfer-Encoding: binary ");
它在浏览器上显示图像。
查看转换后的图片快照。
(conversion.php)下面是文本到图像转换的示例代码。
<?php
### Declare this script will be displayed as a PNG image.
header("Content-type: image/png");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=").$im.("png ");
header("Content-Transfer-Encoding: binary ");
if (isset($_POST['convert'])) {
$username = $_POST['text'];
$fsize = $_POST['size'];
$fsize=200;
if(strlen($username)<=6){
####################### BEGIN USER EDITS #######################
$imagewidth = 1200;
$imageheight = 600;
$fontsize = $fsize;
$fontangle = "0";
$font = "ByzantineEmpire.ttf";
$text = $username;
$backgroundcolor = "003366";
$textcolor = "black";
######################## END USER EDITS ########################
### Convert HTML backgound color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $backgroundcolor, $bgrgb ) )
{$bgred = hexdec( $bgrgb[1] ); $bggreen = hexdec( $bgrgb[2] ); $bgblue = hexdec( $bgrgb[3] );}
### Convert HTML text color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
{$textred = hexdec( $textrgb[1] ); $textgreen = hexdec( $textrgb[2] ); $textblue = hexdec( $textrgb[3] );}
### Create image
$im = imagecreate( $imagewidth, $imageheight );
## Declare image's background color
$bgcolor = imagecolorallocate($im, $bgred,$bggreen,$bgblue);
### Declare image's text color
$fontcolor = imagecolorallocate($im, $textred,$textgreen,$textblue);
### Get exact dimensions of text string
$box = @imageTTFBbox($fontsize,$fontangle,$font,$text);
### Get width of text from dimensions
$textwidth = abs($box[4] - $box[0]);
### Get height of text from dimensions
$textheight = abs($box[5] - $box[1]);
### Get x-coordinate of centered text horizontally using length of the image and length of the text
$xcord = ($imagewidth/2)-($textwidth/2)-2;
### Get y-coordinate of centered text vertically using height of the image and height of the text
$ycord = ($imageheight/2)+($textheight/2);
### Declare completed image with colors, font, text, and text location
imagettftext ( $im, $fontsize, $fontangle, $xcord, $ycord, $fontcolor, $font, $text );
### Display completed image as PNG
imagepng($im);
### Close the image
imagedestroy($im);
}
}
?>
答案 0 :(得分:1)
尝试更改
header("Content-Disposition: attachment;filename=").$im.("png ");
到
header("Content-Disposition: attachment;filename=".$im.".png");
看起来您忘记声明 $ im 变量。
答案 1 :(得分:0)
以下是将文本转换为图像并从浏览器下载转换后图像的完整解决方案 有力地
1)创建html文件作为index.php,如下一个
<html>
<head>
<title>
</title>
</head>
<body>
<form name="FORM" method="post" action="convertimage.php">
Text:
<input type="text" name="text">
<br>
Size:
<select name="size">
<option value="12">12</option>
</select>
<br>
<input type="submit" name="convert" value="Generate stencil image">
</form>
</body>
</html>
2)将html文件创建为convertimage.php,如下图
<?php
### Declare this script will be displayed as a PNG image.
header("Content-type: image/png");
$im="Stencil";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=".$im.".png");
header("Content-Transfer-Encoding: binary ");
if (isset($_POST['convert'])) {
$username = $_POST['text'];
$fsize = $_POST['size'];
$fsize=200;
if(strlen($username)<=8){
####################### BEGIN USER EDITS #######################
$imagewidth = 1300;
$imageheight = 600;
$fontsize = $fsize;
$fontangle = "0";
$font = "ByzantineEmpire.ttf"; //Add Your ttf or otf font file here
$text = $username;
$backgroundcolor = "003366";
$textcolor = "black";
######################## END USER EDITS ########################
### Convert HTML backgound color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $backgroundcolor, $bgrgb ) )
{$bgred = hexdec( $bgrgb[1] ); $bggreen = hexdec( $bgrgb[2] ); $bgblue = hexdec( $bgrgb[3] );}
### Convert HTML text color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
{$textred = hexdec( $textrgb[1] ); $textgreen = hexdec( $textrgb[2] ); $textblue = hexdec( $textrgb[3] );}
### Create image
$im = imagecreate( $imagewidth, $imageheight );
### Declare image's background color
$bgcolor = imagecolorallocate($im, $bgred,$bggreen,$bgblue);
### Declare image's text color
$fontcolor = imagecolorallocate($im, $textred,$textgreen,$textblue);
### Get exact dimensions of text string
$box = @imageTTFBbox($fontsize,$fontangle,$font,$text);
### Get width of text from dimensions
$textwidth = abs($box[4] - $box[0]);
### Get height of text from dimensions
$textheight = abs($box[5] - $box[1]);
### Get x-coordinate of centered text horizontally using length of the image and length of the text
$xcord = ($imagewidth/2)-($textwidth/2)-2;
### Get y-coordinate of centered text vertically using height of the image and height of the text
$ycord = ($imageheight/2)+($textheight/2);
### Declare completed image with colors, font, text, and text location
imagettftext ( $im, $fontsize, $fontangle, $xcord, $ycord, $fontcolor, $font, $text );
### Display completed image as PNG
imagepng($im);
### Close the image
imagedestroy($im);
}
else{
header("Location: error.php");
exit();
}
}
?>