0

我正在使用带有 GD 2.0.34 的 PHP 5.3.9 的 Jquery 插件 ImgAreaSelect。

按照插件中的一些示例,我添加了一个表单,它给出了 X 和 Y 值,从我开始选择图像直到选择结束。

一切正常,因为我正确接收了这些值,但我无法裁剪图像。遵循了一些示例/教程,但总是失败。

这是我的PHP代码:

$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;
}

我想知道是否有问题(很可能)。

感谢所有人并花时间提供帮助。

4

4 回答 4

2
imagecopyresampled($new, $copy, $x1, $y1, 0, 0, $w, $h, $size[0], $size[1]);

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

换句话说,imagecopyresampled() 将从位置 (src_x,src_y) 处宽度为 src_w 和高度为 src_h的 src_image 获取一个矩形区域,并将其放置在位置 (dst_x,dst_y) 处宽度为 dst_w 和高度 dst_h 的 dst_image 矩形区域中。

换句话说,您需要将其更改为:

imagecopyresampled($new, $copy,0 ,0 ,$x1, $y1, $w, $h, $w, $h);
于 2013-07-24T11:08:14.580 回答
0

无论如何,您也可以尝试此代码

<?php
  $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  = "path_to_file";
  $info = getimagesize( $src );

  switch( $info[2] ) {
    case IMAGETYPE_JPEG:
      $copy = imagecreatefromjpeg( $src );
      $new  = imagecreatetruecolor( $w, $h );
      imagecopyresampled( $new, $copy, 0, 0, $x1, $y1, $info[0], $info[1], $w, $h );
      header( 'Content-type: image/jpeg' );
      imagejpeg( $new );
    break;
    default:
    break;
  }
?>
于 2013-07-24T11:14:29.480 回答
0
//resize and crop image by center
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];

    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;

        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 7;
            break;

        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 80;
            break;

        default:
            return false;
            break;
    }

    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);

    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, 
Resize and crop image from center with PHP


$max_height, $width_new, $height);
    }

    $image($dst_img, $dst_dir, $quality);

    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}
//usage example
resize_crop_image(100, 100, "test.jpg", "test.jpg");
于 2017-04-19T09:51:26.203 回答
-1

谢谢大家,@Deepanshu 给了我一个链接,我看到了一段代码,但有点奇怪: bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

最后$src_w$src_h我不得不输入新的宽度和新的高度,而不是原始图像的宽度和高度。

因此,正常工作的最终代码是:

imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $w, $h);

于 2013-07-24T11:11:12.283 回答