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条)

报告相同问题?

悬赏问题

  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题