使用 jWindowCrop 裁剪图像 - PHP

Posted

技术标签:

【中文标题】使用 jWindowCrop 裁剪图像 - PHP【英文标题】:Crop Image with jWindowCrop - PHP 【发布时间】:2013-01-29 15:09:01 【问题描述】:

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

【问题讨论】:

【参考方案1】:

我想通了,这是我的代码,以防其他人有同样的问题,裁剪将由 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);

【讨论】:

【参考方案2】:

我也在使用 jwindowcrop。当您单击 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);

【讨论】:

以上是关于使用 jWindowCrop 裁剪图像 - PHP的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP 或 Javascript 裁剪图像 [关闭]

使用 PHP 裁剪图像

使用 php 裁剪图像

从中心 PHP 裁剪图像

使用 PHP (GD) 进行图像裁剪

使用 php 保存裁剪的图像