2012-08-28 126 views
1

我正在拍攝源圖像並嘗試基於用戶選擇裁剪它。它總是留下一個黑色的空白空間,我認爲它與我使用的參數不正確的順序有關。在查看php文檔中的imagecopyresampled()之後,我仍然想盡一切辦法來完成這項工作。imagecopyresampled裁剪黑色條

這是我的。我爲示例使用了硬編碼值,但每個圖像的每個$targ_變量都會發生變化。

$targ_w = 183;  // width of cropped selection 
$targ_h = 140;  // height of cropped selection 
$targ_x = 79;   // x-coordinate of cropped selection 
$targ_y = 59;   // y-coordinate of cropped selection 
$targ_x2 = 263;   // x-coordinate of 2nd point (end of selection) 
$targ_y2 = 199;  // y-coordinate of 2nd point (end of selection) 

$src = '/uploads/test.png'; 
$img_r = imagecreatefrompng($src); 
$dst_r = ImageCreateTrueColor($targ_w, $targ_h); 

imagecopyresampled($dst_r,$img_r,0,0,$targ_x,$targ_y,$targ_x2,$targ_y2,$targ_w,$targ_h); 

$newFileName = '/uploads/test2.png'; 
imagepng($dst_r, $newFileName, 9); 
+0

如果我只是在錯誤的路徑上,請糾正我,但您的變量標識符建議您使用需要維度的座標。順序是_dest座標 - 源座標 - 目標尺寸 - 源尺寸_ – Andre

+0

它們是座標,但在技術上不是它們的尺寸,因爲我選擇的x和y要裁剪? –

+0

由於裁剪區域的尺寸(也是尺度)應該保留,因此目標尺寸和來源(=作物面積)尺寸必須相同。請參閱下面的答案。 – Andre

回答

2

也許這是因爲你沒有正確計算裁剪區域的尺寸。除此之外,您還可以使用座標,其中應該使用尺寸。比較參數7和8。 請注意,這不會保持透明度。

<?php 
$targ_x1 = 59;  // x-coordinate of cropped selection 
$targ_y1 = 69;  // y-coordinate of cropped selection 
$targ_x2 = 160;  // x-coordinate of 2nd point (end of selection) 
$targ_y2 = 82;  // y-coordinate of 2nd point (end of selection) 

//1st: dynamically calculate the dimensions 
$crop_area_width = $targ_x2-$targ_x1;  // width of cropped selection 
$crop_area_height = $targ_y2-$targ_y1;  // height of cropped selection 

$image_path = 'http://cdn.sstatic.net/stackoverflow/img/sprites.png'; 
$src_img = imagecreatefrompng($image_path); 
$dst_img = imagecreatetruecolor($crop_area_width, $crop_area_height); 

//2nd: cropping does not change scale so source and target dimensions are the same 
imagecopyresampled($dst_img, $src_img, 0, 0, $targ_x1, $targ_y1, $crop_area_width, $crop_area_height, $crop_area_width, $crop_area_height); 

header('Content-Type: image/png'); 
imagepng($dst_img); 
?> 
+0

就是這樣,我在x2/y2的印象之下計算了它,顯然我錯了。非常感謝你。 –

0

請參見下面的網址: -

Cropping an image with imagecopyresampled() in PHP without fopen

試試這個: -

<?php 

function load_file_from_url($url) { 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    $str = curl_exec($curl); 
    curl_close($curl); 
    return $str; 
} 

class cropImage{ 
var $imgSrc,$myImage,$thumb; 
function setImage($image) { 
     //Your Image 
     $this->imgSrc = $image; 

     //create image from the jpeg 
     $this->myImage = imagecreatefromstring($this->imgSrc) or die("Error: Cannot find image!"); 
     $this->thumb = imagecreatetruecolor(200, 112); 

     //imagecopyresampled($this->thumb, $this->myImage, 0, 0, 0, 45, 200, 112, 480, 270); 
     imagecopy($this->thumb, $this->myImage, 0, 0, 0, 45, 200, 112, 480, 270); 
    } 
    function renderImage() 
    {        
     header('Content-type: image/jpeg'); 
     imagejpeg($this->thumb); 
     imagedestroy($this->thumb); 
     //imagejpeg($this->myImage); 
     //imagedestroy($this->myImage); 
    } 
} 

    $image = new cropImage; 
    $image->setImage(load_file_from_url($_GET['src'])); 
    $image->renderImage(); 

?> 
+0

imagecopy的參數似乎太多了PHP.net說imagecopy()是 'bool imagecopy(resource $ dst_im,resource $ src_im,int $ dst_x,int $ dst_y,int $ src_x,int $ src_y,int $ src_w,int $ src_h)' –