drqwbh2150 2013-07-24 10:32
浏览 53
已采纳

使用Php裁剪图像

I'm using Jquery plugin ImgAreaSelect with PHP 5.3.9 with GD 2.0.34.

Following some examples from the plugin, I added a form that gives X and Y values from where I start selecting the image till the end of the selection.

This is going OK, because I recieve the values correctly, but I cannot crop the image. Followed some examples/tutorials but always failed.

Here is my PHP code:

$x1 = $_POST['x1']; //this one gives me the point where start to crop
$x2 = $_POST['x2']; //the end of X axis
$y1 = $_POST['y1']; //same for Y1 and Y2
$y2 = $_POST['y2'];
$w = $x2 - $x1; //getting the width for the new image
$h = $y2 - $y1; //getting the height for the new image
$src_img = "path/image";
$format = end(explode(".", $src_img)); //taking the image format (jpg, png, gif)
$size = getimagesize($src_img);
switch($format) {
    case "jpg":
        $copy = imagecreatefromjpeg($src_img);
        $new = ImageCreateTrueColor($w, $h);
        imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $size[0], $size[1]);
        header('Content-type: image/jpeg');
        imagejpeg($new);
    break;
}

I would like to know if there's something wrong (most probably).

Thanks for all and taking your time to help.

  • 写回答

4条回答 默认 最新

  • doutuichan2681 2013-07-24 11:08
    关注
    imagecopyresampled($new, $copy, $x1, $y1, 0, 0, $w, $h, $size[0], $size[1]);
    

    http://php.net/manual/en/function.imagecopyresampled.php

    In other words, imagecopyresampled() will take a rectangular area from src_image of width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).

    in other words, you need to change it to:

    imagecopyresampled($new, $copy,0 ,0 ,$x1, $y1, $w, $h, $w, $h);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 微信聊天记录备份到电脑提示成功了,但还是没同步到电脑微信
  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥20 wpf datagrid单元闪烁效果失灵