Remove Part of File Names using PHP


Since David’s Batch Processor was pre-installed in GIMP on Ubuntu Studio, I used it extensively in the ICAF Gallery project today. My goal for these images is to resize their thumbnail version to 10KB or smaller and retain the original size for the enlarged version. Because most of these images are in different resolution, I randomly selected the scale value in the Batch Processor to resize the images. Then I had to manually view the file size of each image to see whether or not an image has to be resized again. After the differentiation, I moved the images that need to be resized once more to a new sub-directory and started the above process one more time. I would repeat these steps again and again until all the thumbnail version of the images are under 10KB.

During the early use of the Batch Processor, I missed a very important feature that is to save the newly processed images to a different folders. This resulted my decision to write this Batch File Name Remover script. The source code for this script is very easy to understand. It’s just some PHP functions plus two Linux commands: ls and mv. I always place a postfix after the file name of processed thumbnails: “-thumb”. Because of that, I need to remove this postfix whenever I repeated the above resize steps. The ls command is used to list the file name that contains the specified characters into an array. The mv command is used to remove the specified characters from the file name using PHP explode function. I used PHP instead of native Linux Bash script because PHP is easier than Bash for me :) . The source code for this script is available here.

Below is the Batch File Name Remover in action:

Posted in GNU/Linux, Web Development | Tagged , , , , , , | Leave a comment

Transitioning to Ubuntu Studio


Ubuntu Studio LogoYesterday I switched to Ubuntu Studio 10.04 mainly because I love the audio production tools available in the distro. The first time I tried to install Ubuntu Studio from the original DVD was a failure. That’s probably because this distro is usually for multimedia workstation, therefore it doesn’t install NetworkManager by default. As the result, I didn’t get very far with this original distribution because of the wifi struggle.

Instead, I reinstalled Ubuntu 10.04 and upgraded to Ubuntu Studio by following this Ubuntu Documentation article. Here is the video preview of the Application menu in the Original Ubuntu Studio:

Note that I installed the distro inside VirtualBox using original DVD ISO image in the above video to show the different default interface between Ubuntu and Ubuntu Studio. The version of Ubuntu Studio upgraded from the original Ubuntu installation is very different, notably the NetworkManager.

I’m currently in the process of transitioning to this new Ubuntu environment. I have many tasks to complete like deciding which applications to reinstall and restore their configurations from my backed up home directory, etc. Once I finished all these tasks (probably today), I will start to learn about the tools available in Ubuntu Studio like LMMS and StopMotion. Look forward to the upcoming posts about these tools.

Posted in GNU/Linux | Tagged , , , , , , , , | Leave a comment

Changing Back to the Original Schedule and First Video Post (Updated)


Update (08/25/2010): The video quality has been improved thanks to OpenShot. Note that to watch the original HD quality, you must switch to 720p or 1080p. I also fixed the PHP script to exclude the XML file inside the background.xml file so that the slideshow can run smoothly without an empty image.

After weeks of consideration, I finally decided to change back to my original post schedule of one post per day. Two posts each day simply doesn’t work for me.

I also plan to make a video version of the post along with each post. Here is my first video showing how to create a wallpaper slideshow in Ubuntu, one of the popular posts I wrote back to March of this year:

The steps in the video are different from the post. I recorded this video this afternoon. Just before that, I wrote a PHP script that can generate background.xml file for me.  You can download the PHP script here:
http://blog.robbychen.com/Ubuntu/slideshow.tar.gz

To use this script, you must first change the permission of the folder where the images are to 777. You may change it back to original permission after that for security reason.

If you have any problem running this script, please leave a comment below.

Posted in GNU/Linux, Web Development | Tagged , , , , , | 1 Comment

C Increment and Decrement Operators


C Plus Plus Minus MinusPrograms coded with short-hand operators can run several milliseconds faster. Same as increment and decrement operators. They are — (minus minus) and ++ (plus plus). The following example shows the usage of these two operators:

x = y + z++; // z is incremented by one after x was calculated
x = y + --z; // Decrement z by one first, then calculate x

In the above example, if y = 2 and z = 4:

x = 2 + 4++ => x = 6 (z has become 5, x doesn’t change since it’s already calculated)
x = 2 + –4 => x = 2 + 3 => x = 5 (z first decremented to 3, and then calculates x)

As you can see, the position of the operator is very important.

Here is the sample code for these operators:

/*
 * inDecrement.c
 *
 * Program to illustrate the use of the increment
 * and decrement operators
 *
 * by Mark Virtue, 2001.
 *
*/

#include <stdio.h>

main() {
 int x, y, z = 1;

 y = 5;
 x = 6;

 puts(""); // Prints out a new line
 printf("Before: x = %d, y = %d, z = %d\n", x, y, z);

 z = x++ + ++y; // First y is incremented to 6, and then z gets calculated to 12. Lastly, x is incremented to 7

 puts("");
 puts("z = x++ + ++y");
 printf("After: x = %d, y = %d, z = %d\n", x, y, z);

 puts("");
 printf("Before: x = %d, y = %d, z = %d\n", x, y, z);

 z = x++ - --y; // Same format as above

 puts("");
 puts("z = x++ - --y");
 printf("After: x = %d, y = %d, z = %d\n", x, y, z);

 fflush(stdin);
 getchar();
}

And here is the output for the above code:

C Increment and Decrement Operators

Output for the above sample code. Click the image to enlarge.

If you have any question regarding the increment and decrement operators, leave a comment below.

Posted in Computer Programming | Tagged , , , | Leave a comment

Running Sims 3 under PlayOnLinux in Ubuntu Linux


The Sims 3 running on GNU/LinuxBack when I was using Windows Vista years ago, I played The Sims 3 and other games for several hours. Once I switched to GNU/Linux, I got rid of these games when I failed to run them under Wine. However, for some reasons, I found a folder which stores The Sims 3 ISO file while cleaning and organize the hard drives today that I downloaded from a website that I don’t remember anymore. I searched over on the Wine database and found that the game and its expansion packs are in the gold state which I assume that it works very well on the GNU/Linux platform.

After searching around in the Ubuntu repositories, I discovered PlayOnLinux which is the front-end of Wine. I installed it and found out that there are almost all the Windows software and games that are pre-configured by PlayOnLinux. One thing that I discovered when I was running The Sims 3 under PlayOnLinux is that the sound would go out about one minute after running the game. The following is the solution that I came up with the help of this PlayOnLinux forum thread:

  1. Open the terminal and type playonlinux to run the GUI which you can add to the GNOME Menu manually with this command. Read this article on detailed instructions on how to do this.
  2. Install the game by clicking the Install button on the toolbar and follow the instruction.
  3. Run the game you installed. If it doesn’t have any sound problem, enjoy the program. Otherwise, go on to the next step.
  4. Follow the instruction on the first post of this thread under the FIX section.
  5. Instead of using last step on that thread, create a launcher on the desktop with the following information:
    Type: Application
    Name: The program name that you installed in step 2. In my case is Sims 3.
    Command: padsp playonlinux –run “The Sims 3″
    (Replace The Sims 3 with the program name that you want to run, note that this name must exactly the same with the name listed in the PlayOnLinux main window, otherwise it will fail to run)
    Comment: Optional. You can add your own description.
  6. You can add an unique icon by clicking the button on the left and select a JPG image.
  7. Click the launcher that you created and enjoy the game.
  8. You can also add this launcher to the GNOME menu by following the instruction from the link I provided in step 1.

If you have any problem for the above steps, leave a comment below and I will try to help as quick as possible. Note that I will not answer comments other than sound problem, especially the CD-related issue for the legal concern of my hosting provider.

Posted in GNU/Linux, Open Source | Tagged , , , , | Leave a comment

My Latest Project


I have been volunteering at ICAF (International Child Art Foundation) since April of this year. When I first started off, I was assigned to be a video editor to edit pre-recorded videos for some international festivals. The reason why I selected this position is probably because I put the video editing skill on my resume :) I used Kdenlive and Openshot interchangeably to edit these videos. Sometimes I also used Audacity to sync between left and right channel of the videos.

After three months of video editing, I was just beginning to love to edit videos. However, I assigned to redesign the gallery page on the ICAF website two weeks ago. This is my second  web development challenge. My first challenge, as you can see on my website, was to build the first website for another non-profit organization. Although that project was abandoned long time ago because the interruption of school work, I learned the existence of server-side scripting at the time and wrote the first PHP application, the contact form on my website. This time I need to place thousands of children’s artworks and some other pictures to the gallery page in an organized layout.

I know that I have to use pagination for this project. It’s my first time to create pagination pages using PHP. The original design shown below was using JqPageFlow jQuery plugin to implement the pagination as the user scroll down the page.

ICAF Gallery Original Desgin

ICAF Gallery page original design

After some more research, I thought that the infinite scrolling is not good for bookmarking since the manager specifically pointed out that he wants to have a back button for each page. Therefore I revised the design to use the traditional pagination method similar to Google:

ICAF revised gallery layout

Revised ICAF Gallery page layout

Based on the above design, I wrote a test page to test my pagination code. This page is still under development. I will integrate the code into the original page once I completed the testing stage. You can download the source code for this page here (I didn’t include the image files since these images are over 1.5 GB). Note that I used exec PHP function to execute ‘ls’ Linux command to output the image file names to an array rather than use a loop or a database table to process file names. This page also contains a script to split different parts of the file name since these names followed a specific pattern: “country-firstNam_lastName-age-thumb.png” as well as some knowledge I got from the C for loop tutorial. Also note that this is the test code, meaning that I haven’t added extensive comments to the code yet.

http://php.net/manual/en/function.exec.phphttp://php.net/manual/en/function.exec.php
Posted in Web Development | Tagged , , , , , , , , , | 1 Comment

C Advanced Operators


There are some advanced operators in C. First set of these operators is the Assignment Operators. Here are five operators presented in the arithmetic assignment operators:

  • += (Plus Equals)
  • -= (Minus Equals)
  • *= (Multiply Equals)
  • /= (Divide by Equals)
  • %= (Modular Equals)

For example, x += y – 3 is the same as x = x + y – 3.

Other assignment operators that are not commonly used are:

  • &&= (And Equals)
  • ||= (Or Equals)
  • &= (Bitwise And Equals)
  • |= (Bitwise Or Equals)
  • ^= (Bitwise Not Equals)
  • >>= (Bitwise Shift Right Equals)
  • <<= (Bitwise Shift Left Equals)

Here is the sample code for the arithmetic assignment operators:

/*
 *
 * assignment.c
 *
 * Program to demonstrate the use of assignment operators
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 int x, y;

 y = 3;
 x = 100;
 x /= y - 1;    // It's the same as x = x / (y - 1), therefore it can substitute to x = 100 / (3 - 1).

 printf("x = %d\n", x);    // The answer is x = 50.

 fflush(stdin);
 getchar();
}
Posted in Computer Programming | Tagged , , | Leave a comment

C String and Character Functions Samples


Here are the exercises I did for the string and character functions:

/*
 *
 * exercise1.c
 *
 * Allow user to input a string from the keyboard and
 * display the ASCII value for each character
 *
 * by Robby Chen, 2010
 *
 */

#include <stdio.h>

main() {
 char string[51];
 int character;

 puts(""); // Prints a new line
 fputs("Please enter a string (less than 50 characters, including spaces): ", stdout); // Does not add the new line at the end
 fgets(string, 51, stdin);
 puts("");
 puts("Character ASCII Value");
 puts("-------------------------");

 for (character = 0; string[character] != '\0'; character++) {
  if (string[character] == '\n') { // prints the new line character and its ASCII code
   printf("    \\n%12d\n", string[character]);
  }
  else { // prints individual letters of the string and their ASCII codes
   printf("%5c %12d\n", string[character], string[character]);
  }
  puts("-------------------------");
 }

 puts("");
 fflush(stdin);
}

Note that the above code contains ASCII-related technique. You can read more about ASCII characters set here if you haven’t already done so above.

/*
 *
 * exercise2.c
 *
 * Allow users to enter their names from the keyboard
 * and output the names with upper case on the first letter
 * and lower case on the other letters of each word
 *
 * by Robby Chen, 2010.
 *
 */

#include <stdio.h>
#include <ctype.h>

main() {
 char name[51];
 int letter;

 fputs("Please enter your name: ", stdout);
 fgets(name, 51, stdin);

 fputs("Hello, ", stdout);

 for (letter = 0; name[letter] != '\0'; letter++) {
  if (letter == 0 || isspace(name[letter - 1])) {
   name[letter] = toupper(name[letter]);
   printf("%c", name[letter]);
  }
  else {
   name[letter] = tolower(name[letter]);
   printf("%c", name[letter]);
  }
 }

 fflush(stdin);
}
Posted in Computer Programming | Tagged , , , , | Leave a comment

C Character Functions


There are several types of character functions in the standard C library. One type of character functions is character classification functions. They are isuppper, islower, isdigit, isspace, and other boolean functions. For example, if (isupper(ch)) determines whether ch is upper case or not.

Another type of character functions is character conversion functions. They are toupper, tolower, and others. For example, ch = toupper(ch) converts ch to upper case whether the original is lower case or upper case.

Here is the same code for the character functions:

/*
 *
 * ctype.c
 *
 * Program to show examples of the use of isupper, toupper, etc.
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>
#include <ctype.h>

main() {
 char ch;

 printf("Please type in a character (enter q to quite): ");
 scanf("%c", &ch);

 while (ch != 'q') {
  if (isupper(ch)) {
   printf("This is an uppercase character\n");
   ch = tolower(ch);
   printf("Lowercase equivalent is '%c'\n", ch);
  }
  else if (islower(ch))
   printf("This is an lowercase character\n");
  else if (isdigit(ch))
   printf("This is an digit\n");
  else if (isspace(ch))
   printf("This is an space-type char\n");
  else
   printf("This is an unknown char\n");

  printf("Please type a character (enter q to quite): ");
  while (getchar() != '\n' && getchar() != EOF) {}
  scanf("%c", &ch);
 }

 fflush(stdin);
}
Posted in Computer Programming | Tagged , , , , , , , , , , | Leave a comment

Play Around with Google Nexus One


I received my Nexus One today which I ordered from Google on Saturday. The FedEx delivery of the device was earlier than I expected. They delivered my order 10 AM in the morning, nearly 5 hours earlier than the estimated time of 3 PM.

After I set up the phone, I decided to move the SIM card from my old T-Mobile based cell phone to Nexus One. It only adds basic call functionality to the device since I own the pre-paid SIM card.

GPS on the Nexus One doesn’t require Internet connection. However, the maps must be cached on the device in order to use the full GPS function. Turn-by-turn direction also needs to be cached from the Internet. I have not found any GPS app that supports offline viewing of entire US map that supports different zoom levels from Android Market. Maybe I will create one for myself using OpenStreetMap once I learn the Android SDK :) But first I must finish learning C.

The 5MP camera works very well. Below is a test photo that I took in the bathroom.

imageThis photo was taken in front of mirror
Click the image to enlarge

As you can see, the picture is very smooth without flashlight. I have not tested the flashlight and video recorder yet, but I think it won’t be very bad. One thing I noticed when playing around with camera is that the zoom level is not great. For the most time, I got pixelated pictures with the max zoom level. However, the problem isn’t visible on the device until I see the pictures on the PC.

Since I have no idea which audio/video formats does Android support, I copied one mp4 and one avi videos to the root of the memory. The gallery only showed the mp4 video. After browsing through Android Market, I downloaded and tried the ad version of RockPlayer. This player supports DivX, AVI, MKV, and Real formats. I watched some avi videos using this app and they all worked fine.

I transferred all the stuffs from iPod Touch and cell phone to Nexus One. From now on, I only need to carry one device in my pocket instead of two devices. I will see how long will Google Nexus One last in my pocket because my pockets usually get water when it’s raining whether I wear different cloths :)

Posted in Google | Tagged , , , , , , , , | 1 Comment