|
Problem This is in response to Mote's post titled "2 questions about GtkImage".
He has set up an application that allows drag-and-drop of 12 different images. And he would like to be able to retrieve the URI (or filenames) of these 12 images at some later time for processing.
In Part 1, you can only drag and drop one image at a time.
In Part 2, you can drag and drop multiple images at one go.
In Part 3, you can delete unwanted images with right mouse click.
In this Part 4, I'll show you how to automatically scale the image to size 100 by 100 pixels (when the width of height is greater than the 100 x 100) as shown below:

Solution
Important Note: This only works for PHP-GTK2 compliled with gtk+ v2.10 and above. If you are using an older version, for linux, you may follow the step-by-step instructions to recompile php-gtk2 with gtk+ v2.10. For windows, please refer to How to install php gtk2 on windows? You may also want to take a look here to see some of the new exciting PHP-GTK2 Functionalities.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| <?php $app = new App; $app->go();
class App {
var $num_rows = 4; var $num_cols = 3; var $img_width = 100; var $img_height = 100; var $img = array(); var $img_filename = array();
public function __construct() {
$window = new GtkWindow(); global $argv; $window->set_title($argv[0]); $window->set_size_request(400, 400); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel(" Drag and drog your images\n". "into any of the 12 image positions\n". " Part 4 - scale image"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 60); $vbox->pack_start($title, 0); $vbox->pack_start(new GtkLabel(), 0);
// add a process button
$hbox = new GtkHBox(); $vbox->pack_start($hbox, 0); $process_button = new GtkButton('Process Images'); $process_button->connect('clicked', array(&$this, 'on_process_button')); $hbox->pack_start($process_button, 0);
// init the filenames
for($i=0; $i<$this->num_rows*$this->num_cols; ++$i) { $this->img_filename[$i] = ''; }
// set up the images
$scrolled_win = new GtkScrolledWindow(); $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $vbox->pack_start($scrolled_win); $img_vbox = new GtkVBox(); $scrolled_win->add_with_viewport($img_vbox);
for ($row=0; $row<$this->num_rows; ++$row) { $hbox = new GtkHBox(); $img_vbox->pack_start($hbox, 0); for ($col=0; $col<$this->num_cols; ++$col) { $k = $row*$this->num_cols + $col; $img = new GtkImage(); $img->set_size_request($this->img_width, $this->img_height); $img->drag_dest_set(Gtk::DEST_DEFAULT_ALL, array( array( 'text/uri-list', 0, 0)), Gdk::ACTION_COPY); $img->connect('drag-data-received', array(&$this, 'on_drop'), $k); $this->img[$k] = $img;
// add image to eventbox
$eventbox = new GtkEventBox(); $eventbox->add($img); $eventbox->connect('button-press-event', array(&$this, 'on_button_press_img'), $k);
// add eventbox to frame
$frame = new GtkFrame(); $frame->add($eventbox);
// add frame to the hbox of image_vbox
$hbox->pack_start($frame, 0); $this->add_margin($hbox, 10); } }
$window->show_all(); }
public function go() { Gtk::main(); }
protected function add_margin($hbox, $n) { $space = new GtkHBox(); $space->set_size_request($n, -1); $hbox->pack_start($space, 0); }
public function on_drop($widget, $context, $x, $y, $data, $info, $time, $id) { $uri_list = explode("\n",$data->data); for ($i=0; $i<count($uri_list); ++$i) { $img_file = $uri_list[$i]; if ($img_file!='') { $img_file = str_replace("file:///", "", $img_file);
$img_file = str_replace("\r", "", $img_file);
$pixbuf=GdkPixbuf::new_from_file($img_file); // note 1
$width = $pixbuf->get_width(); // note 2
$height = $pixbuf->get_height(); // note 2
if ($width>$height && $width>$this->img_width) { $scale_factor = $this->img_width/$width; // note 3
$require_scaling = 1; } elseif ($height>$width && $height>$this->img_height) { $scale_factor = $this->img_height/$height; // note 3
$require_scaling = 1; } else { $require_scaling = 0; }
if ($require_scaling) { $new_width = $width * $scale_factor; // note 4
$new_height = $height * $scale_factor; // note 4
$new_pixbuf = $pixbuf->scale_simple($new_width, $new_height, Gdk::INTERP_HYPER); // note 5
$this->img[$id]->set_from_pixbuf($new_pixbuf); // note 6
} else { $this->img[$id]->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.
Explanation We make use of the code from Part 3.
What's new here:
- Load the image into a GdkPixbuf.
- Get the width and height of the image.
- Calculate the scale factor.
- Find out the new width and height of the image.
- Scale the image.
- Load the scaled image into the GtkImage.
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. |