Archive for the 'Programming' Category

06/13/2007

I am doing a Web project using the CakePHP framework for my “day job”. I have said it before, and I’ll say it again, I love the user community and the user contributed code that CakePHP offers at the bakery. I am displaying some images on a particular section of the site and want these images to be uploaded by end users at 640×480 through a Web based interface. I then want these images to be displayed first on the site as thumbnails, however, I didn’t want the end users to have to learn how to resize and upload multiple images. Where did I turn? That’s right, kids! The CakePHP user community!

I found a really nice tutorial with code titled “Thumbnail Generation with phpThumb” at the bakery and dove in. The tutorial uses an open-source thumbnail generation PHP application called phpThumb() (imagine that!). I followed the tutorial line by line and had no issues creating the controllers and implementing user specified this “vendor” application into CakePHP and my web application. However, I did have some issues initially due to a lack of understanding of how phpThumb() works. Here’s what I mean.

The thumbnail generation script that you create with the afore mentioned tutorial and phpThumb() can accept three parameters: height, width, and quality (for JPEGs). When I was calling the script I was taking it back to a thumbnail generator I created a while back. I was only passing the width and quality or the height and quality. I figured the script would automagically determine the other parameter. Well, it will if you specify values for height or width that are less than the default or user defined maximum height and width. If you don’t, it will suppress your image to whatever the maximum height or width values are.

Here’s an example: my script, and probably every default download, has the maximum width and height set to 200px. So, if I were to specify a 600 pixel width for a photo that is 640×480, phpThumb() would try and resize my image to 600×440 (or something, heck I don’t know), but, because 440 is bigger than 200, which is the default for the height, it would make the image whatever it needed to to keep the height at 200 pixels high.

So, the smart thing to do, and probably what I should have been doing all along, is to specify the maximum height and width that you want the given image to be. phpThumb() will resize the image as close to that size as it can while keeping the aspect ratio the same. Pretty swift! I just didn’t know exactly how phpThumb() worked so I did meet some frustration along the way and thought the script was jacked up or something…

Maybe this will save someone out there some frustration. I know it will me in the future. I always need friendly little reminders from myself.

Until next time…

06/11/2007

If you’ve never heard of Lightbox or Lightbox 2, it is an extremely sexy way to display images within a web site. And, I should clear this up. You’re not actually displaying images within a web site, but rather on top of the web site.

Lightbox and Lightbox 2, hereafter referred to as simply Lightbox, uses some nifty CSS markup and JavaScriptto display a transparent overlay on top of the current page being viewed within the browser and then displays the image/photo in a nifty little box on top of that. With Lightbox, you don’t have to be worried about images pushing the limits of your sites design, i.e. column widths, when displaying images or photos.

Lightbox is extremely simple to install and implement. However, I was unable to find any documentation on how to get Lightbox incorporated into my CakePHP framework (for more information on CakePHP, read this or visit www.cakephp.org). I have used Lightbox in a project for my day job before but not when using the CakePHP framework and I had to do some messing around before I got this working. Probably only because I don’t fully understand the CakePHP framework and all of its helpers.

In CakePHP, it is extremely easy to link CSS and JavaScript files into your site’s layout. These types of files have to be included within the <head> and </head> tags of the HTML that is sent to the browser. If you follow the CakePHP standards for file locations, you would drop your CSS files into /app/webroot/css/ and your JavaScript files into /app/webroot/js/. Once your files are located in these locations, you add one line of code for each respective file type you want to link, like so:

CSS:

<php echo $html->css ( "myCssFile.css", "stylesheet", NULL, FALSE ); ?>

JavaScript:

<php

if ( isset ( $javascript ) ):
  echo $javascript-$gt;link ("myJsFile.js" );
endif;

?>

Easy enough, right?! Well, sure. Just add the following lines of code to your site’s layout in /app/views/layouts/ and you’re good to go… mostly…

<php

echo $html->css ( "lightbox", "stylesheet", NULL, FALSE ); ?>;

if ( isset ( $javascript ) ):
  echo $javascript-$gt;link ("prototype.js" );
  echo $javascript-$gt;link ("scriptaculous.js" );
  echo $javascript-$gt;link ("lightbox.js" );
endif;

?>

The above lines of code assume you’re using Lightbox 2 and you have placed the necessary files into the recommended folders within the CakePHP file system. The newest version of Lightbox has some display effects that are derived from the Prototype and Scriptaculous libraries. Therefore, you have to include those files as well. Please note that if you aren’t going to be using any of the other features available from the Scriptaculous library, append ?load=effects after scriptaculous.js in the above code.

You also need to make sure that any paths to images in the CSS and JavaScript files point to the correct location of the images included in the Lightbox download.

So, everything is great, right?! I should just be able to put rel="lightbox" in my <a> tags and when I click the link to go to an image, Lightbox should take over and help a brother out, right?! No! I had to add the JavaScript helper to my controller within CakePHP and then I was good to go! To do this, add the following line of code to your controller:

var $helpers = array ( "Javascript" );

If you are already specifying $helpers within your controller, just append:

, "Javascript"

…to the end of your helper strings you have defined like so:

var $helpers = array ( "Html", "Ajax", "Javascript" );

Like I said earlier, if I had a better understanding of CakePHP and which helpers were included by default (I know, I know, I should have looked at the pages controller…), I probably wouldn’t have had to mess with this. But, if anyone else out there is a CakePHP newbie like myself and wants to use Lightbox, or any other external JavaScript file, this should get you pointed in the right direction.

Until next time…

06/08/2007

I have been a long time Zend Studio user for developing web sites and PHP applications. Now, don’t get me wrong. I am a huge fan of the Zend development environment. It is a multi-platform application (thanks to Java), has excellent code completion and highlighting capabilities, and is overall a well designed IDE. But, some of its turn-ons are also turn-offs.

Java is extremely bulky. I have never been a fan of Java applications. Now, there’s nothing wrong with Java or Java based applications, but I just haven’t ever particularly liked Java apps. I prefer an application that is native to the operating system I am running, which these days is Mac OS X on an Apple MacBook. When I used to run FreeBSD exclusively, I preferred applications that would run natively on FreeBSD. But, I was able to run Zend Studio on FreeBSD due to the fact that it runs in a Java Environment. See what I mean?! The turn-ons are turn-offs and vice versa… It’s like a catch 22.

Zend Studio also has a ton of useful features for developing web sites. Utilities related to SQL and debugging, etc. Basically, a lot of features I never used. I only used the development portions of the environment so I really don’t know how good the other utilities in the Zend Studio are for development.

So, what if you could have an application with an excellent development interface with code highlighting and completion, built-in previewing of your applications, a graphical CSS editor, a built-in SSH client for connections to a remote server or a local shell, a PHP manual/language reference, and books. That’s right… books! And, what if this development environment was developed to run natively on Mac OS X?! Sound good (to you Mac users, anyway)?!

Well, now is your chance. This software may not be new to you, or the general public as a whole, but it is to me. The application is called Coda (cool web site, eh?!), developed and distributed by Panic, Inc. Imagine everything you love about Zend and runs natively on your Mac… Plus it has books! That’s right… books!

I have been using Coda for a little over a month. The interface is extremely sexy (well, it does run on Mac) and intuitive. The way sites are displayed is extremely nice with a screenshot preview of the site you have defined. It is a lot more fun to choose a site to work on based on a home page preview than from a list. Not real functional, but definitely a plus in the eye-candy department. Having all of the built-in features (that I described earlier) at your disposal within this easy to use window is extremely helpful, and helps reduce desktop clutter.

So, since I have been using Coda, I have been very impressed with the IDE. It is intuitive (which I said before), extremely sexy (which I said before), robust in the functionality arena, and it has books… Have I mentioned the books?!

Now, I haven’t used the books, but I have “virtually” thumbed through them. Well, it’s really only one book but it is divided into three parts: HTML, CSS, and JavaScript. The book these subsets are from is titled The Web Programmer’s Desk Reference from No Starch Press. It is extremely handy to have these references right in your development environment for easy access when you need to know what property X does in CSS, or what arguments function Y accepts in JavaScript.

And my last words are this: If you’re a Mac OS X user and also a web developer, I highly recommend you give Coda a try. There is a “Try Before You Buy” download available from the Panic web site (again, nice web site). Give it a try and let me know how it compares to your current development environment. I’d be interested to see what a.) other development environments and tools you all use, and b.) what you think of Coda.

Until next time…

06/07/2007

I have been looking for a rapid development framework for PHP for a little while now and stumbled upon CakePHP. The CakePHP web site describes the frame work as follows:

Cake is a rapid development framework for PHP which uses commonly known design patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. Our primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility.

I am currently using the framework on a production project and have been very happy with the way everything is organized and how it all relates. It is starting to make sense to me and I am very happy so far with my choice. Everyone says the framework is basically Rails for PHP (ever heard of Ruby on Rails?). Since I have never programmed in Ruby (nothing extravagant anyway), nor used the Rails framework with Ruby, I cannot validate this comment but will take everyone’s word for it.

I have had some people ask me (you know who you are) and seen others asked in the #cakephp IRC channel on irc.freenode.net why people don’t just use Ruby on Rails. My answer to that is simple, to me anyway: I have been using PHP since around late 1999 and have been happy with it ever since. My code is getting better and better, more efficient, better organized, etc. I don’t have time right now in my life to learn a new programming language and a development framework. I need to use something now that will allow me to turn out applications and robust dynamic sites with the quickness.

One thing I really like about CakePHP is the user/developer community. There is a lot of user contributed code available for use at the CakePHP Bakery. The range of examples and working applications on that site is enormous (huge-mongous?). I have found a lot of examples that I have been able to include into the site I am currently working on and some things I hope to implement after launch.

Again, I have been very happy with my CakePHP experiences thus far and hope that trend continues. I will be blogging about my experiences, and possibly be making notes in my blog for my own future reference, from here on. If you’re a PHP developer and are itching to try something new to develop sites and applications quicker and with more reusable code, I would give CakePHP a try. It may be worth it in the long run.

Until next time…

06/07/2007

Every time I log into my blog, obviously not to write anything, I am greeted with the latest release announcement from the WordPress team in my dashboard. I have been meaning to upgrade for a while, basically, ever since it was announced (3 weeks, 2 days, 1 hour, and 12 minutes ago) but just got around to doing so this evening. The upgrade went smooth, as always. I’m just dreading the upgrade on averageadmins.com (aa) (which is happening as I write this).

I have customized some of the core functions in the WordPress code on the aa site and they are always overwritten when I perform an upgrade. Then, I get to spend about 2 hours sifting through code trying to determine which functions do this, and which ones do that. You would think I would document these functions somewhere so I didn’t have to do all that searching every time WP upgrades their software… Oh, well… Such is life…

Until next time…

 

Categories