0

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.

click here demo link

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. converted image from text

(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);
}
}
?>
Sanjay Nakate
  • 2,020
  • 6
  • 39
  • 74
  • I have to ask the obvious question: Is PHP running on the server? Can you run a "hello world" script on it? When you said "the image converting properly", how were you able to verify that? – TecBrat Apr 18 '14 at 11:53
  • if i use only header("Content-type: image/png"); at the header image dispyling on browser – Sanjay Nakate Apr 18 '14 at 12:00
  • 1
    @SanjayNakate humm, try changing 'header("Content-Disposition: attachment;filename=").$im.("png ");' to `header("Content-Disposition: attachment;filename=".$im.".png");` – Himal Apr 18 '14 at 12:33
  • 1
    @SanjayNakate BTW, looks like you forgot to declare the $im variable as well. – Himal Apr 18 '14 at 12:41
  • @Himal you are right can you put this as an answer..all code working properly with force download. – Sanjay Nakate Apr 19 '14 at 04:58
  • @SanjayNakate.glad to hear that.i've posted that as an answer. – Himal Apr 19 '14 at 09:34

2 Answers2

1

As per OP's Request

try changing

header("Content-Disposition: attachment;filename=").$im.("png ");

to

header("Content-Disposition: attachment;filename=".$im.".png");

looks like you forgot to declare the $im variable as well.

Community
  • 1
  • 1
Himal
  • 1,351
  • 3
  • 13
  • 28
0

Here is total solution for convert text to image and download converted image from browser forcefully

1) Create html file as index.php like bellow one

 <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) Create html file as convertimage.php like bellow one

<?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();
}
}
?>
Sanjay Nakate
  • 2,020
  • 6
  • 39
  • 74