linein designs

29 Nov, 2010

Rotate Images with jQuery and PHP

Posted by: john In: javascript|jquery|php|tutorial

Things you’ll need:

DEMO | DOWNLOAD PROJECT

We’ll first setup the control div since it should help make things clearer starting here. We have the two rotate buttons (clockwise and anit-clockwise) and the save button to save our final rotated image. We also have an input box that will store the rotated value.





current angle:

Next we have the javascript function that is going to rotate the images based on the buttons we press. Each time they are pressed it’s going to rotate the images 90 degrees either way. (you can change these value to fit your project)

function updateAngle(image,direction,currentAngle)
{
	var rotateAngle = 0;
	switch (direction)
	{
		case 'anticlockwise':
			if(currentAngle == 0){rotateAngle = -90;}
			else{rotateAngle = parseInt(currentAngle) - 90;}
		break;
		default:
			if(currentAngle == 0){rotateAngle = 90;}
			else{rotateAngle = parseInt(currentAngle) + 90;}
	}

	$('#'+image).rotateAnimation({
		animateAngle:rotateAngle
	});

	$('#currentAngle').val(rotateAngle); // sets currentAngle value
}

Then we have the AJAX request that will send the image and angle value to the PHP script to be saved.

$(document).ready(function()
{
	$('#saveImage').click(function(){
		$('#save_results').html('saving...'); // display while image is being saved
		$.ajax({
		   type: "POST",
		   url: "saveImageAjax.php",
		   data: "img=image.jpg&angle="+$('#currentAngle').val(), // image value is currently hard-coded due to plugin limitation
		   success: function(msg){
	     		$('#save_results').html(msg);
	   	}
	 	});
	});
});

And finally we have the PHP script that takes the variables and stores a local copy of the rotated image. The script is fairly straight forward, but if you have any questions, please leave me a comment below.

Credits:
rotateImage – PHP function developed by Roshan Bhattara (function included in project files)
iconfinder.com/ – icons

Similar Posts:

2 Comments »

Comment by Sam Dorrington

Nice, this could be pretty handy for my site, I’ve been looking for something like this.

I think I’ll give it a whirl and see if I can get my head around installing it all properly.

Thanks!

 
Comment by Sachin Thakur

But how can i save this rotate image in php (save with same dimensions)???

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post


  • Seren: Thanks - google did not fail me in finding this snippet (after 2 hours of 'why isn't this working!' before I choose to look for answers. That select
  • zdenis: type your scp command with nohup at the beginning: >nohup scp... enter your password stop temporarily the command with CTRL+z put on b
  • Criz: php script >> How to get the number of hours and minutes of a two set of time like this sample. 1st set of time : 8:00 to 17:00 2nd set of


Please Support Our Friends

Banner
FreshBooks

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.