crop images using jcrop plugin

crop images using jcrop plugin

crop images using jcrop plugin

For image based web application and websites, Image cropping is one of the important functionality.

There are many useful jquery plugins are available for image cropping. Jcrop is one of the best cross browser jQuery image crop plugin available in the web world

Jcrop provide simple functionalists to crop images
Let us check how we can crop images using jcrop plugin.

Here I’m giving a simple example to crop image using jcrop plugin.
By enabling most of the options and giving an option to crop image.
This is combining most of the options of jcrop and creating a working image crop functionality.

So let us create a php script for coping

	$targ_w = $targ_h = 150;
	$jpeg_quality = 90;

	$src = 'demos/sago.jpg';
	$img_r = imagecreatefromjpeg($src);
	$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );


	header('Content-type: image/jpeg');



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <title>crop images using jcrop plugin</title>
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <script src="js/jquery.Jcrop.js" type="text/javascript"></script>
    <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
    <link rel="stylesheet" href="demos/demos.css" type="text/css" />
    <style type="text/css">
      .optdual { position: relative; }
      .optdual .offset { position: absolute; left: 18em; }
      .optlist label { width: 16em; display: block; }
      #dl_links { margin-top: .5em; }
    <script type="text/javascript">


        // The variable jcrop_api will hold a reference to the
        // Jcrop API once Jcrop is instantiated.
        var jcrop_api;

        // In this example, since Jcrop may be attached or detached
        // at the whim of the user, I've wrapped the call into a function

        // The function is pretty simple
        function initJcrop()//{{{
          // Hide any interface elements that require Jcrop
          // (This is for the local user interface portion.)

          // Invoke Jcrop in typical fashion
            onRelease: releaseCheck

            jcrop_api = this;

            // Setup and dipslay the interface for "enabled"



        // Use the API to find cropping dimensions
        // Then generate a random selection
        // This function is used by setSelect and animateTo buttons
        // Mainly for demonstration purposes
        function getRandom() {
          var dim = jcrop_api.getBounds();
          return [
            Math.round(Math.random() * dim[0]),
            Math.round(Math.random() * dim[1]),
            Math.round(Math.random() * dim[0]),
            Math.round(Math.random() * dim[1])

        function releaseCheck()
          jcrop_api.setOptions({ allowSelect: true });

        // Attach interface buttons
        // This may appear to be a lot of code but it's simple stuff
        $('#setSelect').click(function(e) {
          // Sets a random selection
        $('#animateTo').click(function(e) {
          // Animates to a random selection
        $('#release').click(function(e) {
          // Release method clears the selection
        $('#disable').click(function(e) {
          // Disable Jcrop instance
          // Update the interface to reflect disabled state
        $('#enable').click(function(e) {
          // Re-enable Jcrop instance
          // Update the interface to reflect enabled state
        $('#rehook').click(function(e) {
          // This button is visible when Jcrop has been destroyed
          // It performs the re-attachment and updates the UI
          return false;
        $('#unhook').click(function(e) {
          // Destroy Jcrop widget, restore original state
          // Update the interface to reflect un-attached state
          return false;

         $('#ar_lock').change(function(e) {
            { aspectRatio: 4/3 }: { aspectRatio: 0 });
        $('#size_lock').change(function(e) {
          jcrop_api.setOptions(this.checked? {
            minSize: [ 80, 80 ],
            maxSize: [ 350, 350 ]
          }: {
            minSize: [ 0, 0 ],
            maxSize: [ 0, 0 ]



					aspectRatio: 1,
					onSelect: updateCoords


			function updateCoords(c)

			function checkCoords()
				if (parseInt($('#w').val())) return true;
				alert('Please select a crop region then press submit.');
				return false;



    <div id="outer">
    <div class="jcExample">
    <div class="article">

      <h1>crop images using jcrop plugin</h1>
      <img src="demos/sago.jpg" id="target" alt="Jcrop Image" />

      <div style="margin: .8em 0 .5em;">
        <span class="requiresjcrop">
          <button id="setSelect">setSelect</button>
          <button id="animateTo">animateTo</button>
          <button id="release">Release</button>
          <button id="disable">Disable</button>
        <button id="enable" style="display:none;">Re-Enable</button>
        <button id="unhook">Destroy!</button>
        <button id="rehook" style="display:none;">Attach Jcrop</button>

      <fieldset class="optdual requiresjcrop">
        <legend>Option Toggles</legend>
        <div class="optlist offset">
          <label><input type="checkbox" id="ar_lock" />Aspect ratio</label>
          <label><input type="checkbox" id="size_lock" />minSize/maxSize setting</label>
        <div class="optlist">
          <label><input type="checkbox" id="can_click" />Allow new selections</label>
          <label><input type="checkbox" id="can_move" />Selection can be moved</label>
          <label><input type="checkbox" id="can_size" />Resizable selection</label>

	  	<form action="index.php" method="post" onsubmit="return checkCoords();">
			<input type="hidden" id="x" name="x" />
			<input type="hidden" id="y" name="y" />
			<input type="hidden" id="w" name="w" />
			<input type="hidden" id="h" name="h" />
			<input type="submit" value="Crop Image" />


So by using jcrop you can easily crop image.
Here I’m showing a simple way to crop using jcrop. You can try all advance way of coping

Jcrop Manual

Download                              Labs