首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用jWindowCrop实现裁剪图像

用jWindowCrop实现裁剪图像
EN

Stack Overflow用户
提问于 2013-01-29 23:09:01
回答 2查看 2K关注 0票数 1

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

http://www.tmatthew.net/jwindowcrop

如你所见,在PHP端使用它真的很容易,但我的问题是用jQuery /GD裁剪真实的图像。

通过一些眼神,我得到了:

代码语言:javascript
复制
$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来裁剪和保存图像呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-30 05:28:15

我想出了这一点,这是我的代码,如果其他人有同样的问题,裁剪将由斑马图像类完成:

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

PHP:

代码语言:javascript
复制
// 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);
票数 3
EN

Stack Overflow用户

发布于 2014-06-05 16:12:22

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

你必须确保你使用了正确的参数,在我的例子中,你使用了php手册http://www.php.net/manual/en/function.imagecopyresampled.php,我使用了imagecopyresized,并且我可以正确地裁剪图像,包括缩放。

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14586011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档