Hi,
I'm a begginer at php and I need to create a image editing tool in php.
I want to crop the image, display the cropped image next to the original and be able to resize, re-crop and replace the cropped image in the same location as previous cropped image, each time I click the 'crop' button.
I would be grateful if someone could kindly help me correct this to work.
Thank you.
P.S. Someone made a similar request to this a few months ago, however this is slightly different, and I am still struggling to make this work.
This is the html code:
<!DOCTYPE html> <head> <title>Image Crop - Old Version</title> <link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <script src="index.js"></script> </head> <body> <div id="container"> <img width="470" src="photo2-W470xH601.jpg" /> <div id="box"></div> </div> <div id="output"> <img id="changeable" src="" /> </div> <button id="crop_button">Crop</button> </body> </html>
This is the jquery / javascript code, 'index.js':
// JavaScript Document $(document).ready(function(){ $('#box').draggable({containment: '#container'}); $('#box').resizable({containment: '#container'}); $(document).on('click', '#crop_button', function(){ var top = $('#box').position().top; var left = $('#box').position().left; var width = $('#box').width(); var height = $('#box').height(); alert('Top: ' + top + 'Left: ' + left + 'Width: ' + width + 'Height: ' + height); $.post('crop.php', {top:top, left:left, width:width, height:height}, function(){ $('#changeable').attr('src', 'new.jpg'); }); }); });
This is the PHP code for 'crop.php':
<?php ob_start(); // top of the page $dst_x = 0; // Leaving zero X margin when cropping $dst_y = 0; // Leaving zero Y margin when cropping $src_x = $_POST['left']; // Crop Start X $src_y = $_POST['top']; // Crop Start Y $dst_w = $_POST['width']; // Thumb width $dst_h = $_POST['height']; // Thumb height $src_w = $_POST['width']; // $src_x + $dst_w $src_h = $_POST['height']; // $src_y + $dst_h $dst_image = imagecreatetruecolor($dst_w, $dst_h); //$src_image = imagecreatefromjpeg("IMAG0278.jpg"); $src_image = imagecreatefromjpeg("photo2-W470xH601.jpg"); imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); imagejpeg($dst_image, "new.jpg"); ob_flush(); // at the end of the page ?>