Dec 23

A Comprehensive Guide To Run Yii Framework On XAMPP In Linux

Yii is professional PHP framework best for developing Web 2.0 applications. This guide elaborates on how to set up the Yii framework in XAMPP under a Linux environment.

First you need to download and install XAMPP as indicated in the XAMPP guide in http://www.apachefriends.org/en/xampp-linux.html. Then you need to get write permissions to the /opt/lampp/htdocs folder (where all the websites will be stored). First look at who is owning /opt/lampp/htdocs .

ls -ld /opt/lampp/htdocs

This should return something like

drwxr-xr-x. 5 root root 4096 Dec 22 22:55 /opt/lampp/htdocs

drwxr-xr-x. denotes the file permissions of the folder. The part root root specifies the owner of the directory and the owning group of the directory. Let’s first change the owning group of the folder. Let’s create a group called www and change the group ownership of the folder to www.

sudo groupadd www
sudo chgrp -R www /opt/lampp/htdocs

Now a ls -ld should return something like the following

drwxr-xr-x. 5 root www 4096 Dec 22 22:55 /opt/lampp/htdocs

Now the XAMPP server should be configured to run using the group www. Open the /opt/lampp/etc/httpd.conf file.

sudo vim /opt/lampp/etc/httpd.conf

Then find the entry,

User daemon
Group daemon

(yours might be slightly different). Change the User entry to your user name and Group entry to www. Now you need to set the permissions of the /opt/lampp/htdocs folder.

sudo chmod 2775 /opt/lampp/htdocs
usermod -aG www

You’ll need to logout and log in to the system since group memberships are read at login time. But before doing that, let’s carry out another task that will require a logout. We need to add PHP to our PATH. So open your .bashrc as follows.

vim /home/.bashrc

Then add the following lines to the end of that file.

##Adds XAMPP bin to PATH
export PATH=/opt/lampp/bin:$PATH

Now logout and login. Then extract the Yii framework to a folder named yii in the /opt/lampp/htdocs folder, start the XAMPP server and go to the address http://localhost/yii/requirements/index.php to check whether Yii is working. Then to create a Yii application, follow the following commands. I have used the test name blog.

cd /opt/lampp/htdocs/
php ./yii/framework/yiic webapp blog

After completing the creation, visit http://localhost/blog/ to verify the functionality of the created Yii application…!.

References :

  1. http://superuser.com/questions/268987/cant-create-any-folder-in-htdocs-on-ubuntu
  2. http://stackoverflow.com/questions/14318699/xampp-virtual-host-access-denied
  3. http://www.yiiframework.com/forum/index.php/topic/1838-install-yii-on-xampp-on-linux/page__p__10296__hl__yii+linux+in+tallation#entry10296
  4. http://www.yiiframework.com/doc/blog/1.1/en/start.testdrive
Share Button
Jan 24

Handling images in java using imageIO

Have you ever faced the need of sending an image through a network using a Java program? If yes, you must have probably read the image to a byte array sent it through the network. The problem with this method is it is too long and you have to remake the image file ( using the received byte array) at the destination to take any use of it.

But in this article I’m going to show how to read an image to a Java class called “Image” and use it appropriately. You can use this “Image” class directly in GUI’s, send it through networks, store it, etc. Sol let’s look at the code now. The code consists of two parts. Reading the image to an instance of the “Image” class and writing it back to the hard drive. I have put comments in almost every step. So understanding the code should be easy.

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class Main {

 public void copyImage(){

 //path of the image to be read
 String path="picture.jpg";

 //create a file entry in java for that image
 File imageSrc=new File(path);

 if(!imageSrc.exists()){
 System.out.println("Input image does not exist.");
 System.exit(0);
 }

 try {

 /*
 * This part of the code is for reading the image in to
 * an instance of an Image class.
 */

 //Create a stream to read the image
 ImageInputStream iis = ImageIO.createImageInputStream(imageSrc);
 /*
 * ImageIO is a class containing static convenience methods for locating
 *ImageReaders and ImageWriters, and performing simple encoding and decoding.
 */

 //get a collection of Image readers which support jpg
 Iterator<?> imgReaders = ImageIO.getImageReadersByFormatName("jpg");

 //get the first image reader from the collection
 ImageReader reader = (ImageReader) imgReaders.next();

 //set the input for the image reader
 reader.setInput(iis, true);
 /*
 * Setting seekForwardOnly parameter ensures that the image file is
 * read in the ascending order.
 *
 */

 //to ensure that the default read parameters are used when
 //decoding data from the stream
 ImageReadParam param = reader.getDefaultReadParam();

 //read the image
 Image image = reader.read(0, param);

 /*
 *
 * This part of the code is for writing the image object
 * back to the hard disk as an image file.
 */

 //To render the image
 BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);

 //using "painter" we can draw in to "bufferedImage"
 Graphics2D painter = bufferedImage.createGraphics();

 //draw the "image" to the "bufferedImage"
 painter.drawImage(image, null, null);

 //the new image file
 File outputImg=new File("output.jpg");

 //write the image to the file
 ImageIO.write(bufferedImage, "jpg", outputImg);

 System.out.println("Image transfer was successful...!");

 } catch (IOException ex) {
 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
 }
 }

 public static void main(String[] args) {
 // TODO code application logic here

 Main main=new Main();
 main.copyImage();
 }

}

You can download the text file from here.

As you must have understood, I have written the code to read a JPEG image. I tested the same code for BMP, PNG, and TIFF. All those formats worked just fine. The only draw back that I saw was there is a quality loss when using JPEG. To change the image format, change the following code segments (change xxx to the desired foramt).

//path of the image to be read

String path="picture.xxx";

————————————————————————————————————————————————————————–

//get a collection of Image readers which support jpg
 Iterator<?> imgReaders = ImageIO.getImageReadersByFormatName("xxx");

————————————————————————————————————————————————————————–

//the new image file
File outputImg=new File("output.xxx");

————————————————————————————————————————————————————————–

//write the image to the file
ImageIO.write(bufferedImage, "bmp", outputImg);

————————————————————————————————————————————————————————–

If you used a byte array to read the image file, you have to make an image file( on the hard drive) before you use that image again. But using the above method you can directly use it in the Java program. Click here to see an example on that.

As you can see, the possibilities are endless. So experiment and enjoy…..!

Share Button