Sample Code 478: How to display a list of thumbnail images using GtkIconView - Part 4 - with auto scaling to a max width of 24? |
|
Written by kksou
|
|
Monday, 14 April 2008 |
|
Problem This is in response to Andrea's post titled "How to resize images?"
You have displayed a list of thumbnail images for selection using the technique as outlined in How to display a list of thumbnail images using GtkIconView?
Howver, as the images are of different width, you would like to auto scale each image to a maximum width.
In this example, the original images are all of width=49. You will see that all of them have been scaled to a width of 24 as shown below:

Solution
Sample Code Note: The following image files are required by the sample code below. Please save a copy of the image files and put them in the same directory where you store the sample code.
 | square_red.jpg |
 | square_yellow.jpg |
 | square_green.jpg |
 | square_blue.jpg |
 | square_blue2.jpg |
 | square_blue3.jpg |
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
| <?php define('MAX_IMG_WIDTH', 24); // note 1
$window = new GtkWindow(); $window->set_size_request(400, 240); $window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel(" Display thumbnail images using GtkIconView\n". "Part 4 - with auto-scaling to a max width of 24"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); // add a small gap
// Set up a scroll window
$scrolled_win = new GtkScrolledWindow(); $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
if (defined("GObject::TYPE_STRING")) { $model = new GtkListStore(GdkPixbuf::gtype, GObject::TYPE_STRING); } else { $model = new GtkListStore(GdkPixbuf::gtype, Gtk::TYPE_STRING); }
foreach(array('square_red.jpg', 'square_yellow.jpg', 'square_green.jpg', 'square_blue.jpg', 'square_blue2.jpg', 'square_blue3.jpg') as $img) { $pixbuf = GdkPixbuf::new_from_file($img); $width = $pixbuf->get_width(); // note 2
$height = $pixbuf->get_height(); // note 2
if ($width>MAX_IMG_WIDTH) { // note 3
$new_width = MAX_IMG_WIDTH; $new_height = MAX_IMG_WIDTH * $height / $width; // note 4
$pixbuf = $pixbuf->scale_simple($new_width, $new_height, Gdk::INTERP_HYPER); // note 5
|
- 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
- Specify the maximum width.
- Get the width and height of the original image.
- Check if the width is greater than the maximum width.
- Calculate the new height.
- Scale it!
- Set the image.
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. |
Latest Blog Articles
|
Monday, 05 October 2009 This module makes use of the Google Weather API to display the current temperature, weather condition and a four-day weather forecast, as you can see on the left of the page.
This is the first, and currently the only weather module with the following features:
- Uses the Google Weather API that provides 4-day weather forecast with images.
- Allows user to interactively enter zip codes (for US, UK and Canada) and city names.
- Displays the weather information in many different languages, as provided by Google.
- Allows you to display two or more weather modules in the same page.
- Supports both Joomla 1.0 and Joomla 1.5!
- You can turn on or off the input field.
- You can specify the default unit for the temperature (in °C or °F)
- You can switch between °C or °F interactively with no reloading of pages.
Download: Google Weather Module - AJAX version v1.5.
|
|
|
Friday, 02 October 2009 Added new parameter "hide_direction_text=1" - allows you to hide the text description on the right of the map.
Download here: googleDirections plugin v1.58
Demo: Show map directions with maps only (and text description hidden) |
|
|
Wednesday, 09 September 2009 You can now download the latest version of Glade 3 (v3.6.7) windows binary from glade.gnome.org.
Thanks to Roeland from Belgium who informed me about this.
Details here: How to install glade3 for PHP GTK2 on windows?
|
|
|
Tuesday, 08 September 2009 If you're using the googleMaps plugin, by default, the google search bar is displayed in the lower left hand corner.
To hide this google search bar, please refer to the following article: googleMaps with search bar hidden
By default, the Overview Map Control is also displayed in the lower right hand corner.
To hide this Overview Map Control, please refer to the following article: googleMaps with Overview Map Control hidden
To hide both the google search bar and the Overview Map Control, please refer to the following article: googleMaps with search bar and Overview Map Control hidden
|
|
|
Thursday, 03 September 2009 Have just added Joomla FAQs
http://www.kksou.com/php-gtk2/Joomla-FAQs/
Here you will find a list of Frequently Asked Questions (FAQs), Troubleshooting guides, and Tips & Techniques for the Joomla plugins, modules and components written by me.
Many of thees are contributed by users. Thanks to you all!
I'm starting with the DirectPHP plugin:
DirectPHP - FAQs, Troubleshoot and Tips & Techniques
If you know some tips and tricks, share them with the rest too!
|
|
|
Tuesday, 01 September 2009 Added support for retrieving google currency data using file_get_contents() or fread(). So for those people with no cURL library, you can now use the module using file_get_contents() or fread().
Download here: http://www.kksou.com/php-gtk2/Joomla-Gadgets/Google-Currency-Converter-AJAX-version.php
|
|
|
Tuesday, 01 September 2009 This is in response to Alamshahi's post titled "uncorrect results : missed digits".
The error occurred when Yahoo returns a conversion rate greater than 1,000. The original program was "confused" because of the ',' in 1,000. Have fixed the bug in this version.
In this new release, I've also added support for retrieving yahoo currency data using file_get_contents() or fread(). So for those people with no cURL library, you can now use the module using file_get_contents() or fread().
Download: http://www.kksou.com/php-gtk2/Joomla-Gadgets/Yahoo-Currency-Converter-AJAX-version.php |
|
|
Monday, 31 August 2009 This is in response to Redback's request to have time offset in half hours e.g. South Australian time is +9.30 GMT.
Download here: Yahoo Stock Quote (AJAX module version) v1.5.8 |
|
|
Saturday, 01 August 2009 This module allows you to display your twitter news in a module. The tweets are displayed using the Pausing up-down Scroller plugin.
This module is contributed by Claudio Tortorelli of Italy.
Download: Twitter Module v1.5. |
|
|
Saturday, 25 July 2009 This is in response to Chris's request to allow multiple lines for the home label.
For new lines, please use ~ (similar to that of the googleMaps plugin). Please refer to here for details.
Download here: http://www.kksou.com/php-gtk2/Joomla-Gadgets/googleDirections-To-Here-plugin.php |
|
<< Start < Prev 1 Next > End >>
| | Results 1 - 10 of 10 |
|