Archive for the '' Category

May 20th 2008

New Mac Widget: QR Code Generator

www.rafaeldohms.com.br em QRWith the advent of the smartphones, tools used before in the most random areas end up coming to a phone near you. QR codes are one of these examples. The QR (or Quick response) codes are a matrix, a barcode in 2D, initially created for tracking packages and car parts. However they rapidly infiltrated various other areas, like our phones.

Celular phones with cameras and the right software (N95 has this native, iPhone has some apps coming along) can scan these codes and decode the text in it. This has been more and more used to send links to phones, avoiding the whole hassle of typing, which is great for installing new softwares or loading bookmarks.

O widget em açãoOther uses involve for example personal cards, where you can print a code on your card that holds all the info of the actual card, like, name, mail and phone, just scan the code and you are ready to go. Nokia has a solution similar to this called Nokia Mobile Codes.

This QR Generator here, is a simples mac dashboard widget, you just paste the text or URL you want to convert in the field, you can then generate the code and scan it to your phone.

To install it follow this link (Only for Mac OS X)

Future versions

This version of the widget uses a web connection to access a remote generator, in future I want this all to be done locally. Further along i will try to implement a “save as” routine so you can save this image and use it elsewhere, as well as generating bigger codes with more info.

1 Star2 Stars3 Stars4 Stars5 Stars (Sem votos registrados)
Loading ... Loading …

1 Comment »

February 12th 2008

Adding text to images in real time with PHP

Using PHP and the GD library the task of manipulating images, editing them and even adding text before presenting it to the user becomes a simple feat. Since I had to make use of this tool recently while making "on-the-fly" cupons for a client, i decided to write this article showing how easy this can be done.

First Steps

Attention: verify that the GD library is installed using a phpinfo() command.

There are a few ways of outputting the image after finishing the process, for example:

  • Save the file to disc
  • Output image on screen (as an image, inside the HTML)
  • Output to screen (just the image code)

In this example we are going to output the image to a <img> tag inside the HTML code, but for this we must understand a little about how this structure fits together. Generally an image tag points to a ".jpg" or ".gif" file, but here we need it to point to a ".php" file. This file will be responsible for seeking the image, editing it (insert text) and outputting the raw image code that will be read by the <img> tag.

So as a result we will have three files: the main.php file, that will output our HTML code, the image.php file that will edit and output the image, and a img.jpg that will be our base image that we will add text to.

main.php

This is a simple and straight-through file with an image tag. The only twist here is that in the image src attribute we will include (via GET) the name that should be written on the image.

HTML:
  1. Get your certificate below&lt;br&gt;
  2.  
  3. <img src="imagem.php?nome=Rafael%20Dohms" />

imagem.php

This is where the magic begins, this file should retreive the original image, the input text, and join the two, outputting the image with the text in it. So let's go at it step by step.

The first step is to load the image into a PHP resource, basically loading it into the memory. PHP has a few functions for this, depending on the image's format: ImageCreateFromJPEG, ImageCreateFromGIF and so on ...

PHP:
  1. $rImg = ImageCreateFromJPEG( "ex-img.jpg" );

Now comes the time to write the text to the image, but first we need to get some parameters ready. There are basically two ways to write text on an image, one char at a time or a whole string at once (imagechar ou imagestring). In our example we will use the string method, but both receive the same parameters varying only on the text to be written. Before invoking the mothod we should setup the color to be used.

To do this we use the imagecolorallocate that receives as values the image resource and the color in RGB format (3 numbers) . In this case we use a black color:

PHP:
  1. $color = imagecolorallocate($rImg, 0, 0, 0);

Now we can begin writing. The function imagestring receives these parameters, in this order:

  • image: the image resource
  • font: an interger, from 1 to 5 (default PHP font, the bigger the numb, the bigger the font size) or a system font
  • x: horizontal shift (coordinate)
  • y: vertical shift (coordinate)
  • string: text to be written
  • color: variable with the allocated color

Note that the x and y coordinates are relative to the top left of the image. Also, for this example we will be using the default PHP font, but generally any TTF font will do, as i'll describe towards the end of the article.

This is our base image:

Basically, I'll write my name in the reserved spot, taking all this into account I can calculate the coordinates removing 2 or 3 pixels from the top because of the font overhead. The text should also be urldecoded and then on to the function that looks like this:

PHP:
  1. imagestring( $rImg,5,126,22,urldecode($_GET['nome']),$cor );

Before we finish up we have to define our content-header indicatig that our content is a JPG image, and then we can output the image code using the imageJPEG, that will spit out our raw image code. This function can also receive a second parameter that will output the content into the indicated file.

PHP:
  1. header('Content-type: image/jpeg');
  2. imagejpeg($rImg);

This is the final result:

And this is the final code:

PHP:
  1. &lt;?php
  2.  
  3. //Carregar imagem
  4. $rImg = ImageCreateFromJPEG("ex-img.jpg");
  5.  
  6. //Definir cor
  7. $cor = imagecolorallocate($rImg, 0, 0, 0);
  8.  
  9. //Escrever nome
  10. imagestring($rImg,5,126,22,urldecode($_GET['nome']),$cor);
  11.  
  12. //Header e output
  13. header('Content-type: image/jpeg');
  14. imagejpeg($rImg,NULL,100);
  15.  
  16. ?&gt;

Using custom fonts

We can use custom fonts to write our text, like Arial, Verdana or even more exotic ones like Futura or any other font not present by default in typical systems.To use TrueType fonts we can use the imagettftext function that allows us to point a .ttf file of the font we wish to use, as we can see jus below:

PHP:
  1. $font = 'arial.ttf';
  2. imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

1 Star2 Stars3 Stars4 Stars5 Stars (3 votos, média: 3.33 de 5)
Loading ... Loading ...

3 Comments »

March 13th 2007

Optimizing code with __autoload()

With the advent of PHP5 a new tool was available to all who wished to optimize their code, its called __autoload. This function can reduce the time lost by having to include object and class files in your code. But how do you proceed in creating an __autoload function to optimize code?
Continue Reading »

1 Star2 Stars3 Stars4 Stars5 Stars (Sem votos registrados)
Loading ... Loading ...

No Comments yet »

October 12th 2006

AJAX: What is it?

Author's note: This article was first published in my AJAX Column (AjaxOnline.com.br) in Portuguese where my intended audience is a group of beginners learning about the AJAX initiative. Since it turned out to be a really interesting article I decided to translate it and share it with a wider audience.

I wondered where I should begin explaining AJAX and finally decided I should begin explaining what's behind it and how it came around, so everyone knows where we are coming from.

Ajax, web 2.0, RSS, if you've never heard any of these terms than something is wrong, you are either way behind in web technologies or not serious enough about web development. The concept of Web 2.0 is what we may call the "father" of the latest trends in web applications, where various technologies are used to improve the user's experience, but what is the concept? Continue Reading »

1 Star2 Stars3 Stars4 Stars5 Stars (2 votos, média: 5 de 5)
Loading ... Loading ...

2 Comments »

October 2nd 2006

dmsAutoComplete v1.1 - ChangeLog

Recently I published an updated version of my auto-complete (Google suggest) script compatible with IE and FireFox and based on PHP/AJAX.

After publishing version 1.0, I had some feedback from people who downloaded an tested it, so now I decided to correct some of the bugs that were found, and make a few improvements also. So now I’m going to publish version 1.1, check out some of the changes I made. Continue Reading »

1 Star2 Stars3 Stars4 Stars5 Stars (1 votos, média: 5 de 5)
Loading ... Loading ...

4 Comments »

September 28th 2006

Using User-Defined Varibles in MySQL

Cast the first stone he who never made a mistake modeling a database! Every now and then in your career you will be face to face with a problem like this: due to the nature of a table’s data you created a table without a primary key, or using a composed key. So far so good, but due to an upgrade you see the need to have a unique key identifying all the registers in your table, in my case it was due to a AJAX interface.

So what now? You have a table full of data, and of course, as Murphy’s law will tell you, that data cannot be erased. MySQL will prevent you from turning a filled to a primary key if it finds duplicated values in the table. Quite a brain twister, but I did a little research and found a rather simple solution to the matter.

UPDATE: So it actually came to my atention that a query I had already tried does the job in an even simpler form, but my modelling tool executed the commands out of sync and that why i had problems. So this article stays on as a good example of how to use mysql variables.
Continue Reading »

1 Star2 Stars3 Stars4 Stars5 Stars (5 votos, média: 5 de 5)
Loading ... Loading ...

5 Comments »

September 18th 2006

A study on RSS - Part 2: The RSS format

In the last article I made a simple introduction to what is an RSS Feed and showed the path to creating XML files in PHP. Now it’s time to explain the RSS file structure along with some basic history.
Continue Reading »

1 Star2 Stars3 Stars4 Stars5 Stars (2 votos, média: 5 de 5)
Loading ... Loading ...

1 Comment »

September 15th 2006

strtotime() - is it useful?

Every now and then I get e-mails with questions that ask “How can I add X days to a given date?”, “How can I figure the day that corresponds to next thursday?”, and others along the same line. It scares me when I see replies that include enormous codes that execute innumerous function even including some bizarre leap year determination algorithms, I just can’t understand why all the complication and fuss.

The strtotime() function exists to solve these problems and i plan to introduce you to it and show a few usage examples. Also I’m going to check function performance using a simples benchmark comparison. Continue Reading »


1 Star2 Stars3 Stars4 Stars5 Stars (4 votos, média: 5 de 5)
Loading ... Loading ...

2 Comments »

September 1st 2006

WP Widget: BlogBlogs.com.br Favorites

After BlogBlogs released its public API, it became possible to develop plug-ins that used their data to display different information on our blogs. Using this widget its possible to retrieve you favorite blogs list, and keep it synchronized always. Continue Reading »

1 Star2 Stars