PHP GD图像调整大小并使用/ jQuery裁剪
Just wanted to put a simple cropper for thumbnails on my site. The jQuery plug-in jCrop grabs the coordinates and sends them to the PHP just fine, I've tested it. The problem is when the PHP crops it, it just leaves me with an empty image that has a "0" filesize. Here's the PHP:
//GRAB DIMENSIONS
//COORDINATES OF CROPPER BOX
$getDimX = htmlspecialchars(trim(urldecode($_POST['valX'])));
$getDimY = htmlspecialchars(trim(urldecode($_POST['valY'])));
//WIDTH AND HEIGHT OF CROPPER BOX
$getDimW = htmlspecialchars(trim(urldecode($_POST['valW'])));
$getDimH = htmlspecialchars(trim(urldecode($_POST['valH'])));
//SQUARE THUMBNAIL DIMENSIONS
$targ_w = $targ_h = 150;
//DIRECTORIES
$src = '../users/'.$_SESSION['userName'].'/temp/defaultPicTempResizePng.png';
$defaultDir = '../users/'.$_SESSION['userName'].'/default/defaultPic.png';
$img_r = imagecreatefrompng($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$getDimX,$getDimY,
$targ_w,$targ_h,$getDimW,$getDimH);
imagepng($dst_r, $defaultDir, 100);
$JSONData = array('true', ' ');
echo json_encode($JSONData);
After some googling, I found some people needing the original width and height of the image. That is easy to obtain and send to the php with jQuery if that's what is needed, but where do I input those if needed? Not sure if that's what I'm doing wrong or there's something incorrect in my php.
只想在我的网站上放一个简单的缩略图裁剪器。 jQuery插件jCrop抓取坐标和 将它们发送到PHP就好了,我已经测试过了。 问题是当PHP裁剪它时,它只留下一个具有“0”文件大小的空图像。 这是PHP: p>
// GRAB DIMENSIONS
// CROPPER BOX的COORDINATES
$ getDimX = htmlspecialchars(trim(urldecode($ _ POST ['valX'])) );
$ getDimY = htmlspecialchars(trim(urldecode($ _ POST ['valY'])));
// CROPPER BOX的宽度和高度
$ getDimW = htmlspecialchars(trim(urldecode($ _ POST ['valW '))));
$ getDimH = htmlspecialchars(trim(urldecode($ _ POST ['valH'])));
// SQUARE THUMBNAIL DIMENSIONS
$ targ_w = $ targ_h = 150;
/ n / / DIRECTORIES
$ src ='../users/'.$_SESSION['userName'].'/temp/defaultPicTempResizePng.png';
$ defaultDir ='.. / users /'。$ _ _SIONION ['userName ']。'/ default / defaultPic.png';
$ img_r = imagecreatefrompng($ src);
$ dst_r = ImageCreateTrueColor($ targ_w,$ targ_h);
imagecopyresampled($ dst_r,$ img_r ,0,0,$ getDimX,$ getDimY,
$ targ_w,$ targ_h,$ getDimW,$ getDimH);
imagepng($ dst_r,$ defaultDir,100);
$ JSONData = array( 'true','');
echo json_encode($ JSONData);
code> pre>
经过一些谷歌搜索,我发现有些人需要图像的原始宽度和高度。 这很容易获取并使用jQuery发送到php,如果这是需要的,但如果需要我在哪里输入? 不确定这是我做错了还是我的php中出现了错误。 p>
div>
Have you considered using timthumb? It's just 1 script and does everything for you if you pass the correct params. For example (not tested), if you were to want an image like the following:
width = 100
height = 75
quality = 90%
crop x = 0
crop y = 0
crop width = 100
crop height = 75
You'd have to just make a request to something like:
http://yourserver.com/timthumb.php?src=path/to/image.png&w=100&h=75&q=90&cx=0&cy=0&cw=100&ch=75
This will result in a thumbnail of 100x75 of your image, starting from the left upper corner. Timthumb integrates beautifully with jCrop, I've used it on a project too.