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

标签 php javascript image-manipulation rotation crop

几天来一直在谷歌上搜索工具,除了 Kroppr 之外,我一无所获。但我无法 promise 使用无法在部署前在本地进行测试的工具。

我所需要的只是可以为用户提供裁剪和旋转图像的工具,并让服务器保存它,覆盖原始图像。似乎有很多裁剪工具,但没有什么可以旋转的。

有这样的工具吗?

最佳答案

还没有人回答这个问题?嗯,好吧,我想你会很难找到一个能完全按照你喜欢的方式工作的工具。我想您正在寻找类似于 Kroppr 的东西,但不依赖于服务。

所以这里有一些您可以用来构建一个的资源!

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

http://raphaeljs.com/

这两个看起来都非常可靠,可以让您旋转图像。

将其与裁剪器结合使用。显示图像的外观。

现在是偷偷摸摸的部分。您只需要跟踪两件事。

1)选择旋转 Angular 0-360

2)crop的x1,y1,x2,y2。

收集完这些数据后,调用服务器上的 php 脚本并将图像处理的值( Angular 、x1、y1、x2、y2)传递给它。这可以通过 ajax 的 POST 来完成,表单提交,或者您甚至可以使用 GET 并直接将它们作为 URL 中的变量发送

现在使用 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);
?>

这只是一个供您处理的快速而肮脏的示例。如果您希望它适用于 jpeg 以外的图像类型,您需要进行一些检查并使用 GD 的其他方法来处理 .pngs 或 .gifs。旋转和裁剪代码将保持不变。

现在剩下的大部分修补工作都在前端,我会把它留给你来决定。您需要捕获的只是一个旋转 Angular 和 x、y 裁剪点。

希望这对您有所帮助。如果您在前端方面需要更多帮助,请告诉我。

附注我会发布更多资源链接,但显然我只允许发布 2,因为我的声誉还不够高。

关于php - 使用 JS 和 PHP 裁剪和旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7791763/

相关文章:

php mysql 图像处理

php - 在 php 字符串中添加换行符以使其在 mysql 数据库中显示的正确方法

php - 设备方向和分页

PHP 在插入数据库之前删除空行

javascript - AngularJs 指令在更改时获取单选按钮 ng-model 值

image-processing - Graphicsmagick如何删除透明区域并缩小32位图像的 Canvas 大小

php - 使用 Toggle 按钮设置 href,使用 mysql 和 php

基于下拉菜单的javascript搜索

javascript - 修改d3js中 map 的大小

cocoa - 有没有最好的方法将 NSImage 调整为最大文件大小?