Jump to content

Jquery image crop problems


unplugged_web

Recommended Posts

I came across this image crop script which is great and looks like it does exactly what I want it to do, but I'm having a lot of problems getting it to work. I've searched for a solution and although other people are having the same problem nobody seems to have a solution so I'd be grateful for any help please. I can select the area I want to 'crop' but when I click the Crop Image button I just get black thumbnail. I've checked I've got the most up-to-date version of jquery as well as the jcrop script. This is the code I've got:

<?phpsession_start();if (!empty ($_SESSION['loggedin']) ) {$_SERVER['PHP_SELF'];} else {  header("location: index.php");}/*** Jcrop image cropping plugin for jQuery* Example cropping script* @copyright 2008-2009 Kelly Hallman* More info: http://deepliquid.com/content/Jcrop_Implementation_Theory.html*/ if ($_SERVER['REQUEST_METHOD'] == 'POST'){$targ_w = $targ_h = 100;$jpeg_quality = 100; $src = $_POST['filename'];$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); exit;} // If not a POST request, display page below: ?>

......

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title><?php echo $title; ?></title><meta name="description" content="<?php echo $description; ?>" /><meta name="keywords" content="<?php echo $keywords; ?>" /><base href="<?php echo $base_url; ?>/"><link rel="stylesheet" type="text/css" href="../new_site/css/admin.css" /><link rel="stylesheet" href="../new_site/css/jquery.Jcrop.css" type="text/css" /><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script src="../new_site/js/jquery.Jcrop.js"></script><script type="text/javascript"><script language="Javascript">// Remember to invoke within jQuery(window).load(...)// If you don't, Jcrop may not initialize properlyjQuery(window).load(function(){ jQuery('#cropbox').Jcrop({boxWidth: 700,boxHeight: 700,onSelect: updateCoords,aspectRatio: 1,		bgOpacity: 0.2,		bgColor: 'black', }); }); function updateCoords(c){$('#CropX').val(c.x);$('#CropY').val(c.y);$('#CropW').val(c.w);$('#CropH').val(c.h);};</script></head><body><div id="container"><div class="artists index"><div id="homepage_thumbs"><div style="float: left; margin-right: 25px; height: 750px;"><img src="/uploads/image/filename/<?php echo $filename ?>" alt="<?php echo $title ?>" id="cropbox" title="<?php echo $title ?>" style="border: 1px solid #CCCCCC;"/></div><input value="Update Image" type="submit" /></form> <!-- This is the form that our event handler fills --><form action="new_site/admin/thumbnail_test.php?id=<?php echo $image_id ?>" method="post" onsubmit="return checkCoords();"><input name="filename" type="hidden" value="/home/user/domain.com/app/webroot/uploads/image/filename/<?php echo $filename ?>"/><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="Create Thumbnail" /></form><form method="post" name="delete_Image" style="margin-left: 25px;"><input type="button" onclick="confirmation()" value="Delete Image"></form><br /><br /></div></div>			</div>		</div>  </body></html>

Edited by thehappyappy
Link to comment
Share on other sites

Is the correct filename being submitted by the form?
I tried using the same filename as the image itself, but I had to use the full path for it because I got errors otherwise.
you one to many open script tags<script type="text/javascript"><script language="Javascript"> remove<script language="Javascript"> as language="Javascript" is never used these days.
Oops, I didn't notice that, I've removed that but it's still not working. If it helps I've switched on errors but none are shown. I wondered if it was something to do with the gd library but the site has just been moved to a server with the latest version so I guess that's not it?
Link to comment
Share on other sites

I tried using the same filename as the image itself, but I had to use the full path for it because I got errors otherwise.
Does that mean that you have verified that the form is submitting the correct filename, that the full path in PHP is correct, and that the file exists? Are you just assuming all of that is correct? If you're getting a blank image it sounds like the file you're trying to open doesn't exist.
Link to comment
Share on other sites

Does that mean that you have verified that the form is submitting the correct filename, that the full path in PHP is correct, and that the file exists? Are you just assuming all of that is correct? If you're getting a blank image it sounds like the file you're trying to open doesn't exist.
Yep it's all correct, but I just get a black image :(If I remove the first part of the path I get this error:
Warning: imagecreatefromjpeg(/uploads/image/filename/46.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/user/domain.com/app/webroot/new_site/admin/thumbnail_test.php on line 21 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/user/domain.com/app/webroot/new_site/admin/thumbnail_test.php on line 25ÿØÿàJFIFÿþ
but if I have the full path then the image is just black rather than showing the part of the image I've tried to crop
Link to comment
Share on other sites

Which elements is this supposed to target:
function updateCoords(c){$('#CropX').val(c.x);$('#CropY').val(c.y);$('#CropW').val(c.w);$('#CropH').val(c.h);};

Brilliant thank you, I copied the script from the website so I must have missed that. It now works where it creates a thumbnail of what ever I select! Fantastic. One question though the image source says the url instead of the filename. How do I change that name and save it to a folder? Thanks for your help.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...