Jump to content

Position caption on photo with php?


Glenskie
Go to solution Solved by Glenskie,

Recommended Posts

Hello I have a script that puts text on a picture... i need help, i dont know to position these captions ? i need the topcaption to be center top and the bottom center bottom but i do not know how to achieve this. here is my code so far, any help is greatly appreciated.

 

$picture = $_GET['image'];
            $topcaption = $_GET['topcaption'];
            $bottomcaption = $_GET['bottomcaption'];


      //Set the Content Type
      header('Content-type: image/jpeg');
       if (isset($_GET['image'])){
          
      // Create Image From Existing File
      $jpg_image = imagecreatefromjpeg("../useruploadedphotos/$picture");


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


      // Set Path to Font File
      $font_path = '../fonts/Timeless-Bold.ttf';


      // Set Text to Be Printed On Image
      $text = $topcaption;


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


      imagejpeg($jpg_image, "../photos/$picture");


      imagejpeg($jpg_image, "../useruploadedphotos/$picture");


       imagejpeg($jpg_image);
      // Clear Memory
      imagedestroy($jpg_image);


// header("location: success.php");
}

 

Link to comment
Share on other sites

is there anyway to do this with jquery ? and php?

 

Do "what" exactly with JQuery? You could superimpose text on top of an image using CSS (no JavaScript involved). Or, if you are talking about using JQuery to perform AJAX calls to PHP, yes. But, not knowing how you want to use it, I can't give you any input. I will say this, however: I don't see why you are creating the image, writing it to two different places and then also outputting it directly to the browser. It would make more sense to me to write the image ONCE to the file system and then have a normal IMG tag for displaying it.

<?php
 
if (isset($_GET['image']))
{
    ## Hard coded values
    $images_folder = '';
    $font_path = '../fonts/Timeless-Bold.ttf';
    $font_size = 10;
    $top_margin = 50;
    $bottom_margin = 50;
    $photo_directory = '../photos/';
    $upload_directory = '../useruploadedphotos/';
 
    #Variable values
    $image_name    = $_GET['image'];
    $topcaption    = $_GET['topcaption'];
    $bottomcaption = $_GET['bottomcaption'];
    $image_path     = "{$images_folder}{$image_name}";
 
    //Create image resource from source image
    $imageRes = imagecreatefromjpeg($image_path);
    //Determine the image hieght & width
    $image_x = imagesx($imageRes);
    $image_y = imagesy($imageRes);
    //Create color identifier for the text
    $black = imagecolorallocate($imageRes, 0, 0, 0);
 
    //Create bounding box for the top caption
    $bbox = imagettfbbox($font_size, 0, $font_path, $topcaption);
    //Calculate cordinates for X and Y of top caption
    $x = $bbox[0] + (($image_x-$bbox[4]) / 2);
    $y = $top_margin - $bbox[5];
    //Write top caption to image
    imagettftext($imageRes, $font_size, 0, $x, $y, $black, $font_path, $topcaption);
 
    //Create bounding box for the bottom caption
    $bbox = imagettfbbox($font_size, 0, $font_path, $bottomcaption);
    //print_r($bbox);
    //Calculate cordinates for X and Y of bottom caption
    $x = $bbox[0] + (($image_x-$bbox[4]) / 2);
    $y = $image_y - $bottom_margin;
    //Write bottom caption to image
    imagettftext($imageRes, $font_size, 0, $x, $y, $black, $font_path, $bottomcaption);
 
    //Write image to file system
    imagejpeg($jpg_image, "{$photo_directory}{$image_name}");
    imagejpeg($jpg_image, "{$upload_directory}{$image_name}");
 
    //Output to browser
    header('Content-Type: image/jpg');
    imagejpeg($imageRes);
    imagedestroy($imageRes);
 
    // header("location: success.php");
}
 
?>
Edited by Psycho
Link to comment
Share on other sites

i really appreciate it but for some reason it just gives me a broken img ?

and this is for a upload website so basically it will upload with a caption to both and then an admin aproves it or denys it 

Edited by Glenskie
Link to comment
Share on other sites

i really appreciate it but for some reason it just gives me a broken img ?

and this is for a upload website so basically it will upload with a caption to both and then an admin aproves it or denys it 

 

 Well, that code works for me. I don't work with images much. I just took the example code from the manual and then made one change after another to meet the intent of your original code. That's what I was expecting you would do. If you are getting a broken image, then an error is occurring, but because the header is forcing the output to be an image you see a broken image. Comment out the following line to see the errors

 

header('Content-Type: image/jpg');

 

Are you sure the two directories where you are trying to save the photos exist where you think they do?

 

 

Also, your comments above still don't explain why you need to save two instances of the photo. makes no sense. Just create one file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.