January 13, 2024 5:04 am

How to Crop Image with jQuery and PHP

I have received many requests from my readers to write something on image crop in php, so today I am going to write this tutorial on PHPGang how to crop images with jQuery and PHP. I have used jCrop javascript library to perform this task with PHP its a very easy to implement and very useful for thumbnail generation. Hope you love this tutorial.

crop-images

[wpdm_file id=26]DEMO

 

PHP Methods to save image:

<?php
$targ_w = $targ_h = 150;
$jpeg_quality = 90;

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

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);

header('Content-type: image/jpeg');
imagejpeg($dst_r,null,$jpeg_quality);
?>

 

This will simply show image on crop if you want to save that image in some directory then make changes in last two line as below.

//header('Content-type: image/jpeg');
imagejpeg($dst_r,PATH_TO_SAVE_IMAGE,$jpeg_quality);

 

Javascript Configurations:

<script type="text/javascript">
    $(function(){
    $(\'#cropbox\').Jcrop({
    aspectRatio: 1,
    onSelect: updateCoords,
//    minSize:[200,200], start minimum image size
//    maxSize:[200,200], max size should be...
    });
});
</script>

 

 

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

14 responses to “How to Crop Image with jQuery and PHP”

  1. This is an awesome tutorial . It is very useful for me. 🙂

  2. selvakumar says:

    How to crop image in round shape. But It shuld not include the css. I want new image in round shape. Tell me sir

  3. vivek says:

    Hello there i am unable to download the source code event after signin

  4. Jignesh says:

    Hi i am not able to download source code even after subscribe

  5. Kevin says:

    hi, i can’t download source code

  6. Joshua says:

    I receive this error when I try to save to directory /uploads… Warning: imagejpeg() [function.imagejpeg]: Unable to open ‘/uploads’ for writing: Permission denied in

    I am trying to save the image after crop using this line of code:

    imagejpeg($dst_r,’/uploads’,$jpeg_quality);

    Please help!

    Thanks!

    • huzoorbux says:

      The problem is that your folder is not having write permission. And because of that it is not uploading the file.

      You have to give it write permission. You can also use chmod for giving write permission to that folder.

    • Banu says:

      same error occurs to me if you got the answer pls reply to my mail id. id: [email protected].
      thank you

  7. Chamandeep singh ranot says:

    very nice…….its very helpfull…

  8. Vikas Arora says:

    Hi,
    When i have tried to download the script getting following errors.
    /var/www/html/wp-content/plugins/download-manager/cache/ must have to be writable!

    Please look into this.

  9. kishan rathod says:

    hi, i can’t download source code.please help me.

  10. Sohni saxena says:

    Hi i am not able to download source code even after subscribe

    •

    Reply

    •

    Share ›

  11. Raihan Haque says:

    hi, i cant download your code .Could you please help me to download this source code.

  12. Sarath says:

    What a piece of shit.Not working Please mention what all additional scripts you had used.

Leave a Reply

Your email address will not be published. Required fields are marked *