Cropping Image using JCROP and Save to Database PHP >=5

Getting Started

Please take note that Bootstrap used in this tutorial are hosted so you need internet connection for them to work.

Creating our Database

1. Open phpMyAdmin. 2. Click databases, create a database and name it as crop. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `images` (
  2. `imageid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `location` VARCHAR(200) NOT NULL,
  4. `date_added` datetime NOT NULL,
  5. PRIMARY KEY(`imageid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
database insert

Creating our Output Folder

Next, we create a new folder and we're gonna name it upload. This will be our output folder for uploaded images.

index.php

This is our index which contains our upload form.
  1. <?php
  2. $folder = 'upload/';
  3. $or_w = 500;
  4.  
  5.  
  6. if(isset($_POST['upload'])){
  7. $image = $_FILES['image']['tmp_name'];
  8. $filename = basename($_FILES['image']['name']);
  9.  
  10. list($width, $height) = getimagesize($image);
  11. $or_h = ($height/$width)* $or_w;
  12.  
  13. $src = imagecreatefromjpeg($image);
  14. $tmp = imagecreatetruecolor($or_w, $or_h);
  15. imagecopyresampled($tmp, $src, 0, 0, 0, 0, $or_w, $or_h, $width, $height);
  16. imagejpeg($tmp, $folder.$filename);
  17.  
  18. imagedestroy($tmp);
  19. imagedestroy($src);
  20.  
  21. $filename = urlencode($filename);
  22. header('location:crop.php?file='.$filename);
  23. }
  24.  
  25. ?>
  26. <!DOCTYPE html>
  27. <html>
  28. <head>
  29. <title>Cropping Image using JCROP</title>
  30. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  31. </head>
  32. <body>
  33. <div class="container">
  34. <h1 class="page-header text-center">Cropping Image using JCROP</h1>
  35. <div class="col-md-4 col-md-offset-4">
  36. <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  37. <input type="file" name="image" id="image" required><br>
  38. <input type="submit" value="Upload" name="upload" class="btn btn-primary">
  39. </form>
  40. </div>
  41. </div>
  42. </body>
  43. </body>
  44. </html>

crop.php

This is our cropping page.
  1. <?php $filename = $_GET['file']; ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <title>Cropping Image using JCROP</title>
  6. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  7. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  8. <script src="js/jquery.min.js"></script>
  9. <script src="js/jquery.Jcrop.js"></script>
  10. <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
  11.  
  12. <script type="text/javascript">
  13. $(function(){
  14. $('#cropbox').Jcrop({
  15. aspectRatio: 1,
  16. onSelect: updateCoords
  17. });
  18. });
  19.  
  20. function updateCoords(c){
  21. $('#x').val(c.x);
  22. $('#y').val(c.y);
  23. $('#w').val(c.w);
  24. $('#h').val(c.h);
  25. };
  26.  
  27. function checkCoords(){
  28. if (parseInt($('#w').val())) return true;
  29. alert('Please select a crop region then press submit.');
  30. return false;
  31. };
  32.  
  33. </script>
  34. </head>
  35. <body>
  36. <div class="container">
  37. <h1 class="page-header text-center">Cropping Image using JCROP</h1>
  38. <div class="row">
  39. <div class="col-md-8 col-md-offset-2">
  40. <img src="upload/<?php echo $filename; ?>" id="cropbox">
  41. <form action="save.php?file=<?php echo $filename; ?>" method="post" onsubmit="return checkCoords();">
  42. <input type="hidden" id="x" name="x" />
  43. <input type="hidden" id="y" name="y" />
  44. <input type="hidden" id="w" name="w" />
  45. <input type="hidden" id="h" name="h" />
  46. <br>
  47. <input type="submit" value="Save Image" class="btn btn-primary btn-large btn-inverse">
  48. <br><br>
  49. </form>
  50. </div>
  51. </div>
  52.  
  53. </div>
  54. </body>
  55. </html>

save.php

This is our PHP code in saving the cropped images.
  1. <?php
  2.  
  3. $file = $_GET['file'];
  4.  
  5. $location = 'upload/'.$file;
  6. $newFile = time().'_'.$file;
  7. $newloc = 'upload/'.$newFile;
  8.  
  9. $targ_w = $targ_h = 250;
  10.  
  11. $img_r = imagecreatefromjpeg($location);
  12. $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
  13.  
  14. imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
  15. $targ_w,$targ_h,$_POST['w'],$_POST['h']);
  16.  
  17. imagejpeg($dst_r, 'upload/'.$newFile);
  18.  
  19. //connection
  20. $conn = new mysqli('localhost', 'root', '', 'crop');
  21.  
  22. $sql="insert into images (location, date_added) values ('$newloc', NOW())";
  23. $conn->query($sql);
  24.  
  25. header('location:view.php');
  26.  
  27. ?>

view.php

This contains our images table.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Cropping Image using JCROP</title>
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. </head>
  7. <body>
  8. <div class="container">
  9. <h1 class="page-header text-center">Images Table</h1>
  10. <div class="col-md-8 col-md-offset-2">
  11. <div class="row">
  12. <div class="col-md-12">
  13. <a href="index.php" class="btn btn-primary pull-right">Home</a>
  14. </div>
  15. </div><br>
  16. <table class="table table-bordered table-striped">
  17. <thead>
  18. <th>Image</th>
  19. <th>Location</th>
  20. <th>Date Added</th>
  21. </thead>
  22. <tbody>
  23. <?php
  24. $conn = new mysqli('localhost', 'root', '', 'crop');
  25.  
  26. $sql="select * from images order by imageid desc";
  27. $query=$conn->query($sql);
  28. while($row=$query->fetch_array()){
  29. ?>
  30. <tr>
  31. <td><a href="<?php echo $row['location']; ?>"><img src="<?php echo $row['location']; ?>" width="30px" height="30px" class="thumbnail"></td>
  32. <td><?php echo $row['location']; ?></td>
  33. <td><?php echo date('M d, Y h:i A', strtotime($row['date_added'])); ?></td>
  34. </tr>
  35. <?php
  36. }
  37. ?>
  38. </tbody>
  39. </table>
  40. </div>
  41. </div>
  42. </body>
  43. </html>
That ends this tutorial. Happy Coding :)

Add new comment