【问题标题】:Crop and rotate images with JS and PHP使用 JS 和 PHP 裁剪和旋转图像
【发布时间】:2011-12-09 03:46:39
【问题描述】:

几天来一直在谷歌上搜索一个工具,除了Kroppr,我什么也没找到,但我无法承诺在部署之前使用无法在本地测试的工具。

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

有这样的工具吗?

【问题讨论】:

    标签: php javascript image-manipulation rotation crop


    【解决方案1】:

    我还找到了另一种使用 javascript 裁剪图像并让服务器影响更改的方法。 please check out this project on git。虽然我仍在测试它,但是,我很快就会提供一些代码 sn-ps,只要我能完美地得到它。 Image Cropper

    【讨论】:

      【解决方案2】:

      上面的答案真的很好,但是你可以看看

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

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

      这两个在我所有的项目中都很有效

      【讨论】:

        【解决方案3】:

        还没有人回答这个问题吗?嗯,我认为您将很难找到一个完全符合您喜好的工具。我想您正在寻找类似于 Kroppr 的东西,但与服务无关。

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

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

        http://raphaeljs.com/

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

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

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

        1)选择的旋转角度0-360

        2)作物的x1, y1, x2, y2。

        一旦你收集了这些数据,在你的服务器上调用一个 php 脚本并将图像处理的值传递给它(角度、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 的其他方法来处理 .png 或 .gifs。旋转和裁剪代码将保持不变。

        现在剩下的大部分修补都在前端,我将由您决定。您只需要捕获一个旋转角度和 x,y 裁剪点。

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

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

        【讨论】:

          猜你喜欢
          • 2015-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-28
          • 2013-02-09
          • 2020-02-01
          • 2013-04-21
          • 1970-01-01
          相关资源
          最近更新 更多