2015-02-10 32 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);