Tuesday, May 24, 2011

WebP: A new image format for the Web

WebP is a new image format that provides lossy compression for photographic images. In a large scale study of 900,000 web images, WebP images were 39.8% smaller than jpeg images of similar quality. A WebP file consists of VP8 image data, and a container based on RIFF. The standalone libwebp library serves as a reference implementation for the WebP specification and is available at this git repository and as a tarball. Webmasters and web developers can use the WebP image format to create smaller and better looking images that can help make the web faster.
Did you know? WebP is pronounced "weppy".

 How WebP works 

WebP uses predictive coding to encode an image, the same methodology used by the VP8 video codec to compress keyframes in videos. Predictive coding uses the values in neighboring blocks of pixels to predict the values in a block, and then encodes only the difference (residual) between the actual values and the prediction.
The residuals typically contain many zero values, which can be compressed much more effectively. The residuals are then transformed, quantized and entropy-coded as usual. WebP also uses variable block sizes.
WebP support
WebP is supported by a variety of tools. In addition, it is now natively supported in Google Chrome, the Google Chrome Frame plug-in for Internet Explorer and Opera 11.10.
Developers have also added support to a variety of image editing tools. This release also provides a lightweight encoding and decoding library, libwebp and command line tools cwebp and dwebp for converting images to and from the WebP format. The full source code is available on the download page.

WebP converter download 
Convert your favorite collection from PNG and JPEG to WebP by downloading the precompiled cwebp conversion tool for Linux, Windows or Mac OS X.

Friday, May 20, 2011

Google’s WebP Image Format Takes On JPEGs With Sharper Pictures (Google is on a mission to make the Web faster)


Google is on a mission to make the Web faster. One thing that slows down pageload times are fat image files. Even JPEG and PNG files can get pretty big. So Google is developing a new image format called WebP (which is a sister format to its WebM project for videos).
The key to making image files on the Web smaller without losing sharpness is better compression of the original file. Today, Google announced via the Chromium blog that the compression algorithms for WebP just got better and that they can even handle different parts of an image separately so that parts of it can be displayed without waiting for the entire image to be compressed and decompressed.
The images above, for instance, are from this gallery comparing JPEG to WebP. The WebP images are significantly smaller, but look just as sharp. Can you tell the difference? Unfortunately, WebP is only supported in Chrome and Opera browsers. Google products such as Gmail, Picassa Web albums, and Google Instant Previews also support WebP. But other than Opera, it’s pretty much an all-Google affair.
Websites aren’t going to start using WebP images just for Google Chrome visitors. But if it does speed things up without reducing the quality of images, other browsers will start supporting it as well or else be left behind.

Wednesday, May 18, 2011

Create SEO friendly URL`s using mod rewrite in PHP (.htaccess)

In this tutorial I will show you how to transform this:
http://www.mywebsite.com/tutorials.php?id=123&title=mod_rewrite&category=php
into this
http://www.mywebsite.com/tutorials/php/mod_rewrite-123

Why to rewrite your URL`s ?
The main reason is because static URL`s (the second URL is static) are indexed more faster than dynamic URL`s (like the first one), because from a static URL is easier to understand what the page is about both for search engines and your visitors. When using dynamic URL`s there is an another problem: search engines are reading only the beginning of the long URL`s, so if your addresses are different only at the end, than search engines will see the same URL for all of your pages and from this reason none will be indexed.

How this works ?
We will use Apache mod_rewrite. The mod_rewrite transforms back the second URL into the first URL, so the server will access the "ugly" address, but the visitors and the search engines will see the "clear" one. mod_rewrite simply rewrites the URL`s what meets specific conditions into addresses what are understood by the server.

Let`s rewrite it!
The code what rewrites the website address needs to be placed in your .htaccess file (if you don`t have it in your root folder, create it), the code is very short:

RewriteEngine on
RewriteRule ^tutorials/(.*)/(.*)-([0-9]+)/?$ /tutorials.php?id=$3&title=$2&category=$1





Take it apart!
The first line: RewriteEngine on simply does what it says, it starts the rewrite engine.
Now let`s take apart the second line:

RewriteRule - this tells the server about the rule to follow when rewriting the URL^ - this exponential sign means the start of the URL: http://www.mywebsite.com/tutorials - this word is simply added to the website adress/ - this character separates the directories (.*) - this tells the server that here will be placed some data (any kind of characters) (e.g.: php)- - this character will be simply added to the website adress([0-9]) - this tells the server that here will be placed a digit (e.g: 1)+ - the plus sign tells the server that here can be more digits (e.g.: 123)? - this sign tell the server that the character in it`s front it`s not required (so the URL will work with and without the last "/")$ - the dollar sign tells the server that here is the end of the rule


The last part of the line is the old address, the value of each variable (id, title and category) are changed to $3, $2 and $1. mod_rewrite takes these variables and places them in their new position in the new URL. In the first place for data (the first (.*)) it places the $1`s value (the category value), in the second place the value of $2 and so on. If you want to change the order just change the $1, $2 and $3 order, but be careful to set the right data type in the new URL. 

Benefits of Programming in Ruby on Rails


There are several programming languages that to-be programmers (or current programmers) can choose from. For first-time programmers, choosing a programming language can be difficult. There are many programming languages that are dying out, just aren’t popular, or are just generally hard to learn. It would be a shame for any first-time programmer to purchase books to learn a language to find that it is too difficult. It would also be a shame to learn a programming language that has no application at work. Ruby is a great programming language because it offers a great feature called Ruby on Rails. Rails is a web framework that can be used by programmers to speed up development.
The programming language, Ruby, was created in the mid 1990’s in Japan, by Yukihiro “Matz” Matsumoto. Ruby is based on Perl, Smalltalk, Eiffel, Ada, and Lisp and was written to create a programming language that, according to Matsumoto, “was more powerful than Perl, and more object-oriented than Python.”
One of the goals of Ruby is to allow the simple and fast creation of web applications. The language itself satisfies this goal. Because of this, there is much less tedious work with this language than many other programming languages. Ruby is also free of charge. It is also free to copy, use, modify, and distribute. Since Ruby can be modified, programmers can make necessary changes and can code without feeling restricted. Another feature of Ruby is its mark-and-sweep garbage collection which allows programmers the ability to code without having to worry about the need to maintain reference counts in extension libraries. Also, if an operating system allows for it, Ruby can dynamically load extension libraries. Ruby currently ranks as the 9th most popular programming language in the world. There are many online guides where Ruby programmers to-be can learn how to program in Ruby.
Ruby on Rails, often simply called Rails, is an open source web application framework that was designed for Ruby by David Heinemeier Hansson and was released in July 2004. Ruby on Rails makes use of an object relational mapping layer known as the ActiveRecord. Because of this, programmers do not have to specify database column names in class definitions. Rails will retrieve this information on its own from the database based on the class name. Rails also has a testing framework built in which allows programmers to write test cases which helps them create more reliable, robust code.

Ruby on Rails Proves it Deserves the Hype (JAVA VS RUBY)


With the multitude of programming languages out there such as Fortran, Java, C++ and C#, you may wonder why Ruby on Rails has suddenly become such a hot topic in the development world. Created in the late 1990's by an individual in Japan, the Ruby programming language had a cult following but never reached top-tier status. In 2004, a company called 37 Signals released a Web programming framework called Rails, which was originally developed for their own in-house projects. Rails was written using the Ruby programming language. It is the combination of Ruby and Rails together that has generated notable excitement in the development community, and the emerging language and application framework is proving to be a game-changer to the world of Web application development.

Ruby on Rails' Impact on Productivity

Rails is an integrated framework that utilizes the dynamic nature of the object-oriented Ruby programming language. Rails developers emphasize features of the framework for productivity rather than tools, and they take a common approach to Web architecture that should satisfy the needs of a large portion of the Web application projects in the wild.
As Rails was developed, the innovators of the framework realized that there were a multitude of features inherent to the framework that could benefit all Web applications. Rails is an extremely productive framework for database-backed Web applications. It is different from other frameworks in that it preaches convention over configuration. Typically, if you were building a computer program, you had to start from scratch and rely heavily on the skills and experience of the team's architect to develop and adhere to standards of writing good software. Rails primarily uses conventions to avoid configuration when possible, resulting in time saving and enhanced productivity. In a study by IBM, Rails applications needed a fraction of the configuration code of Java counterparts, often by a factor of ten or more.¹ Where the programmer must specify configuration, Rails usually relies on Ruby programming language to provide configuration. The strategy saves developers many lines of code and simplifies the code that does need to be written. By taking mundane but essential decisions away from the developer, productivity increases substantially because more time and attention is spent satisfying the customer's requirements rather than the technical plumbing that can burden most projects.
Ruby on Rails is known for its ability to build Web applications quickly and with ease. For first-time users of Java, it could take about five to ten days to build a moderately useful Web application that will communicate with a database using accepted Java Web best-practices such as separating business logic, from display logic (commonly referred to as Model-View-Controller). With no prior training on Ruby on Rails, it takes about one day to accomplish the same goal using the Ruby programming language. In several more days, it is possible to build a relatively complete Web application with Ruby on Rails.
In Java, a developer has countless decisions to make about how to proceed when communicating with a database. Should I use JDBC? Should I use an Object-Relational-Mapping (ORM) library, and, if I use an ORM, which one should I choose? TopLink? Hibernate? JDO? Do I need to use EJB2 or EJB3? Rails has an ORM database layer built into its framework called ActiveRecord. Another useful feature of Rails is scaffolding -- Similar to how a building under construction uses scaffolding to assist in the construction of the final structure, Rails can generate code called scaffolding that can be used to quickly put up functional Web pages that talk to a database and allow you to add, delete and change information quickly and with very little coding. As previously mentioned, in other languages you have to select naming conventions and define the general structure of the file system, while Rails utilizes conventions and the Ruby programming language so that all of that is provided for you. Managers will appreciate the fact that Rails minimizes configuration and encourages standardization. This feature enables programming skills to be more portable. Rails developers can move from one project to another with ease, since standards common to all development projects, such as naming conventions, directory structures, unit-test frameworks, and interface templates, are already decided, with working samples always available.
Another feature of Ruby on Rails that enhances developer productivity is the rapid feedback loop. The feedback loop is the amount of time between making a change in code and seeing the results in the execution of your application on the screen. In Rails, you get nearly-instant feedback as you code. There is no compile or packaging phase needed. You can immediately load a browser page to see the results of your changes. Managers should take note of this feature, because with enhanced productivity comes improved client satisfaction. Say you are in a meeting with a client, and they have some edits that they would like for you to incorporate into their Web application. With Ruby on Rails, you can make changes promptly and see the results immediately, improving the overall client experience.
It is important to note that introducing a radically different framework such as Ruby on Rails can be detrimental to a company's development, testing, and production infrastructure. For example, Rails applications are installed differently than common development platforms such as Java or Microsoft's .NET. Operations managers need to understand how to deploy it effectively.
But Rails acceptance is already being witnessed in large companies such as IBM, Microsoft, Apple, and Sun Microsystems. Sun Microsystems supports a project called JRuby that enables Ruby applications to run in a Java Virtual Machine. Efforts like JRuby will make it easier for companies to transition and adapt to Ruby on Rails since it will become a part of a familiar structure. Apple Computer has announced that Ruby on Rails will be included in the next release of its flagship operating system, OS X 10.5 (Leopard). And, even Microsoft has included Ruby programming language support in its newest rich-Internet framework, Silverlight. The world's largest technical publisher, O'Reilly Media, has even acknowledged Ruby's rapid growth and now considers it a major language with triple digit growth between 2006 to 2007. (http://feeds.feedburner.com/~r/oreilly/radar/atom/~3/117292190/state_of_the_co_10.html)
A common question regarding Ruby on Rails relates to its performance relative to its peers. Some companies claim that Ruby on Rails does not perform as well as comparable systems. Comments such as these must be looked at objectively and balanced against productivity gains. Managers then need to make the decision of whether it is cheaper to hire people or purchase additional hardware, and chances are they will find that purchasing more hardware makes better financial sense.

Conclusion

People are paying more and more attention to Ruby on Rails, and for good reason. With the advent of the Rails framework, the Ruby programming language is making its way towards the top. By focusing on convention over configuration, Ruby on Rails is lowering the barriers of entry to programming and positively impacting employee productivity-ultimately proving that all of the hype is true and well-deserved.

Tuesday, May 10, 2011

Building Desktop Applications in PHP

Introduction

So, you’re a hotshot PHP developer. You’ve written a lot of code
powering some pretty cool websites; maybe you’ve even written some
public / open source scripts that other developers are using. You’ve
been through it all with the web, from basic database hacks to battling
with inconsistent browser rendering, and now you want to have a go at
something different.
Of course, the standard destination would
be desktop application development. But how can you get into desktop
application development without leaving behind all your existing PHP
skills? In this article, I’ll examine the techniques and systems you
can use to build desktop applications, from GTK libraries to native
Windows API calls.

Fundamentals

Now, desktop application development is an entirely different cup of
tea. It’s not what you’re used to: you’re used to being input-driven,
but desktop apps are event driven (Delphi for PHP
is the best of both worlds). As a result, before you start developing
desktop applications, you’ll have to step back for a moment and examine
the differences.
As a web developer, you’re familiar with the
basics of web scripting. Client makes request to server, server runs
your PHP code through PHP compiler/interpreter, output is sent to
client etc. etc. Your entire request has finished very quickly;
possibly in less than a second. The user didn’t do anything in between;
the data for the request (cookie data, get / post values, server and
environment variables etc.) was all set once before your code was run.
When executing your script, PHP went from top to bottom, ending at the
last line of code in your PHP file. Once it’s finished, the request is
gone; the process finished, possibly even the thread killed.
Desktop
applications, on the other hand, are nothing like this. Now that we
have full-on JS-heavy web applications, you should have a basic
understanding of how desktop application development works – think of
it like a web application written entirely in JavaScript. It’s heavily
event driven, with functions for different events such as a button
click or a mouse move, and it keeps going without executing any code.
In desktop applications, you first draw the window using some basic UI
code, and then wait – just wait. You have to give up a bit of control
to the operating system – for example, if you want a menu, you
(typically) tell the operating system to draw a menu and to bind some
of your functions to the menu items, then let it take care of the rest.
It takes quite a while to get used to, but soon enough you’ll get the
hang of it, and be building fantastic desktop applications in no time.
Of
course, as your applications are written in PHP, you can also reuse all
your existing PHP code – well, almost all (HTML output won’t exactly
help). As a result, once you understand the basics of desktop
development, you can be building powerful desktop applications in no
time – you could even integrate with your existing web application!

Benefits of desktop development

There are a number of benefits to desktop development, from
technical advantages to fundamental benefits. Here are some reasons why
you should be developing desktop applications.

Technical advantages

There
are a number of basic technical advantages to desktop-based
applications. They can interact with the local filesystem, allowing
settings to be saved on the user’s computer, files to be retrieved and
manipulated, databases to be stored and so on. They generally integrate
with the GUI toolkit of the operating system, providing a UI style that
the user is familiar with – users are still getting used to the web
application interfaces of our web 2.0 applications. And finally, they
use consistent interfaces, unlike the hope-for-the-best approach to
writing HTML, allowing for complex UI elements.

Fundamental advantages

However,
perhaps most importantly, they are geared to user-driven applications,
especially where the user has content to share. Web applications can
easily bring users together, for effective sharing of information.
Desktop applications, on the other hand, can easily work with the user
and manage information, especially when there is no need to share that
information.
Gaming is an excellent example; interface and
graphics issues aside, games are built around the user, and as a result
work very well on the desktop. Image editing is another; despite the
availability of online Photoshop alternatives, everything at the higher
end of image editing is still squarely on the desktop. Sure, there’s
Flickr, but Flickr is more geared towards the sharing of photos and
basic editing.
As a result, if you’re considering building an
application that fits into one of these categories, and you’ve
currently got a web application in mind, a desktop application is
definitely worth considering.

Building desktop applications in PHP

To demonstrate basic application development in PHP, we’ll take a
look at the PHP-GTK extension. PHP-GTK offers GTK bindings for PHP,
allowing you to build scripts that create windows using the GTK
graphics toolkit.

The desktop approach

As you can
imagine, building a desktop application when coming from a web
background is a whole new ballgame. Consider that, when you run a PHP
script on the web, it goes from top to bottom. When you run a desktop
application, you open it, and wait. And wait. And wait. It doesn’t
matter how long you wait for – you are waiting, and the application is
not doing anything. Of course, the moment you click a button or open a
menu, it springs into action again. At what point in the execution of a
standard PHP script is absolutely nothing happening? (Besides in sleep()) .
Now,
the only real way to achieve this in PHP is an infinite loop, with a
check for some sort of change or input on every iteration. Which is
exactly how the various most of the PHP-desktop systems available
function. Of course, it would be impractical for you to code this loop
manually, as there’s a lot of code all over the place. Instead, you
tell the interface system to draw your interface and bind your
functions/methods to the interface elements. Drawing your interface
involves telling it what interface elements to create – such as buttons
and input boxes – and where to put them. Binding allows you to link
your functions/methods to certain events for these elements. For
example, you could create a button, and have one of your functions
called when the button is clicked.
That’s where the
fundamentals end and the various differences in systems come into play.
So, let’s take a look at some code to create a basic desktop
application.

PHP-GTK

PHP-GTK is one of the standard
approaches for creating desktop-based PHP applications. As it only
requires GTK for interface bindings, it’s cross-platform – you can
write your application on a Mac, test it on a Linux machine and deploy
it to a Windows box, and with no code adjustments it will look (almost)
exactly the same on each.
Before continuing, if you want to try out the code samples, you’ll need a copy of PHP-GTK. You can download it from gtk.php.net;
there are windows binaries, but for everything else you may have to use
CVS. Installation instructions are available on the download page; you
might find these instructions for Ubuntu useful as well.
Now, let’s take a look at a basic PHP-GTK script from the manual. Here’s the full code:
<?php// Code snippet from the PHP-GTK manual.if (!class_exists('gtk')) die("Please load the php-gtk2 module in your php.ini");
$window = new GtkWindow();$window->set_title('Our PHP-GTK window');$window->connect_simple('destroy', array('gtk', 'main_quit'));
$label1 = new GtkLabel("Hello world!");$window->add($label1);
$window->show_all();Gtk::main();?>
As you can see, it’s perfectly normal PHP code. However, it’s relatively short; it ends with a call to this mysterious Gtk::main().
Remember that infinite loop I mentioned? When you finish creating your
window and drawing it, your scripts hand over to the GTK library that
handles that same loop. Still, it’s new territory, so let’s step
through the code and review it.
if (!class_exists('gtk')) die("Please load the php-gtk2 module in your php.ini");
Pretty
standard stuff: we check that the ‘gtk’ class is available. Without it,
you won’t be able to render your windows with the GTK toolkit. As PHP
isn’t really designed for desktop applications, there is still the
potential for deployment quirks such as lack of the GTK module, so we
carefully check it here.
$wnd = new GtkWindow();$wnd->set_title('Our PHP-GTK window');
The GtkWindow
class is, as it’s name suggest, a GTK window. Working with GTK in PHP
is heavily object oriented; you’ll have objects for your windows, the
interface elements on those windows, GTK itself and more. The set_title() method sets the title, similar to using a <title> tag in HTML, with the exception that what really goes in the title bar is no longer at the mercy of the end-user environment.
$window->connect_simple('destroy', array('gtk', 'main_quit'));
This
is where we start binding to the interface. When the user clicks the
close button on our application, it doesn’t just magically clean up
after itself and quit. The operating system can’t simply kill our
application; chances are we want to tie up some loose ends and clear
out data we’ve stored. As a result, we have to link the ‘destroy’
signal to the gtk::main_quit() method. This method essentially ends our application so that we can cleanly exit.
$label1 = new GtkLabel("Hello world!");$window->add($label1);
Now
we start creating our interface. We’ll start with a simple label. In
desktop applications, if you want to cleanly position text (and you do,
as opposed to running it all from the top-left corner of the form) it
needs to be in some sort of container, and a label is the common term
for a basic text container in a window.
You can position your
label, give it some additional properties and so on, but for now we’ll
just give it the text "Hello world!". By calling the add() method of our GtkWindow object $window,
we tell GTK to put our new label on our window. Without this, the label
can’t really appear anywhere. Interestingly, this approach of not
scoping an interface element within a particular form makes element
reuse simple and effective.
$window->show_all();Gtk::main();
This is the interesting part. First of all, our window won’t actually appear until we call the show_all()
method on our window’s object. This call tells GTK to show the window
on the screen. However, that infinite loop I mentioned before? Now that
we’ve finished drawing our window, we’re ready to roll. We call Gtk::main(), and away we go…

Your first PHP-GTK application!