PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to save image after add text to an Image

 October 10, 2022     gd, php     No comments   

Issue

In order to add text to an image I'm using below code from a 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 is working well but I am unable to save in pic for text file saving 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 any way I can able to save image in jpg ?


Solution

This is my own code and working well! Just change the name of name.jpg to the file name what 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 with yours, one of different is You didn't change the place of pointer, the place that you are going to put your character I mean the $x:

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

And another different is character, You gave the string (not character) to the imagettftext function, But I give one character. I think character is better than string In particular to creating captcha.



Answered By - Mohammad Kermani
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing