简体   繁体   English

使用JS和PHP裁剪和旋转图像

[英]Crop and rotate images with JS and PHP

Having been Googling for a tool for a few days, I have found nothing apart from Kroppr but there's no way I can commit to using a tool that can't be tested locally before deployment. 谷歌搜索工具几天后,除了Kroppr之外我什么都没找到,但是我无法承诺使用在部署之前无法在本地测试的工具。

All I need is something that can provide the facility for a user to crop and rotate an image, and have the server save it, overriding the original image. 我需要的是能够为用户提供裁剪和旋转图像的设施,并让服务器保存它,覆盖原始图像。 There seems to be plenty of cropping tools out there but nothing that can rotate as well. 似乎有很多裁剪工具,但没有什么可以旋转。

Is there such a tool? 有这样的工具吗?

No one has answered this yet? 还没有人回答这个问题? Hmmm well I think you are going to have a hard time finding a tool that does exactly what you like. 嗯,我认为你很难找到一个能完全符合你喜欢的工具。 I imagine you are looking for something similar to Kroppr, but not tied to a service. 我想你正在寻找类似于Kroppr的东西,但不是与服务有关。

So here are some resources you can use to build one! 所以这里有一些资源可以用来构建一个!

http://code.google.com/p/jqueryrotate/ http://code.google.com/p/jqueryrotate/

http://raphaeljs.com/ http://raphaeljs.com/

Both of these look like pretty solid libraries that will let you rotate an image. 这两个看起来都很漂亮的库,可以让你旋转图像。

Use this in conjunction with a cropper. 将此与裁剪器结合使用。 To display what the image will look like. 显示图像的外观。

Now here is the sneaky part. 现在这里是偷偷摸摸的部分。 You only need to keep track of 2 things. 你只需要跟踪两件事。

1)The selected angle of rotation 0-360 1)选定的旋转角度0-360

2)The x1, y1, x2, y2 of the crop. 2)作物的x1,y1,x2,y2。

Once you have collected this data, make a call to a php script on your server and pass it the values of the image manipulation (angle, x1, y1, x2, y2) This can be done through a POST via ajax, form submission, or you can even use a GET and just directly send them as variables in the URL 收集完这些数据后,调用服务器上的php脚本并传递图像操作的值(angle,x1,y1,x2,y2)这​​可以通过ajax,表单提交,或者您甚至可以使用GET直接将它们作为变量发送到URL中

Now do all of the image manipulation in PHP using GD. 现在使用GD进行PHP中的所有图像处理。

<?php
//Incoming infomation. This should be set by $_GET[] or $_POST[] rather than the hardcoded examples.
$x1 = 100;
$y1 = 100;
$x2 = 400;
$y2 = 400;
$degrees = 47;
$filename = 'images/ducks.jpeg';

//find the original image size
$image_info = getimagesize($filename);
$original_width = $image_info[0];
$original_height = $image_info[1];

//calculat what the new image size should be.
$new_width = abs($x2 - $x1);
$new_height = abs($y2 - $y1);


$image_source = imagecreatefromjpeg($filename);

//rotate
$rotate = imagerotate($image_source, $degrees, 0);

//rotating an image changes the height and width of the image.
//find the new height and width so we can properly compensate when cropping.
$rotated_width = imagesx($rotate);
$rotated_height = imagesy($rotate);

//diff between rotated width & height and original width & height
//the rotated version should always be greater than or equal to the original size.
$dx = $rotated_width - $original_width;
$dy = $rotated_height - $original_width;

$crop_x = $dx/2 + $x1;
$crop_y = $dy/2 + $y1;


$new_image = imagecreatetruecolor($new_width, $new_height);

imagecopyresampled($new_image, $rotate, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $new_width, $new_height);

//save over the old image.
imagejpeg($new_image, $filename);
?>

This is just a quick and dirty example for you to work off of. 这只是一个快速而又肮脏的例子,供你工作。 If you want this to work for image types other than jpeg you will need to do some checking and use the other methods of GD for handling .pngs or .gifs. 如果你想让它适用于jpeg以外的图像类型,你需要做一些检查并使用GD的其他方法来处理.pngs或.gifs。 The rotation and cropping code will remain the same. 旋转和裁剪代码将保持不变。

The majority of tinkering left is now in the front-end, I will leave that up to you to decide. 剩下的大部分修补现在都在前端,我会留给你决定。 All you need to capture is a rotation angle and the x,y cropping points. 您需要捕获的只是旋转角度和x,y裁剪点。

Hopefully this was helpful. 希望这很有帮助。 If you need more help on the front-end stuff let me know. 如果你需要更多关于前端的帮助,请告诉我。

ps I would post more links to resources, but apparently I am only allowed to post 2 because my reputation is not high enough yet. ps我会发布更多的资源链接,但显然我只允许发布2,因为我的声誉还不够高。

I have also found another way to crop images using javascript and have the server to effect the changes. 我还发现了另一种使用javascript裁剪图像的方法,并让服务器实现更改。 please check out this project on git . 请在git上查看这个项目 I am still testing it though, however, I will soon provide some code snippets, whenever I get it perfectly. 我仍在测试它,但是,每当我完美地完成它时,我将很快提供一些代码片段。 Image Cropper Image Cropper

The answer above is really good, but you could take a look at 上面的答案真的很好,但你可以看看

http://phpthumb.sourceforge.net/ - For croping http://phpthumb.sourceforge.net/ - 用于裁剪

http://code.google.com/p/jqueryrotate/ - jQuery rotate (posted above) http://code.google.com/p/jqueryrotate/ - jQuery旋转(上面发布)

These two worked good in all my projects 这两个在我的所有项目中都很好用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM