Archive for November, 2009

Borders on images with rounded corners using GD-lib in PHP

Wednesday, November 25th, 2009

I just extended my “image.php”-script (which I have written about in a previous post) to be able to add borders on images. Nothing special really – except the script also supports doing it with rounded corners. Its actually just extending the rounded-corners-function, because it would be a hell doing it manually on a picture, after it had gotten rounded corners.

Its done by adding “edgeborder=htmlcolor”. Like this:

https://www.kaspernj.org/image.php?picture=temp/kasper_test_blog.jpg&edgeborder=0000ff&edgesize=35&bgcolor=000000&force=true&width=400

…which will result in:

This enables me to make nice rounded images with borders – without having to send tons of HTML to the client. The image is even cached through the image.php-cache – saving both the server and the client for tons of calculations. Just sending a cached image to every clinet – wee :-)

Chrissi’s iPhone: Jailbreak, SSH automount and VLC+MPlayer media players

Wednesday, November 25th, 2009

Today Christina had a lot of trouble with getting her iPhone working with Linux. Since Apple apparently hate their own customers (Microsoft 2 anyone?), I had to take several steps to get some basic functionality up and running on her expensive phone.

First of all she needed to transfer music back and forward to the phone. My first guess was just using iTunes under VirtualBox – but that was just too ugly and complicated. I then jailbroke her phone using BlackRain’s tool. It was pretty easy, once I got my Windows-box connected.

I then installed Cydia – and through that SSH.

I then installed the “ipod-convenience” package on her laptop and was able to mount the iPhone via “iphone-mount”. Shortly after I found out that Apple fucked every application which worked with the iTunes-music-dir format up – simply because they didn’t want the iPhone to work with anything else than their own iTunes (wtf!?). Fucking retards…

I then installed MPlayer and vlc4iphone to get basic media functionality up again… Further more I set up “autofs” on Christina’s laptop and copied her SSH-key to her phone, so she didnt have to mount anything. I altered the DHCP-config, so her phone always were on the same IP. Then I made the following config in “/etc/auto.iphone” for “/media/iphone_auto”:

iPhone -fstype=fuse,allow_other,reconnect,uid=1000,gid=1000 sshfs\#192.168.1.155:/var/mobile/Media

Everytime Christina now accesses /media/iphone_auto/iPhone – her system will automatically try to mount her iPhone via SSHFS (SSH File System).

She can now copy music, pictures and all the other stuff over by using Nautilus (the filemanager of Gnome – Linux).

This whole thing was even easier on the OpenMoko-phone I played with a year back… I am never going Apple…

Glade auto connect in JRuby

Sunday, November 15th, 2009

I have been playing a lot around with JRuby lately (since I was scared away from IronRuby by another lame Microsoft-patent). I have been really disappointed by the Gtk-implementation in Java – it really misses a lot of the good stuff from PHP-GTK – like the auto-connect functionality.

I decided I wanted to write my own Glade Auto Connect functionality to learn some more about JRuby – it turned out a bit harder than I expected. In the end I got it working with some of the basic widgets (GtkButton::Clicked, GtkWindow::destroy, GtkTreeViewSelection::changed and so on). I am adding support for more events as I continue my adventure into JRuby.

It works like this – when you want to spawn a new Glade-object, you do it through my function:

class MyWindow
  def initialize
    @glade = GladeAutoConnect("gladefile.glade", "window", self)
  end

  def on_window_destroy
    print "The window was closed!\n"
  end
end

No more connecting events manually – just write the event-names in the Glade-application directly. This functionality really shortens the code – which is what I like about Ruby: Less code to write. Right now it produces some eval-code though, which I plan to remove, when I learn more about how to dynamically make new classes in Ruby.

The code can bee seen here:
http://knjrbfw.kaspernj.org/jruby_gtk.rb

HowTo: Make a simple GtkWindow with IronRuby, GtkSharp, Glade and events (not Ruby-Gtk!)

Sunday, November 8th, 2009

To make this a lot easier for the ones of us using Ubuntu, I added IronRuby to my Ubuntu-repository. Read more about how to set it up here:

http://wiki.kaspernj.org/index.php/KnjUbuntuRepository

Afterwards do a: “sudo aptitude install ironruby glade libgtk2.0-cil libglade2.0-cil”.

While you are at it, install “glade” (”glade-3″ if you are using Jaunty, I think). Make a new project folder – I have called mine “IronRubyTest”. Make a small Glade-file (not GtkBuilder) with a label and a button. Remember the names – I called mine “window1″, “label1″ and “button1″.

To load the Glade-file, we have to import some modules first. To get the names of the modules, type “gacutil -l | grep gtk” and “gacutil -l | grep glade”. Type this in the beginning of your Ruby-code:

require "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"
require "glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"

Be aware you properly have to use the lines, that you got from the “gacutil grep”-command.Its possible that yours can be different. Then type this afterwards in the Ruby-file:

include Gtk
include Glade


We are now ready to begin coding our class:

class MainWindow
  def initialize
    Application.init

    tha_glade = Glade::XML.new("test.glade", "window1", nil)
  end
end

Notice the “Application.init” – I guess this is to init the Gtk-stuff, like you would do in other languages when you are using Gtk.

The GladeXML-object is stored in the “tha_glade”-variable. “window1″ is the name of the Window, which can be set through Glade.

...
    tha_window = tha_glade.GetWidget("window1")
    tha_window.ShowAll
    tha_window.destroyed{|sender, e|
       Application.Quit
    }

    tha_button = tha_glade.GetWidget("button1")
    tha_button.clicked{|sender, e|
       tha_window.destroy
    }
...

We store the GtkWindow in the “tha_window” variable. Afterwards we attach some code to the destroy-event – we actually make the application kill itself, when we close the Window.

Further more we store the GtkButton in the “tha_button” variable. Afterwards we make the button close the window – which calls the other event and kills the application.

main_window = MainWindow.new
Application.run

The code here is placed at the very end of the file – not included in any function or class. This start our MainWindow-class, loads the window and everything and starts a loop, so our application doesnt end itself after loading the window and all.

The entire code can be seen here:

require "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"
require "glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"

include Gtk
include Glade

class MainWindow
	def initialize
		Application.init

 		tha_glade = Glade::XML.new("test.glade", "window", nil)

		tha_window = tha_glade.GetWidget("window")
		tha_window.ShowAll
		tha_window.destroyed{|sender, e|
			Application.quit
		}

		tha_button = tha_glade.GetWidget("button1")
		tha_button.clicked{|sender, e|
			tha_window.destroy
		}
	end

	def on_button1_clicked(object, event)
		print "hmm\n"
	end
end

main_window = MainWindow.new
Application.run

New cool image-PHP-script, which can be used from every programming language

Wednesday, November 4th, 2009

I just wrote a new set of cool image-functions and a small script to automatically invoke and cache stuff it generates.

The script is able to do smart resizes, resizes, padding, rounded edges, caching, equal sizes, set quality and dynamically output in different support formats (jpeg, png, bmp, wbmp, gif etc).

To start with an example, I have uploaded a picture (”kasper_test_blog.jpg”) and symlinked to the script, so I can call it. It is a picture of me holding a speech at the 2007 OpenSource Days conference. I now want the same width and height + rounded edges on the picture. I set src of an <image> to “image.php?picture=temp/kasper_test_blog.jpg&equaldim=true&edgesize=35&bgcolor=000000&force=true&width=400&padding=15&paddingorigsize=true” resulting in:

This will:

  1. Make the width and height the same size because of “equaldim=true”.
  2. Make 35 size rounded corners because of “edgesize=35″.
  3. Use black as the backgroundcolor for everything because of “bgcolor=000000″.
  4. Not use cache because of “force=true”.
  5. Force the width to 400 pixels because of “width=400″ (could also have used “height=400″).
  6. Make 15 pixels of padding on top, bottom, left and right because of “padding=15″ (still using black as color for the padding because of “bgcolor=000000″).
  7. When using padding, instead of increasing the image-size by the padding, it will keep the original size because of “paddingorigsize=true”.

Other possible parameters are:

  1. “quality=85″ – sets a specific quality of the outputted image. Good when using it for a lot of thumbnails.
  2. “type=jpg” – output the image as JPG (default is PNG). Could also be “type=bmp”, “type=gif”, “type=png”, “type=wbmp” or whatever.
  3. “smartsize=200″ – makes the width or height to 200 – based on which is the larger one. Great for handling sizes on a personal gallery.

The sourcecode for the script and its framework can be found here:

  1. http://knjphpframework.kaspernj.org/scripts/image.php
  2. http://knjphpframework.kaspernj.org/functions_knj_picture.php
  3. http://knjphpframework.kaspernj.org/functions_knj_picture_bmp.php