Intelligent Watermark (php + gd)

August 9th, 2008 by Sajith M.R
This entry is part 5 of 5 in the series Online Photoshop

Online Photoshop in PHP Part (5) >>

(image after watermarked with gmail logo)

Those who haven’t seen  the online photoshop tool created by me, just click here: http://www.sajithmr.com/photoshop

Today we will implement the watermarking on images using php gd library. Intelligent watermarking means, apply a logo or a watermark image on another image by measuring the color depth of the applied image. For this water marking, use png images as logo or watermark  for better results.

Live example for watermark implementation: http://sajithmr.com/photoshop-tuts/watermark/addwatermark.php

You can see different watermark location by changing the x1,y1 and x2,y2 parameters of the following url:

http://sajithmr.com/photoshop-tuts/watermark/showresult.php?x1=405&y1=285&x2=520&y2=392&width=115&height=107

See the watermark php source code here: http://sajithmr.com/photoshop-tuts/watermark/watermark.class.txt

Download the full-source code of watermarking implementation using php+gd from here: http://sajithmr.com/photoshop-tuts/watermark.rar

The biggest blunder I ever made with PHP

July 12th, 2008 by Sajith M.R

Blunder I ever made

I was writing a php code to parse images from a particular url . At the begining I used http://www.google.com as a testing url.

When I tested the php code from my local machine (http://localhost) it took time to get data from a remote url (google.com) . So I decided to check this code with my locally testing web page. So i write the code as bellow :

file: index.php

<?php

$content = file_get_contents(”http://localhost/”);

// parse($content);

?>

This is the biggest mistake I made that day. Did you find the mistake ?? Else try this code with your local machine. You can see your big blunder . Not only you, your system also will get hang .. :)

Because this is the longest recursive call you can call with your web server !!!

Regards

Sajith

Crop Image Like Orkut

June 5th, 2008 by Sajith M.R
This entry is part 4 of 5 in the series Online Photoshop

Online Photoshop in PHP (Series Part 4) >>

Crop Image Like Orkut

Today we will discuss about cropping of images using php and gd library.

Look at the simple example here:

http://www.sajithmr.com/photoshop-tuts/crop/simplecrop.php


$x1 = $_GET['x1'];
$y1 = $_GET['y1'];

$x2 = $_GET['x2'];
$y2 = $_GET['y2'];	

$image_object =   imagecreatefromjpeg('hari.jpg');
$image_cropped =  Crop($image_object,$x1, $y1, $x2,$y2);
$temp_path = rand(1,99999)."hari.jpg";
imagejpeg( $image_cropped,$temp_path);

header('Location: simplecrop.php?img='.$temp_path );	  

?>

Here the Crop function is doing the real work. x1 and y1 are Top-Left Coordinates of new image and x2, y2 are the Bottom-Right .

Here is the function for Crop

function Crop($image, $xpos1,$ypos1,$xpos2,$ypos2)
	{
		require_once('class.cropcanvas.php');
		$cc =& new CropCanvas();
		$cc->loadImageFromObject($image);
		$cc->cropToDimensions($xpos1, $ypos1, $xpos2,$ypos2 );
		return $cc->getImageObject();

	}

?>

You can see the class.cropcanvas.php file from the attachment.

In the above example, we have to enter each coordinates manually. See the example below:

http://www.sajithmr.com/photoshop-tuts/crop/realcrop.php

Here you can select the area by click and drag (like orkut)

This is implemented with the help or jslib javascript. The given attachment in the end of this post contains the all.

http://www.sajithmr.com/photoshop-tuts/crop.zip

Keep reading for the further posts in this series. Next post is on Intellegent watermarking on images.

Subscribe to my Feeds: http://www.sajithmr.com/feed/

Download Youtube Videos using PHP Code

May 29th, 2008 by Sajith M.R

If you want to get the FLV file of any youtube video url using php code, here is the solution.

If you are a PHP Programmer and if you are working with any video website, and if you need to grab videos (FLV files) from youtube and to put it yourown site (not object embedding) , Download the Full Source Code given at the end of this article (ZIP)

I used this php code for the Youtube video download tool http://www.googleneedle.com


Here function getPatternFromUrl is nothing but, get the exact pattern of a particular video from any youtube video url format.

In the above case , it returns pzmP4UvZRa4

The function is below

function getPatternFromUrl($url)

 {

$url = $url.'&';

$pattern = '/v=(.+?)&+/';

preg_match($pattern, $url, $matches);

return ($matches[1]);

}

GetFlvFromYoutube is the main function here, which download the flv file from youtube pattern and saves to your local machine.
The function is below:

function GrabFlvFromYoutube( $pattern )
{

 require_once ("phptube.php");

 $tube = new PHPTube ();

 $flv_http_path = $tube->download($pattern) ;

 echo $flv_http_path;

 set_time_limit(0);

 $data = file_get_contents($flv_http_path);

 $new_flv_path = dirname(_FILE_).'/flvs/'.$pattern.'.flv' ;

 file_put_contents($new_flv_path, $data);

 return $new_flv_path ;

}

Download the fullsource code from this link given:

http://www.sajithmr.com/downloads/youtube-download-php.zip

Online Photoshop in PHP (Series Part 3)

May 26th, 2008 by Sajith M.R
This entry is part 3 of 5 in the series Online Photoshop

Resize a Photo >>

Today we will implement hows to resize photo into a particular width and height (or keeping aspect ratio)

See the live example from: http://www.sajithmr.com/photoshop-tuts/resize/resize.php

You need to install php-gd library to do this. To functions, imagecreatetruecolor and imagecopyresampled will do the resizing.

Those who have not seen the Online Photo Editing tool yet, have a look at: http://www.sajithmr.com/photoshop/index.php 

Here is the function:


function Resize($image,$new_width,$new_height=0)
{

 $old_width = imagesx($image);
 $old_height= imagesy($image);

 if($new_height==0) // if the height is not specified
                           //....calculate the relative height
 $new_height= $new_width * $old_height / $old_width ;

 $new_image= imagecreatetruecolor($new_width, $new_height);
 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width,
 $new_height, $old_width, $old_height);
 return $new_image;

}

Here the $image is the object of the image which is created from the function imagecreatefromgif($path_to_photo), if the image is in GIF format.

Other functions are imagecreatefromjpeg and imagecreatefrompng for JPEG and PNG respectively .

Download Full source code for this example from: http://www.sajithmr.com/photoshop-tuts/resize.zip

In the previous part we implemented browse photos area. http://www.sajithmr.com/online-photoshop-in-php-series-part-2/

In that for browsing picture files, we used full size image, instead of thumbnails. Here new example , in which browse photos is implemented with image thumbnails using resizing (150 px)

The difference is , instead of calling img tag’s src to the orginal image path, called through resize.php?img=path_to_image , passed the image path as parameter to resize function.

See live example: http://www.sajithmr.com/photoshop-tuts/browse-resize/browse.php

Take the html and see the difference from old browse implementation: http://www.sajithmr.com/photoshop-tuts/browse.php

Which is fast ??

Download the full source code for browsing with image thumbnail from: http://www.sajithmr.com/photoshop-tuts/browse-resize.zip

Keep reading for the coming series,  part 4 , Orkut Like Photo Cropping >>

Subscribe to this website feed now:

Regards

Sajith M.R

Online Photoshop in PHP (Series Part 2)

May 21st, 2008 by Sajith M.R
This entry is part 2 of 5 in the series Online Photoshop

Implementation of the Browse Photos Area >>

Today we will implement the browse photos area of online photoshop

Those who haven’t seen the online photo editing tool, click on the above icon (baloon)

You can see the browse area implementation here: http://www.sajithmr.com/photoshop-tuts/browse.php

What you need for this is 4 php functions: is_dir , opendir, readdir, filetype

Normal file browsing can be implemented using while loop with above four functions.

See its usage:

if (is_dir($dir)){if ($dh = opendir($dir)){

while ( ($file = readdir($dh)) !== false   )

{

if($file != '.' &&  $file != '..')

{

//if it is a directory show folder icon

if(  filetype($dir .$dir_sep. $file) == "dir"   )

{

//display a folder icon here with folder name using img tag

}

else

{

// display image here in img tag

}

}

}

}

}

The whole source code is here: http://www.sajithmr.com/photoshop-tuts/browse.zip

PHP post without curl

April 4th, 2008 by Sajith M.R

You can simulate the post method using php without the help of curl library.
download full source code:
OpenID Integration PHP

See the code below:

function do_post_request($url, $data, $optional_headers = null) {

$params = array('http' =>; array('method' =>; 'POST',

'content' =>; $data

));

if ($optional_headers !== null) {

$params['http']['header'] = $optional_headers;

}

$ctx = stream_context_create($params);

$fp = @fopen($url, 'rb', false, $ctx);

if (!$fp) {

throw new Exception("Problem with $url, $php_errormsg");

}

$response = @stream_get_contents($fp);

if ($response === false) {

throw new Exception("Problem reading data from $url, $php_errormsg");

}

return $response;

}

?>

Download the full source code from post_without_curl.zip

OpenID Integration PHP

April 3rd, 2008 by Sajith M.R

OpenID PHP Integration

I think you know the use of OpenID. Else go here and learn

http://en.wikipedia.org/wiki/OpenID
http://openid.net/what/
http://openiddirectory.com/

In a nutshell, the OpenID technology makes life simpler by having only one username and password to remember - yahoo OpenID tour

Here the php source code for integrating your site with OpenID. If you have a website which has user login / signup options , you can also add openID authentication so that user can login into your site with there OpenID

php-openid-2.0.zip

OpenID Integration PHP

(tested in windows. read the README file before installing. set $store_path = “/tmp” to a directory according to OS)

Visit this url: http://openidenabled.com/ for other languages (phython, ruby etc)

Dynamic Function Calling in PHP

February 8th, 2008 by Sajith M.R

Dynamic Function Calling in PHP Dynamic Function Calling in PHP

Why php is more flexible ? See this function example:

Class Record

{

public function getMessage()

{

return "Hello world";

}
}

//You can call this function like this:

$function = "getMessage";

$R = new Record;

call_user_func( array($R, $function   )  );

If you want to pass some argument , you can use rest of the parameters of call-user_function;

eg: call_user_func( array($R, $function ) , $param, $param2);

For more info: http://www.php.net/call_user_func