How to save the image after adding text to the image

To add text to the image, I am using the code below from the site

<?php
    //Set the Content Type
    header('Content-type: image/jpeg');

    // Create Image From Existing File
    $jpg_image = imagecreatefromjpeg('sunset.jpg');

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

    // Set Path to Font File
    $font_path = 'font.TTF';

    // Set Text to Be Printed On Image
    $text = "This is a sunset!";

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

    // Send Image to Browser
    imagejpeg($jpg_image);

    // Clear Memory
    imagedestroy($jpg_image);
?> 

      

It works well, but I can't save to pic to save the text file. I am using this Function

function write($post,$myFile){
    $fh = fopen($myFile, 'a+') or die("can't open file");
    fwrite($fh, $post);
    fclose($fh);
} 

      

Is there a way to save the image to jpg?

+3


source to share


3 answers


This is my own code and works well! Just change the name name.jpg to the filename whatever you want :

    <?php
  header('Content-type: image/jpeg');

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('sunset.jpg');
//$jpg_image=imagecreatetruecolor(100,100);

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


  // Set Path to Font File
  $font_path = 'font1.TTF';

  // Set Text to Be Printed On Image
  $text = "This is a sunset!";

  // Print Text On Image
  $x=20;
  for($i=0;$i<=strlen($text);$i++){
   $print_text=substr($text,$i,1);
   $x+=20;
    imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text);
  }


  // Send Image to Browser
  imagejpeg($jpg_image,'name.jpg');

  // Clear Memory
  imagedestroy($jpg_image);
?> 

      

My code is a little different from yours, one of the options: you did not change the place of the pointer, the place where you are going to put your symbol I mean $ x :



imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text);

      

And one more character, you passed a string (not a character) to the imagettftext function , but I am giving one character. I think character is better than string. In particular, to create captcha.

+2


source


To save the image to a file rather than display it, just change the line imagejpeg($jpg_image);

to imagejpeg($jpg_image, 'yourfile.jpg');

(you can also delete header('Content-type: image/jpeg');

in this case).



0


source


Check out this code. This is the same as your code, the only difference is

imagejpeg($jpg_image,"imagefolderpath/image.jpg");

instead of imagejpeg($jpg_image);


Maybe this is useful to you.

<?php
  header('Content-type: image/jpeg');
  $jpg_image = imagecreatefromjpeg('sunset.jpg');
  $white = imagecolorallocate($jpg_image, 255, 255, 255);
  $font_path = 'font.TTF';
  $text = "This is a sunset!";
  imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
  imagejpeg($jpg_image,"imagefolderpath/image.jpg");
  imagedestroy($jpg_image);
?> 
      

Run codeHide result


0


source







All Articles