PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



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 0104?


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   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
<?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);
    global $window;
    $window->set_size_request($width, $height+24);
}

function on_button($button, $angle_widget) {
    global $img_file;
    $date = $angle_widget->get_text(); // get the rotation angle
    $im = imprint_date($img_file, $date);
    $pixbuf = GdkPixbuf::new_from_gd($im); // note 2
    imagedestroy($im);
    $img = GtkImage::new_from_Pixbuf($pixbuf);

    $dialog = new GtkDialog("imprint date on photo", null, Gtk::DIALOG_NO_SEPARATOR); // place the rotated image in a dialog

    $dialog->vbox->pack_start($img);
    $dialog->show_all();
}

function imprint_date($img_file, $text) {
    list($img_width,$img_height, $img_type) = getimagesize($img_file);
    echo "img_size = $img_width x $img_height ($img_type)\n";
  • 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
 

Add comment


Security code
Refresh

< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved