PlayStation 3 Media Server on CentOS

Linux » PS3 | Posted by: Rob at 8:03 am on February 28, 2010

Getting a PlayStation 3 Media Server running on CentOS was surprisingly easy, after finding a Java based one here.

Install Java

yum install java

Download and Extract the Files

wget http://ps3mediaserver.googlecode.com/files/pms-linux-1.10.5.tgz
tar -xvf pms-linux-1.10.5.tgz

Make the shell script executable

cd /path/to/extracted/files
chmod 755 PMS.sh

Test it out

./PMS.sh

You should see something similar to the following:

Open Blocked Port

I had to open port 5001 using system-config-securitylevel.

Start PS3 Media Server Automatically

vim /etc/rc.d/rc.local/

And append the following line to the end of the file:

/path/to/pms/script/ ./PMS.sh

Install Codecs

Depending on what kind of media files you want to play, you’ll need to download and install the appropriate codecs.

Getting Python To Work With Apache On Windows

Python | Posted by: Rob at 8:41 am on January 16, 2010

I’ve never really dabbled in python, so I figured I’d install it. The first thing that you need to do is download the latest version of Python here. As of January, 2010, that version would be 2.6.

It comes packaged as an executable, and you should install it. By default, a Python directory should be placed under C:\. If you installed Python 2.6, the directory will be named Python26. If this blog is old, and 2.7 or 2.8 is out, then the directory will be named Python27 or Python28 respectively.

Inside of the Python directory, there is an executable, python.exe, that you’ll need to add to your Windows PATH variable. To do this in Windows Vista, click on the start menu, right click on computer and then left click on properties.

This will bring up your system information from the control panel. Click on the Advanced system settings link in the left pane.

This will bring up a System Properties window. Click on the Advanced tab, then click on the Environment Variables button.

There should be two variable grids. The bottom grid is labeled System Variables, and the top grid is labeled User variables for {windows name here}. Directly under the top grid, click on the New button.

Fill in the Variable name textbox with PATH, and set it’s value to C:\Python26;%, or wherever python’s directory is located on your machine.

If the PATH variable has already been set, just add C:\Python26; to the beginning of the string.

Now if you open up a command prompt, and type in “python”, you should see the following response. If you don’t, try rebooting your machine. If you still don’t, you’ve done something wrong :)


Note: I am running an older version of python in the screen shot, that’s why it says Python 2.5.1

The next step is to configure Apache to use CGI… so open up apache’s httpd.conf file.

Make sure you have the cgi_module enabled. You can search for the following, and make sure there’s not an ; before it.
LoadModule cgi_module modules/mod_cgi.so

Scroll down to your Directory node. Mine is labeled Inside of this node, you should see something like: Options Indexes FollowSymLinks… don’t worry if yours is out of this order, or if you have more/less options. Just make sure you add ExecCGI to the end of it. So now, my has the following in it: Options Indexes FollowSymLinks ExecCGI.

Now, scroll down further, searching for the node . Inside of this node, add the following:
AddHandler cgi-script .py

Restart Apache.

Now create a test.py file with the following content:

  1.  
  2. #!C:/Python26/python.exe
  3.  
  4. print "Content-type: text/html\n"
  5.  
  6. print """
  7. <html>
  8. <head><title>It works</title></head>
  9. <body>
  10. You can now run python scripts! Yay.
  11. </body>
  12. </html>
  13. """
  14.  

Try run it through your web browser and hopefully it’ll work! Note that the first line must contain

  1.  
  2. #!C:/Python26/python.exe
  3.  

Or, whatever the path is to pythons executable.

Resizing Images On The Fly With PHP

PHP » Web Development | Posted by: Rob at 8:43 am on January 10, 2010

Every once in a while it’s useful to be able to dynamically resize an image, on the fly. Especially if you’re running a web service like Gravatar, where users want their images in all sorts of different sizes. Some blogs have their gravatars set to 50×50px, while other have their gravatars set to 75×70px. It wouldn’t make sense for Gravatar to store an image of every size on their servers. It makes sense to store one copy of the image, and then dynamically resize it. Generally speaking though, you should avoid this method since it puts a lot of load on the server (image manipulation happening all the time!). Anyways, here’s a simple example of how it’s done:

getImage.php

  1.  
  2. <?php
  3. if(isset($_GET[‘url’]))
  4. {
  5.         $default_size = 50;
  6.        
  7.         $width = (isset($_GET[‘width’])) ? $_GET[‘width’] : $default_size;
  8.         $height =  (isset($_GET[‘height’])) ? $_GET[‘height’] : $default_size;
  9.        
  10.         require_once(‘lib/Image.php’);
  11.        
  12.         $image = new Image();
  13.         $image->getImage($_GET[‘url’]);
  14.         $image->resizeImage($width, $height);
  15.         $image->displayImage();
  16. }
  17. ?>
  18.  

Image.php (PHP Class)

  1.  
  2. <?php
  3. /**
  4.  * Image
  5.  *
  6.  * @author Rob Keplin
  7.  * @link http://www.robkeplin.com
  8.  **/
  9.  
  10. class Image
  11. {
  12.         public $errors;
  13.         private $_image;
  14.        
  15.         public function __construct()
  16.         {
  17.                 $this->errors = array();
  18.                 $this->_image = NULL;
  19.         }
  20.        
  21.         public function getImage($url)
  22.         {
  23.                 $ext = strrchr($url, ‘.’);
  24.                 $ext = strtolower($ext);
  25.                
  26.                 switch($ext) {
  27.                         case ‘.jpeg’:
  28.                         case ‘.jpg’:
  29.                                 $this->_image = imagecreatefromjpeg($url);
  30.                                 break;
  31.                         case ‘.gif’:
  32.                                 $this->_image = imagecreatefromgif($url);
  33.                                 break;
  34.                         case ‘.png’:
  35.                                 $this->_image = imagecreatefrompng($url);
  36.                         default:
  37.                                 $this->errors[] = "The linked file must be a .jpeg, .jpg, .gif, or .png";
  38.                                 break;
  39.                 }
  40.         }
  41.        
  42.         public function resizeImage($new_width, $new_height)
  43.         {
  44.                 if($this->_image)
  45.                 {
  46.                         //Resize the image
  47.                         $old_width = imagesx($this->_image);
  48.                         $old_height = imagesy($this->_image);
  49.                        
  50.                         //Keep the resolution of the image the same
  51.                         if($old_width > $old_height)
  52.                         {
  53.                                 $ratio = $old_height / $new_height;
  54.                         }
  55.                         else
  56.                         {
  57.                                 $ratio = $old_width / $new_width;
  58.                         }
  59.                        
  60.                         $new_width = ceil($old_width / $ratio);
  61.                         $new_height = ceil($old_height / $ratio);
  62.                        
  63.                         $new_image = imagecreatetruecolor($new_width, $new_height);
  64.                         imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
  65.                         $this->_image = $new_image;
  66.                 }
  67.         }
  68.        
  69.         public function displayImage()
  70.         {
  71.                 if($this->_image)
  72.                 {
  73.                         header(‘Content-type: image/jpeg’);
  74.                         imagejpeg($this->_image, NULL, 100);
  75.                         imagedestroy($this->_image);   
  76.                 }
  77.         }
  78.        
  79. }
  80.  
  81. ?>
  82.  

And BAM – you have full control of the image sizes, without having to use the ugly HTML width and height attributes which produce a pixilated mess.

Here’s some live examples:

– height: 200px

– height: 175px

– height: 150px

– height: 50px

– height: 25px

If you’d like to download the PHP files and manipulate them however you want, go right ahead! Download them here.

Note: You must have php-gd installed in order to use the image functions defined in the Image class.

Integrating Gravatars on Wordpress

PHP » Web Development » WordPress | Posted by: Rob at 6:42 am on

I just noticed that a lot of web applications are using gravatars to help personalize comments. So what are gravatars? They are meant to be a globally recognized avatar. You simply register a gravatar account, and then associate your email address with an uploaded picture. When you leave a post or comment on your favorite (or completely random) blog, the picture associated with your email address will show up next to your comment… as long as the blog has gravatars enabled.

And that’s what I’m going to show you in this post. How to enable gravatars on your WordPress account. It’s surprisingly easy, since there are functions for gravatars built in the WordPress library (if you are using WordPress 2.5+).

  1. <?php
  2.         /**
  3.          * The function you’ll want to use is the following.
  4.          *
  5.          * 1)   The first parameter needs to be the users email address.
  6.          * 2)   The second parameter is the size you want the picture to be.
  7.          * 3)   The fourth parameter is the default image, if the user is lame
  8.          *              and does not have a gravatar account.
  9.          * 4)   The fifth parameter is the alternative text for the image. You know,
  10.          *              the contents in the images ‘alt’ attribute.
  11.          *
  12.          * The 2nd, 3rd and 4th parameter are optional. If the second parameter is left
  13.          * blank, the default image size is 96, which is rather large.
  14.          **/
  15.         echo get_avatar(‘users@email.com’, $size=‘50′, ‘../images/default.jpg’, ‘Gravatar Image’);
  16. ?>

I know what you’re (hopefully) thinking, now. How the heck do you get the commenter’s email address? Well, there’s a WordPress function for that, too. It’s get_comment_author_email(), which was added to WordPress in 1.5.

  1.  
  2. <ul id="commentlist">
  3. <?php foreach ($comments as $comment) : ?>
  4.         <li id="comment-<?php comment_ID() ?>"
  5.                 <?php print ($comment->user_id == 2) ? ‘class="admin"’ : ; ?>>
  6.                 <div class="commentHeader">
  7.                         <strong>
  8.                                 <?php comment_author_link() ?>  - <?php comment_date() ?> at
  9.                 <?php comment_time() ?>:
  10.                         </strong>
  11.                 </div>
  12.                 <div class="photo">
  13.                         <a title="get your avatar" href="http://www.gravatar.com">
  14.                                 <?php echo get_avatar(get_comment_author_email(), $size=‘50′); ?>
  15.                         </a>
  16.                 </div> 
  17.                 <?php comment_text() ?>
  18.                 <div class="clear"></div>
  19.         </li>
  20. <?php endforeach; ?>
  21. </ul>
  22.  

Now you’re good to go. Now go register on Gravatar and leave me some comments :D

Older Posts »