Monday, 13 August 2018

JCrop Extension Implementation In PHP



I came across a really nice looking plugin for JQuery the other day that allowed you to select part of an image. This plugin is called JCrop.
This plugin supports things like thumbnail generation, animation, styling and event handling. The only problem is that this is all done on the client side, there is one example of a PHP script that resizes images using PHP, but this only works with JPEG images. The site says that improvements to the code are left as an exercise to the reader, so I though I would take a shot at it.
Rather than start from scratch with the PHP implementation I have adapted the ImageResize class that I talked about some months ago. I have changed the name to ImageManipulation as it has gone beyond a simple image resizing class.
I won't post the entire source code of that class here as it will take up too much space, instead I will provide a link to it.
I'm going to be using the same JavaScript code from the Jcrop website example, but for convenience I will reproduce it here with some small changes. Basically I have removed all unnecessary HTML, included a DOCTYPE declaration, cleaned up the script tags and used the JQuery library from the Google AJAX Libraries site.
  1. //<!--
  2. $(function(){
  3. $('#cropbox').Jcrop({
  4. aspectRatio: 1,
  5. onSelect: updateCoords
  6. });
  7. });
  8. function updateCoords(c)
  9. {
  10. $('#x').val(c.x);
  11. $('#y').val(c.y);
  12. $('#w').val(c.w);
  13. $('#h').val(c.h);
  14. };
  15. function checkCoords()
  16. {
  17. if (parseInt($('#w').val())) return true;
  18. alert('Please select a crop region then press submit.');
  19. return false;
  20. };
  21. // -->
  22. Jcrop - Crop Behavior
This also includes the JCrop file (jquery.Jcrop.pack.js) and a CSS file (called jquery.Jcrop.css) that will make the selection box on the image look nice.
The function updateCoords() is used by JCrop to enter the coordinates of our crop into a set of hidden input boxes. This allows us to use a POST request to send the coordinates to our server. This is a callback function set in the JCrop initialization.
The aspectRatio setting forces the cropping box created to be square.
The checkCoords() function simply checks to see if a crop window has been created. If not then an alert is raised and the browser does nothing.
Next we need to implement our PHP code that will capture the image parameters and crop our image to a given size. This is done by creating an instance of the ImageManipulation class (giving the filename as the source), running the setCrop() method, with the correct parameters and using the show() method to display the output to screen.
  1. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST')
  2. {
  3. $src = 'flowers.jpg';
  4. require('ImageManipulation.php');
  5. $objImage = new ImageManipulation($src);
  6. if ( $objImage-?>imageok ) {
  7. $objImage->setCrop($_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']);
  8. $objImage->resize(500);
  9. $objImage->show();
  10. //$objImage->save('12345.png');
  11. } else {
  12. echo 'Error!';
  13. }
  14. exit;
  15. }
  16. ?>
This will display the output to screen only, if you want to save the output to a file then remove the comment on the line that runs the save() method.
I have put an example of this JCrop implementation in action in the same place as the ImageManipulation class. I have also created a source file so that you can download the full thing easily.
This method of cropping will work with any file type.

0 comments:

Post a Comment