Here i am trying to convert text to png image ,text entered by the user in input box and on submit button i am converting image ,the image converting properly working but i am not able download that image png file which is converted.The force download downloading the conversion.php file insted of .png image.
When i use the only header("Content-type: image/png");
instead of
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 ");
Its display the image on browser.
Check out converted image snap shot.
(conversion.php)Below is the sample code for text to image conversion.
<?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);
}
}
?>