0

I have this little problem with my script since I'm able to output my image and "save it as" locally but I cannot do it when I try with the document uploaded to my server, what I need is that once I click submit the browser alerts me about what I want to do with the image, save it or open it, this is my script:

<?php $nombre=$_POST['nombre'];
    //Set the Content Type
header('Content-type: image/png'); 


    // Create Image From Existing File
    $jpg_image = imagecreatefromjpeg('fabian.jpg');
        // get image dimensions
 list($img_width, $img_height,,) = getimagesize("fabian.jpg");

    // Allocate A Color For The Text
    $white = imagecolorallocate($jpg_image, 255, 255, 255);

    // Set Path to Font File
    $font_path = '/fabian.ttf';

    // Set Text to Be Printed On Image
    $text =strtoupper($nombre);

    // Print Text On Image
    //imagettftext($jpg_image, 75, 0, 50, 400, $white, $font_path, $text);



  // find font-size for $txt_width = 80% of $img_width...
  $font_size = 1; 
  $txt_max_width = intval(0.8 * $img_width);    

  do {

  $font_size++;
  $p = imagettfbbox($font_size,0,$font_path,$text);
  $txt_width=$p[2]-$p[0];
  // $txt_height=$p[1]-$p[7]; // just in case you need it

 } while ($txt_width <= $txt_max_width);

  // now center text...
 $y = $img_height * 0.9 ;// baseline of text at 90% of $img_height
  $x = ($img_width - $txt_width) / 2;

  imagettftext($jpg_image, $font_size, 0, $x, $y, $white, $font_path, $text);



    // Send Image to Browser and Save
 imagepng($jpg_image);

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

// Clear Memory
    imagedestroy($jpg_image);

   ?>

This sends the image to the browser as draw.php but I want it to be saved as snivel.png when the browser prompts me to save the file, this works only locally but not in my server.

brasofilo
  • 23,940
  • 15
  • 86
  • 168
i'llbdevsomeday
  • 151
  • 1
  • 4
  • 11
  • 1
    header() must go first. Enable error logging and take a look in your error log, PHP tells you where you do wrong and where that started. – hakre Apr 16 '13 at 23:30
  • As @hakre says, you have to send header before anything is outputting. Secondly, you can append other header like the content type `header('Content-Type: image/png');` just to be sure that your browser will understand what it will receive – MatRt Apr 16 '13 at 23:45
  • Thanks friends , will take your advice and check for any posible hidden error, currently I'm not receiving any warning or similar and actually the image is displayed on the browser, if I rightclick it I am able to save it as but I want it to be automatically.Thanks again and Talk to you later pals.. – i'llbdevsomeday Apr 17 '13 at 02:09

0 Answers0