31 May, 2011

Image Browse And Upload feature in ckEditor

ckEditor version 3.6 "by default", may be previous versions as well hides the file browse and upload function, but its quite easy to make them display if we know where to change it.
Go to folder ckeditor\plugins\image\dialogs\, in there you will find a file image.js, open it in editor and search the file for id:'Upload', you will see that, its set as id:'Upload',hidden:false, set it as id:'Upload',hidden:true, and refresh your page where you have added ckEditor, you will be able to see the Upload tab with Browse button in it.

Thats it.

23 May, 2011

Javascript Functions Flexibility

In Javascript, everything is an object. Functions in Javascript are said to be first-class objects, that can be passed as arguments to other functions, stored as variables, created at run-time,etc.

Anonymous functions without parameters that run immediately.

(function() {
var one = 1;
var two = 2;
alert(one+two);
})();

In above anonymous function, the parenthesis at the end indicates that this function will be run immediately without being assigned to any variable.

Anonymous functions with parameters that run immediately.
The parenthesis at the end can have some parameters as well. Such as,

(function(one, two) {
alert(one+two);
})(1, 2);

Or, if you want to assign the value to some variable then,

var three = (function(one, two) {
alert(one+two);
})(1, 2);
alert(three); //and output is 3


Assign Attribute to Functions in Javascript
In Javascript we can modify classes after they have been defined and objects after they have been created. Lets take an analogy, we have a car class,

function Car(name, model) {
this.name = name;
this.model = model;
}
Car.prototype = {
getName: function() {
return this.name;
},
getModel: function() {
return this.model;
}
}
//instantiate the class now
var benz = new Car('Benz', 1969);
var bmw = new Car('Bmw', 1984);

//now modify the class
Car.prototype.getPrice = function() {
return "Price for " + this.getName() + "is " + 20000;
};

//now modify the specific object instance
benz.showPrice = function() {
alert(this.getPrice());
};

In above example, we added a new function getPrice() after two object instances for the class are created. And, benz object instance gets showPrice() function in addition to other functions of the class, but bmw object instance wont get the showPrice() function in this case. This feature is called Object Mutability.
This is just few examples that tend to focus on flexibility of javascript functions.

10 May, 2011

REST client library using CodeIgniter

Using REST in applications makes life easier to some extent. And using REST in codeigniter can be easier. There exist some REST libraries in codeigniter but somehow they seem to have lots of code and features which is not required in my case. I'm not degrading existing codeigniter REST libraries, they are excellent codes
developed by experts, but i wanted a simple REST client using Codeigniter, so thought of coding my own.
Following is the code for REST client library in codeIgniter, that suits simple needs.


class Rest_Lib {

private $useCurl;
private $userAgent = "Rest";

/*
* constructor function
*/
function Rest_Lib() {
//check if curl function exists
if(function_exists('curl_init')) {
$this->useCurl = TRUE;
}
else {
$this->useCurl = FALSE;
}
}

/*
* GET method
* @param string $url : this is the service url
* @param array $arr: this is the key value
* @return string : resulting response of GET method
*/
public function get($url, $arr) {
$str = '?';
if(is_array($arr)) {
foreach($arr as $key => $value) {
$str .= urlencode($key) . '=' . urlencode($value) . '&';
}
}
else {
$str .= $arr;
}
$url .= $str;
if($this->useCurl) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
}
else {
$options = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent:' . $this->userAgent . '\r\n'
)
);
//create and return stream context with options
$streamContext = stream_context_create($options);
$fh = file_open($url, 'r', FALSE, $streamContext);
$response = fpassthru($fh);
fclose($fh);
}
return $response;
}

/*
* POST method
* @param string: $url: service url
* @param string $postData: request data
* @return response
*/
public function post($url, $postData) {
if($this->useCurl) {
$curlHeaders = array(
'Content-Type: application/x-www-form-urlencoded'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
$response = curl_exec($ch);
curl_close($ch);
}
else {
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'User-Agent: ' . $this->userAgent . '\r\n',
'Content-Type: application/x-www-form-urlencoded' . '\r\n',
'Content-Length: ' . strlen($postData) . '\r\n',
'Content' => $postData
)
);
$streamContext = stream_context_create($options);
$fh = file_open($url, 'r', FALSE, $streamContext);
$response = fpassthru($fh);
fclose($fh);
}
return $response;
}

/*
* PUT method
*
*/
public function put($url, $putData) {
if($this->useCurl) {
$fh = fopen('php://memory', 'rw');
fwrite($fh, $putData);
rewind($fh);

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putData));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
fclose($fh);
}
else {
$options = array(
'http' => array(
'method' => 'PUT',
'header' => 'User-Agent: ' . $this->userAgent . '\r\n',
'Content-Type: application/x-www-form-urlencoded' . '\r\n',
'Content-Length: ' . strlen($putData) . '\r\n',
'Content' => $putData
)
);
$context = stream_context_create();
$fh = file_open($url, 'r', FALSE, $context);
$response = fpassthru($fh);
fclose($fh);
}
return $response;
}

/*
* DELETE method
*/
public function delete($url, $arr) {
$str = '?';
if(is_array($arr)) {
foreach($arr as $key => $value) {
$str .= urlencode($key) . '=' . urlencode($value) . '&';
}
}
else {
$str .= $arr;
}
$url .= $str;
if($this->useCurl) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'delete');
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
$response = curl_exec($ch);
curl_close($ch);
}
else {
$options = array(
'http' => array(
'method' => 'DELETE',
'header' => 'User-Agent: ' . $this->userAgent . '\r\n'
)
);
$context = stream_context_create($options);
$fh = file_open($url, 'r', FALSE, $context);
$response = fpassthru($fh);
fclose($fh);
}
return TRUE;
}
}
?>

04 May, 2011

How to ImageMagick using PHP

ImageMagick can be used to create and edit images dunamically and display the results online or locally at our desired URLs. It offers lots of features, such as crop, resize, rotate, flip, blur, sharpen, inserting texts, and lots more, using its syntax properly.
And we can do all this stuffs using PHP. In order to use ImageMagick with PHP we can use PHP's function for executing external programs, which is exec and system. Other such functions also exist in PHP but the two mentioned above have the highest
priority.
The usage format is:

echo exec(string command [, array &output ]);
?>

In its simplest form, ImageMagick can be used as:

$firstImg = "test.bmp";
$finalResult = "test2.png";
exec("path_tp_image_magick\imagemagick\convert {$firstImg} {$finalResult}");
?>


Basically, the above function converts test.bmp to test2.png, i.e changes the file format.
Some of the basic stuffs that can be done using ImageMagick

Stuff 1: How to get width and height of image using ImageMagick in php

$dimensionsArr = array();
$inputImage = "full_path_to_input_image.jpg";
//get image width and height
exec("full_path_to_image_magick/identify -format \"%w\n\%h\" {$inputImage}", $dimensionsArr);
?>

In above case width and height of image are stored in $dimensionsArr array.

Stuff 2: How to Watermark an Image using ImageMagick in PHP

$inputImage = "full_path_to_input_image.jpg";
$waterMarkText = "Watermark Me";
$outputImage = "full_path_to_output_image.jpg";
exec("full_path_to_image_magick/composite -watermark 30% -gravity northeast {$inputImage} {$waterMarkText} {$outputImage}");
?>


Stuff 3: Crop an image using ImageMagick in PHP

$firstImg = "first.jpg";
$output = "cropped.jpg";
exec("E:\wamp\www\im\imagemagick\convert {$firstImg} -crop 120x80+160+80 {$output}");
?>

In above code what we did is we cropped an image "first.jpg" to set width of 120px and height of 80px and the cropping is started from 160x80 coordinate. Or a better way of cropping images can be done as:


$firstImg = "first.jpg";
$output = "cropped.jpg";
exec("E:\wamp\www\im\imagemagick\convert {$firstImg} -shave 120x80 {$output}");
?>

What above code does is, it cuts the borders with the values we defined in width X height parameters.

Stuff 4: Rotate Image using ImageMagick in PHP

$firstImg = "first.jpg";
$rotated = "rotated.bmp";
exec("E:\wamp\www\im\imagemagick\convert {$firstImg} -rotate 90 {$output}");
?>

Above code we used value -rotate 90 to rotate the image at 90 degrees, and saved the output in bmp

Stuff 5: Resize an image using ImageMagick in PHP

exec("E:\wamp\www\im\imagemagick\convert first.jpg -resize 10% -sample 500% first_new.png");
?>

The above code resizes first.jpg to 10% of its actual size with sampling at 500% and saves to "first_new.png"

Stuff 6: Extract frames from animated image using ImageMagick in php
One of the exciting feature that ImageMagick has is, we can extract the frames from an animated image file.
For that we can write a simple one-line command as:

$animatedImage ="animated.gif";
exec("E:\wamp\www\im\imagemagick\convert {$animatedImage} +adjoin animated_Image%02d.gif");
?>

The above code extracts frames from the animated image "animated.gif" and saves it as "animated_Image1.gif", "animated_Image2.gif", etc for each frame.

These are just some of the features that ImageMagick offers, there's lot more to check out. Above code snippets can act as a good start prior to delving into the ImageMagick commands in detail.