使用jWindowCrop裁剪图像 - PHP

时间:2013-01-29 15:09:01

标签: php jquery crop

我正在使用这个jQuery插件裁剪图片:

http://www.tmatthew.net/jwindowcrop

正如您所看到的,在jQuery端使用它非常容易,但我的问题是使用PHP / GD裁剪真实图像。

带着一些瞪眼,我得到了:

$targ_w = $targ_h = 150;
$jpeg_quality = 90;

$src = 'demo_files/flowers.jpg';
$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);

但它没有处理由jQuery插件制作的缩放/缩小,如何裁剪图像并使用此插件和PHP保存它?

2 个答案:

答案 0 :(得分:3)

我想出来了,这是我的代码以防其他人有同样的问题,裁剪将由Zebra图像类完成:

http://stefangabos.ro/php-libraries/zebra-image/#documentation

PHP:

// The variables we got from the plugin in upload page:
$x = intval($_POST['x']);
$y = intval($_POST['y']);
$w = intval($_POST['w']);
$h = intval($_POST['h']);
// The img file which we want to crop
$tmp_file = 'path/to/img';
// Now include the Zebra class
include_once('path/to/Zebra_Image.php');
$image = new Zebra_Image();
$image -> preserve_aspect_ratio = true;
$image -> enlarge_smaller_images = true;
$image -> preserve_time = true;
$image -> jpeg_quality = 100;
// Now imagine that the user has selected the area which he want with the plugin, and we also want to make the image out put in a specific size(200*225):
$target_path = 'new/img/path'; // the output img path
$image -> source_path = $tmp_file;
$image -> target_path = $target_path;
$image -> crop($x, $y, $x + $w, $y + $h);
$image -> source_path = $target_path;
$image -> resize(200, 225,  ZEBRA_IMAGE_CROP_CENTER);

答案 1 :(得分:0)

我也在使用jwindowcrop。单击jwindowcrop的缩放时,w和h会发生变化。 (见附图)

get the new w and h from jwindowcrop (not the original image's

您必须确保使用了php手册中所述的正确参数 http://www.php.net/manual/en/function.imagecopyresampled.php 就我而言,我使用了imagecopyresized,我可以正确裁剪图像,包括缩放

dst_image
Destination image link resource.

src_image
Source image link resource.

dst_x
x-coordinate of destination point. 
(in my case the destination image should start from upper left corner)

dst_y
y-coordinate of destination point.
(in my case the destination image should start from upper left corner)

src_x
x-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from x=231, 231 pixels far from the left edge)

src_y
y-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from y=706, 706 pixels far from the top edge)

dst_w
Destination width.
(in my case, my new image should have a width of 800px)

dst_h
Destination height.
(in my case, my new image should have a height of 400px)

src_w
Source width.
(when my cropping function zooms, it changes the width and height of the original image)

src_h
Source height.
(when my cropping function zooms, it changes the width and height of the original image)

imagecopyresized(dst_image, src_image, 0, 0, 231, 706, 800, 400, 521, 318);