Sample Code 104: How to imprint date on photo?
Written by kksou   
Thursday, 30 November 2006
Problem

You have set up a simple drag-and-drop image viewer in How to create a simple drag and drop image viewer?

Now you would like to imprint some text (e.g. date) on the photo as shown below:

How to create a simple drag and drop image viewer?.jpg" alt="How to create a simple drag and drop image viewer?" />


Solution

Please refer to How to rotate an image? on the use of GD2 with php-gtk2. We employ exactly the same technique as described in the article.

  • Use imagettfbbox to find out the width and height of the text that will be printed on the photo.
  • Imprint the date with imagettftext.

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
<?php
$window = new GtkWindow();
$window->set_size_request(240, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));

$img = new GtkImage();
$img->drag_dest_set(Gtk::DEST_DEFAULT_ALL,
    array( array( 'text/uri-list', 0, 0)), Gdk::ACTION_COPY);
$img->connect('drag-data-received', 'on_drop', $img);

// Set up gtkentry to get rotation angle
$vbox = new GtkVBox();
$button = new GtkButton('Imprint date');
$button->set_size_request(-1,24);
$hbox = new GtkHBox();
$hbox->pack_start(new GtkLabel('Date: '), 0, 0);
$date = date('Y-m-d');
$angle = new GtkEntry($date);
$angle->set_size_request(100, -1);
$button->connect('clicked', 'on_button', $angle);
$hbox->pack_start($angle, 0, 0);
$hbox->pack_start(new GtkLabel('  '), 0, 0);
$hbox->pack_start($button, 0, 0);
$vbox->pack_start($hbox, 0, 0);
$vbox->pack_start($img);

$window->add($vbox);
$window->show_all();
Gtk::main();

// process drop
function on_drop($widget, $context, $x, $y, $data, $info, $time, $img) {
    $uri_list = explode("\n",$data->data);
    global $img_file;
    $img_file = $uri_list[0];
    $img_file = str_replace("file:///", "", $img_file);
    $img_file = str_replace("\r", "", $img_file);

    global $pixbuf;
    $pixbuf=GdkPixbuf::new_from_file($img_file);
    $width = $pixbuf->get_width();
    $height = $pixbuf->get_height();
    $img->set_from_pixbuf($pixbuf);
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
 

And here's the code for imprint_date.php. Note that the processed image is stored at c:\ root folder. You may change it to any other location you like.

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
<?php

$img_file = $argv[1]; // the image filename
$text = $argv[2]; // the date
$processed_img_file = "c:\processed_img";

list($img_width,$img_height, $img_type) = getimagesize($img_file);
echo "img_size = $img_width x $img_height ($img_type)\n";

switch($img_type) {
    case 1: $img = imagecreatefromgif($img_file); break;
    case 2: $img = imagecreatefromjpeg($img_file); break;
    case 3: $img = imagecreatefrompng($img_file); break;
    default: echo "unsupported file format\n"; exit(1);
}

$font_color = imagecolorallocate($img, 255, 153, 24);  // set the font color
$font = 'arial.ttf'; // specify the truetype font
$angle = 0; // angle of the text
$fontsize = 16; // set the font size

$bbox = imagettfbbox ($fontsize, $angle, $font, $text); // note 1
$text_width = $bbox[2] - $bbox[0]; // note 1
$text_height = $bbox[1] - $bbox[7]; // note 1

$x_margin = 10;
$y_margin = 2;
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation

This example make use of the method as described in How to rotate an image?.

What's new here:

  1. Get the width and height of the bounding box of the date that will be imprinted.
  2. Calculate the start position of the date. Note that we leave some x and y margin from the bottom right-hand corner of the image.
  3. Imprint the date!

Related Links

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved