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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程