我正在使用Jquery插件ImgAreaSelect和PHP 5.3.9以及GD 2.0.34。
根据插件中的一些示例,我添加了一个表单,该表单从我开始选择图像到选择结束的位置给出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;
}
我想知道是否有错误(最有可能)。
感谢所有人,并花时间提供帮助。
答案 0 :(得分: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_w的dst_image的矩形区域中。高度dst_h在位置(dst_x,dst_y)。
换句话说,您需要将其更改为:
imagecopyresampled($new, $copy,0 ,0 ,$x1, $y1, $w, $h, $w, $h);
答案 1 :(得分: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;
}
?>
答案 2 :(得分: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");
答案 3 :(得分:-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);