I needed a complete PHP class for the Basecamp API with a project I'm working on at REBEL. Finding nothing that would fit the bill, I wrote one.
http://code.google.com/p/basecamp-php-api/
Sunday, August 9, 2009
Sunday, July 26, 2009
A rant on PHP, quotes and variables
Here are 3 ways to display some text with some PHP variables:
single quotes interspersed with vars:
double quotes interspersed with vars:
double quotes with embedded vars:
The vast majority of PHP code I come across uses the first two methods, breaking in and out of quotes for each variable. IMHO the third way much easier to read and maintain. Granted there are slight performance differences between them, but not enough to be concerned about. I think the reason that version 3 isn't so popular is because most PHP developers don't learn this type of curly-brace syntax (enabling you to embed complex variables into quoted strings) right away, and therefore rarely think to use it.
Of course you can take things a step further with sprintf and really keep things tidy. Let's take a SQL query for a common example.
Here is the "messy" way:
And the much cleaner way:
I hope I brought some ideas to light, happy coding.
single quotes interspersed with vars:
echo 'My name is ' . $user
. ' and I eat ' . $food['favorite'] . '.';double quotes interspersed with vars:
echo "My name is " . $user
. " and I eat " . $food['favorite'] . ".";double quotes with embedded vars:
echo "My name is {$user} and I eat {$food['favorite']}.";The vast majority of PHP code I come across uses the first two methods, breaking in and out of quotes for each variable. IMHO the third way much easier to read and maintain. Granted there are slight performance differences between them, but not enough to be concerned about. I think the reason that version 3 isn't so popular is because most PHP developers don't learn this type of curly-brace syntax (enabling you to embed complex variables into quoted strings) right away, and therefore rarely think to use it.
Of course you can take things a step further with sprintf and really keep things tidy. Let's take a SQL query for a common example.
Here is the "messy" way:
$sql = "select * from MYTABLE where id=" . (int)$info['id'] . " and firstname = '" . mysql_real_escape_string($info['firstname']) . "' limit " . (int)$limit;And the much cleaner way:
$sql = sprintf("select *
from MYTABLE
where id=%d
and firstname = '%s'
limit %d",
(int)$info['id'],
mysql_real_escape_string($info['firstname']),
(int)$limit
);I hope I brought some ideas to light, happy coding.
Thursday, May 14, 2009
Camera image orientation
I was having a confusing problem where images taken from the iPhone camera were being displayed in the wrong orientation. I would take a photo in portrait and save the data to disk. Later when I retrieved the photo it would show up rotated 90 degrees.
The iPhone knows what orientation a photo is taken, and this orientation depends on how the camera is being held when you snap the photo. You can get the orientation value on a UIImage from the property imageOrientation.
When you save the photo, it is possible that this information could get lost, depending on the way you save it. If it does get lost, the UIImageView will assume the photo was taken as a left-side landscape photo, and display it with that orientation.
I ran into a problem when trying to save the camera image with UIImagePNGRepresentation(). For some reason this loses the orientation info. I switched to UIImageJPEGRepresentation() and all is well now.
So basically if you run into the problem, you have 2 choices: Be sure you use a format that preserves orientation data, or rotate the UIImage data directly before saving it to a file.
The iPhone knows what orientation a photo is taken, and this orientation depends on how the camera is being held when you snap the photo. You can get the orientation value on a UIImage from the property imageOrientation.
When you save the photo, it is possible that this information could get lost, depending on the way you save it. If it does get lost, the UIImageView will assume the photo was taken as a left-side landscape photo, and display it with that orientation.
I ran into a problem when trying to save the camera image with UIImagePNGRepresentation(). For some reason this loses the orientation info. I switched to UIImageJPEGRepresentation() and all is well now.
So basically if you run into the problem, you have 2 choices: Be sure you use a format that preserves orientation data, or rotate the UIImage data directly before saving it to a file.
Wednesday, May 13, 2009
Rotating view around arbitrary point the easy way
I have a UIImageView that I want to rotate around a given point. By default, the layer will rotate around the center of the UIImageView. Trying to change this anchor point involves a bit of work, either move at the beginning and reposition all the view elements, or translating the view, rotating, and translating back. The last option works fine, unless you are trying to use core animation to smoothly rotate the view.
I found out a much easier solution to the whole problem. You could call it a workaround, but it works well without disturbing the view you want rotated.
Basically, you create a new UIView. Place the center point where you want the rotation to occur. Change the size of the view so that the view you want rotated will fit inside. Make your view (you want rotated) a subview of the new view. Now, just rotate the new view, and your subview will be rotated correctly.
Example: lets say we have a UIImageView that is 320x480 (the size of the whole iPhone screen). We want it to rotate from the bottom center instead of its middle. Create a new UIView that is 0,0,320,960. This is basically double the height of the screen, positioned so the bottom half is off the screen, which places the center point right where we want it. Now place your UIImageView inside the new UIView in the top half (visible on the screen.) Now when you rotate the UIView, it will pivot on the bottom center of the screen, and your UIImageView will rotate along with it, as expected.
I found out a much easier solution to the whole problem. You could call it a workaround, but it works well without disturbing the view you want rotated.
Basically, you create a new UIView. Place the center point where you want the rotation to occur. Change the size of the view so that the view you want rotated will fit inside. Make your view (you want rotated) a subview of the new view. Now, just rotate the new view, and your subview will be rotated correctly.
Example: lets say we have a UIImageView that is 320x480 (the size of the whole iPhone screen). We want it to rotate from the bottom center instead of its middle. Create a new UIView that is 0,0,320,960. This is basically double the height of the screen, positioned so the bottom half is off the screen, which places the center point right where we want it. Now place your UIImageView inside the new UIView in the top half (visible on the screen.) Now when you rotate the UIView, it will pivot on the bottom center of the screen, and your UIImageView will rotate along with it, as expected.
Friday, May 1, 2009
OS X "burning" image to USB flash drive
I recently downloaded an Ubuntu image for netbooks, and I needed to install this onto a USB flash drive so I could boot it. This is pretty simple with OS X and Disk Utility and dd from the command line. Be sure your flash drive is large enough to hold the disk image.
* Plug the flash drive into a USB port and launch Disk Utility.
* Select the flash drive and press apple+i to bring up the info
* remember the device id, it will be diskN where N is a number, such as disk4
* select the volume (not the device) and click unmount.
* open a terminal window, and type:
Be sure to use your path to the image file, and the device id you got from disk utility in place of "disk4". You can drag the .img file into the terminal window if you don't want to type the full path to it.
Now wait a bit for the image to be written. When its done you will get something similar to this:
* Plug the flash drive into a USB port and launch Disk Utility.
* Select the flash drive and press apple+i to bring up the info
* remember the device id, it will be diskN where N is a number, such as disk4
* select the volume (not the device) and click unmount.
* open a terminal window, and type:
sudo dd if=/path/to/file.img of=/dev/disk4 bs=1024
Be sure to use your path to the image file, and the device id you got from disk utility in place of "disk4". You can drag the .img file into the terminal window if you don't want to type the full path to it.
Now wait a bit for the image to be written. When its done you will get something similar to this:
969568+0 records in
969568+0 records out
992837632 bytes transferred in 521.666371 secs (1903204 bytes/sec)
Wednesday, April 1, 2009
Prediction: First Consumer Petabyte Hard Drive?
The first hard drive I owned came with my Amiga 1200, circa 1990. It held 40 megabytes of data, and I believe it was a $500 option. Yes, I said option. The computer happily ran on floppy disks alone, if you so desired. You had to load the OS off of floppy first, then load your programs from other disks. The A1200 came with 2MB of ram, but I loaded it up with an additional 16MB of RAM, costing a healthy $800. That is just sick.
Back to the subject of hard drives. Here is a rough time line for the consumer hard drive. By "consumer", I mean something that fits in a desktop PC form factor that is in the price range for an average consumer.
The timeline between 5MB and 1GB drives was 12 years.
The timeline between 1GB and 1TB drives was 15 years.
Notice the price drop!
So let's hear the predictions. When will the first consumer-ready petabyte drive be available, and what will the average cost be? How aboute exabyte? Zetabyte? Yottabyte?
A Yottabyte is a tough one to grasp. That is ONE TRILLION terabytes. Do you think storage space, or personal computing as we know it, will end at some point, long before these outrageous sizes are a reality?
If history is any indication, the petabyte drive should arrive in 12-15 years, and average $300 or less. I think it will much sooner, cut that time down to about 8 years. The $300 price is probably close.
I also think solid-state disk storage will become the norm, seeing hard drive platters die like the video tape did.
Back to the subject of hard drives. Here is a rough time line for the consumer hard drive. By "consumer", I mean something that fits in a desktop PC form factor that is in the price range for an average consumer.
1980 5MB ~$1,000.00
1992 1GB ~$1,000.00 (1 gigabyte ~ 1000 megabyte)
2007 1TB ~$300.00 (1 terabyte ~ 1000 gigabyte, or 1 million megabyte)
???? 1PB ???? (1 petabyte ~ 1000 terabyte, or 1 million gigabyte)
???? 1EB ???? (1 exabyte ~ 1000 petabyte, or 1 million terabyte)
???? 1ZB ???? (1 zetabyte ~ 1000 exabyte, or 1 million petabyte)
???? 1YB ???? (1 yottabyte ~ 1000 zetabyte, or 1 million exabyte)The timeline between 5MB and 1GB drives was 12 years.
The timeline between 1GB and 1TB drives was 15 years.
Notice the price drop!
So let's hear the predictions. When will the first consumer-ready petabyte drive be available, and what will the average cost be? How aboute exabyte? Zetabyte? Yottabyte?
A Yottabyte is a tough one to grasp. That is ONE TRILLION terabytes. Do you think storage space, or personal computing as we know it, will end at some point, long before these outrageous sizes are a reality?
If history is any indication, the petabyte drive should arrive in 12-15 years, and average $300 or less. I think it will much sooner, cut that time down to about 8 years. The $300 price is probably close.
I also think solid-state disk storage will become the norm, seeing hard drive platters die like the video tape did.
Sunday, March 29, 2009
iPhone 2.2.1 available fonts
Here is a list of the fonts available to the iPhone as of SDK 2.2.1:
Family: Courier
Font: Courier
Font: Courier-BoldOblique
Font: Courier-Oblique
Font: Courier-Bold
Family: AppleGothic
Font: AppleGothic
Family: Arial
Font: ArialMT
Font: Arial-BoldMT
Font: Arial-BoldItalicMT
Font: Arial-ItalicMT
Family: STHeiti TC
Font: STHeitiTC-Light
Font: STHeitiTC-Medium
Family: Hiragino Kaku Gothic ProN
Font: HiraKakuProN-W6
Font: HiraKakuProN-W3
Family: Courier New
Font: CourierNewPS-BoldMT
Font: CourierNewPS-ItalicMT
Font: CourierNewPS-BoldItalicMT
Font: CourierNewPSMT
Family: Zapfino
Font: Zapfino
Family: Arial Unicode MS
Font: ArialUnicodeMS
Family: STHeiti SC
Font: STHeitiSC-Medium
Font: STHeitiSC-Light
Family: American Typewriter
Font: AmericanTypewriter
Font: AmericanTypewriter-Bold
Family: Helvetica
Font: Helvetica-Oblique
Font: Helvetica-BoldOblique
Font: Helvetica
Font: Helvetica-Bold
Family: Marker Felt
Font: MarkerFelt-Thin
Family: Helvetica Neue
Font: HelveticaNeue
Font: HelveticaNeue-Bold
Family: DB LCD Temp
Font: DBLCDTempBlack
Family: Verdana
Font: Verdana-Bold
Font: Verdana-BoldItalic
Font: Verdana
Font: Verdana-Italic
Family: Times New Roman
Font: TimesNewRomanPSMT
Font: TimesNewRomanPS-BoldMT
Font: TimesNewRomanPS-BoldItalicMT
Font: TimesNewRomanPS-ItalicMT
Family: Georgia
Font: Georgia-Bold
Font: Georgia
Font: Georgia-BoldItalic
Font: Georgia-Italic
Family: STHeiti J
Font: STHeitiJ-Medium
Font: STHeitiJ-Light
Family: Arial Rounded MT Bold
Font: ArialRoundedMTBold
Family: Trebuchet MS
Font: TrebuchetMS-Italic
Font: TrebuchetMS
Font: Trebuchet-BoldItalic
Font: TrebuchetMS-Bold
Family: STHeiti K
Font: STHeitiK-Medium
Font: STHeitiK-Light
Subscribe to:
Posts (Atom)