2015-02-10 79 views
1

我试图裁剪我的图像与jcrop.my裁剪过程的作品,但裁剪后图像的高度和宽度不正常工作。图像总是显示空白阴影。裁剪后的图像无法正常显示

after crop image look like

original image look like

我的代码

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
$targ_w = $_POST['w'];$targ_h = $_POST['h']; 
$jpeg_quality = 90; 
$src = 'amazonaws/'.$_POST['rq']; 

    $type = strtolower(substr(strrchr($src,"."),1)); 
    if($type == 'jpeg') $type = 'jpg'; 
    switch($type){ 
    case 'bmp': $img_r = imagecreatefromwbmp($src); break; 
    case 'gif': $img_r = imagecreatefromgif($src); break; 
    case 'jpg': $img_r = imagecreatefromjpeg($src); break; 
    case 'png': $img_r = imagecreatefrompng($src); break; 
    default : return "Unsupported picture type!"; 
    } 

    $img_r = imagecreatefromjpeg($src); 
    $dst_r = ImageCreateTrueColor($targ_w, $targ_h); 


    if($type == "gif" or $type == "png"){ 
    imagecolortransparent($dst_r, imagecolorallocatealpha($dst_r, 0, 0, 0, 127)); 
    imagealphablending($dst_r, false); 
    imagesavealpha($dst_r, true); 
    } 
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']); 


switch($type){ 
    case 'bmp': imagewbmp($dst_r,$src); break; 
    case 'gif': imagegif($dst_r,$src); break; 
    case 'jpg': imagejpeg($dst_r,$src,$jpeg_quality); break; 
    case 'png': imagepng($dst_r,$src); break; 
    } 
} 

回答

1

这里是imagecopyresampled()的原型:

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) 

在脚本中,您可以指定

$targ_w = $_POST['w']; $targ_h = $_POST['h']; 

当你骂

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']); 

你告诉源图像的大小与该DEST图像

你需要得到原始图像尺寸相同的复印功能

list($src_width, $src_height, $src_type, $src_attr) = getimagesize($src) ; 

并在复印功能中使用这些尺寸

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$src_width,$src_height);