1

我正在使用这个 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 保存它?

4

2 回答 2

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);
于 2013-01-29T21:28:15.417 回答
0

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

从 jwindowcrop 获取新的 w 和 h(不是原始图像的

您必须确保使用 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);
于 2014-06-05T08:12:22.000 回答