Web developers on some occasions are required to give user the ability to move image to fit the view or crop some part of image. In this blog post we will see how to allow user to drag/move image using jQuery and save the result (cropped) in new image file using PHP.
I have break this in two posts. Part 1 will contain client side part. Part 2 will contain server side (PHP) part.
Client side part (HTML+ jQuery)
Add a reference to jQuery library. I am using version 1.8 for this example
To learn how to store cropped image in PHP . Read
Drag and Move image using jQuery and crop using PHP - Part2
I have break this in two posts. Part 1 will contain client side part. Part 2 will contain server side (PHP) part.
Client side part (HTML+ jQuery)
Add a reference to jQuery library. I am using version 1.8 for this example
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>Display image inside a div. Div will be of fixed width and height which will be our viewport. Part of image outside this div will be cropped. For this example lets keep it to 850x600. First write a simple css to apply on this div
#imgContainer {Make a div and place image inside it
width:850px;
height:600px;
overflow:hidden; /* part of image outside this div should be hidden */
border-width:2px;
border-style:solid;
border-color:#000000
}
<div id="imgContainer">We will create a FORM that will be POSTed to our PHP script . This form will hold current X and Y co-ordinates of image.
<img src="movable.jpg" style="position:relative;top:0px;left:0px;" id="imgMy" />
</div>
<form method="POST" action="crop.php">Now we will call jQuery to make our image movable/draggable.
<input type="hidden" value="0px" name="locationX" id="locationX" />
<input type="hidden" value="0px" name="locationY" id="locationY" />
<input type="submit" value="Save" />
</form>
<script type="text/javascript">We now have a image that is movable/draggable.
jQuery(document).ready(function ($) {
$( "#imgMy" ).draggable({
drag: function( event, ui ) {
$("#locationX").val($("#imgMy").css('left'));
$("#locationY").val($("#imgMy").css('top'));
}
});
});
</script>
To learn how to store cropped image in PHP . Read
Drag and Move image using jQuery and crop using PHP - Part2
Comments
Post a Comment
Share your wisdom