endenizen

May 20, 2010

Google IO: The Hits Keep Coming

First, an announcement from today: the next version of Android. Better performance, wifi tethering (!), ‘update all’, and more! I can’t wait to get this. These are much-needed updates that make the platform much more powerful. Check out the embedded video below for details.

[Via: Google Announces the Next Version of Android.]

New fonts on endenizen.netNext, an announcement from yesterday: easy web fonts! The previous solutions to non-web-fonts-on-the-web involved rendering images, embedding flash objects, or using canvas (when it was available). These methods might have gotten the job done, but they were difficult to implement and each had its share of drawbacks.

Along comes Google Font API and all those problems disappear. Now you can easily use real fonts without adding complexity. Best of all, it’s cross-browser (even IE6!) and works just the way it should: just set the font-family in css. I’ve already added a new font to my blog which you can see in the screenshot. It really was as easy as they say:

<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Tangerine">
<style>
  body {
    font-family: 'Tangerine', serif;
    font-size: 48px;
  }
</style>

(Note: I’m not actually using Tangerine. I tried it, and it’s gross.)

[Via: Making Good Typography on the Web Easier: Google Introduces Font API and Directory.]

January 8, 2007

HOWTO: ImageMagick and PDFs

ImageMagick is a handy tool for manipulating graphics from the command line. In essence, it’s a basic image editor and will do any standard resizing, color changing, cropping, rotating, etc. With a little cleverness and magick, though, this app is a powerhouse with many features Photoshop can only dream about. I’ve been using ImageMagick (and its variants for perl and php) for a few years and I’m still learning new things about it all the time. One of its coolest features is the ability to work with many different file formats, including PDFs. I’ve been dealing with some PDFs recently that I’d like to turn into sample images (ala “try before you buy”) and before today I had a hard time even loading the image into memory but now I know why.

When dealing with PDFs, ImageMagick rasterizes each page of the file, loading these rasterized images into memory as it works it’s way through the entire file. With a file that’s 5-10 pages, this isn’t a problem. With a file that’s over 500 pages, this is practically impossible. One way to get around the problem is to load only a small part of the PDF by specifying the pages after the filename. In PerlMagick (if I want to load the first page):

$image = new Image::Magick;
$image->Read(‘thefile.pdf[0]‘);

This rasterizes the first page of the file without loading anything past that page. To write the image to disk (in a format that doesn’t require Acrobat Reader) I can use the following:

$image->Write(filename=>’page1.gif’);

If you’re following along on your own computer, you’ll now notice that the new image looks horrible. The quality is severely reduced from the original making most (if not all) text unreadable. This is a problem since PDFs typically contain a lot of text.

The quality is reduced because ImageMagick reads the image in at screen resolution (72 dpi). When the PDF is viewed on the screen by Acrobat Reader, the vector-based text has more information and can be anti-aliased to look decent even when zoomed out very far. When removing the vector information, the image loses all of that helpful data. One way to work around this is by setting the resolution (and in turn, how much of the vector information you’d like to retain) before loading the image.

$image->Set(density=>’300′);

This will set the resolution to 300 dpi. This is typical magazine resolution so it’s plenty for my purposes. Now, however, the resulting image is huge. To fix this, I’ll simply resize the image to a more web-friendly size. The following command will make the picture 500 pixels wide:

$image->Resize(‘500′);

When resizing an image after loading it in at a high-resolution, ImageMagick is able to use the extra data to anti-alias the text, making it look almost as good as the original pdf.

Here are the new commands in order with the rest of the steps (and an extra one to free up the memory):

$image = new Image::Magick;
$image->Set(density => ‘300′);
$image->Read(‘thefile.pdf[0]‘);
$image->Resize(‘500′);
$image->Write(filename => ‘page1.gif’);
undef $image;

Now with a loop, it’s easy to step through each page in the document and convert it. In my program, I only want to use five pages to give someone a sample of the full document. I could use the first five, or jump through the document giving the user a look at the beginning, middle and end.

If you have any questions or notice any errors, let me know by leaving a comment below. Thanks for reading!

September 22, 2006

Dear Diary, I <3 MySQL

Today I wrote my first stored procedure. It was pretty exciting. I grew up with MySQL and it has finally grown up too. Joining the ranks of IBM’s DB2 and MS SQL, MySQL now allows the usage of pure fanciness (that’s the one-word summary of everything you can do with stored procedures). Now, don’t be mistaken, my stored procedure was anything but fancy. In fact, it was a “Hello World!” of sorts (and if you’re not familiar with that and you’re still reading, well you must really like me because this is probably going way over your head…).

Stored procedures are most popular with admins who need extra speed and security, and less network traffic. A bank, for instance, may use a stored procedure rather than exposing the database code used to execute a query to prevent anyone from modifying that query before it is sent to the server. Using a stored procedure, the query would be stored on the server and referenced with a procedure name. The only code the end-user (the web-developer, or an evil hacker) would be allowed to change is the parameters they pass to the procedure. Here’s a real example:

Moving money from one account (frank) to another (bob) may look like this:

INSERT $5 INTO bob SELECT $5 FROM frank

If I was a developer, I might perform evil magic by changing this to:

INSERT $4.99 INTO bob INSERT $0.01 INTO brian SELECT $5 FROM frank

Clearly this is a bad idea, because a penny is large enough to notice, but say I were selecting much smaller amounts from a much larger pool of transactions… it would be like taking pennies from the tray. Not the jar, that’s for the crippled children. The tray is for everyone.

Using a stored procedure the code (which is stored on the database, never to be changed) might be:

PROCEDURE transfer (to, from, amount)
INSERT INTO to SELECT amount FROM from

Now, the developer has to run a procedure and the procedure may only be passed two accounts and an amount. Presuming the bank went so far as to log each transaction, this would mean any malicious activity would show up because it would have to be a performed in a separate transaction. For the evil hacker, the most they could do is a SQL injection to modify or add parameters which, in this case, would be much less of a problem than if they could add on their own queries.

Security is only one of the many benefits of stored procedures, and most DBMSs have had them for years. Before version 5.0, MySQL didn’t have any stored procedures and therefore didn’t occupy much of the financial, or otherwise important, sectors of the IT market. That’s not to say MySQL’s use in these areas will now increase dramatically, but certainly more database administrators will weigh MySQL’s benefits as a clear competitor with costly alternatives like MS SQL.

Disclaimer: Don’t try this example code at home, it probably won’t work and was only used for very simple illustration. There isn’t actually a way to move money in a bank by referencing $5, that’s just dumb. Also, I don’t really have a ton of stored procedure experience (hence the “Hello World!” comment above) so as always, do your own research and see what works best in your situation.

April 18, 2005

Acrobat …useful?

Ooh ooh, found a cool trick. You know when you try to load a pdf file and it takes 3 hours? Adobe Acrobat reader takes forever to load, crashes, and tries to update every 5 seconds. BUT! there is at least a solution to the slow startups and frequent crashes. I tried this out and it starts up in half a second now.

Open windows explorer and go to the folder where acrobat is installed. It’s usually something like C:\Program Files\Adobe\Adobe Acrobat 6.0\Acrobat. You should see a lot of folders and files in here. Find the folder named plug_ins and rename it plug_ins_old or plug_ins_stupid or anything else so that Acrobat won’t use it. Now make a new folder named plug_ins. Copy the files EWH32.api, printme.api, and Search.api from the old folder to the new folder. There, now you’ve tricked Acrobat into using the new folder and all the useless plugins will be disabled because it can’t find them any more. Hooray!

April 16, 2005

Trent is my hero. Still.

Click here to download the samples used in the new nine inch nails album. Yea, that’s right, make your own songs with the same sounds that Trent used. Holy crap this is crazy!

If anyone messes with this let me know, I want to hear some mixes! I’ll probably throw together something of my own if I get a chance.

The files are for garageband but if you’ve got a pc, there’s still some hope. But it might take a while before you can actually use the files…

Ok, so the original file is a .sit file. Just like a .zip but the crazy “mac” way of doing it. Go to http://www.stuffit.com/win/index.html to download a program that will let you extract the file.

Now you’re stuck with a .dmg file. (oh how fun…) You’ll need to get this little tool (this is where it might get tricky) http://vu1tur.eu.org/tools/ to change the dmg file (a mac-format cd image) to an iso (a pc-format cd image). I had trouble getting the windows binary to do anything other than crash on a memory error… But if you have access to a linux box (or have perl installed on windows) you can use the perl script to convert the file.

Once again, you still can’t use it …yet. The cd image can be read by your pc but the file format is still mac-isized. Click here to get daemon tools. You can use this to emulate the cd drive (acts like the cd is in your drive even though it’s still just a file on your computer).

Now you can use mac drive (http://www.mediafour.com/products/macdrive6/freetrial.asp) to get to the files and finally…copy them to your computer.

That was altogether too much to do… If you’d just like the files, let me know and I’ll get them to you.