2011-02-11 107 views
4

我想使用imagecreatetruecolor裁剪圖像,並且它總是裁剪出黑色空格,或者縮放太大。我希望圖像的寬度爲191像素,高度爲90像素,因此我還需要調整圖像大小和裁剪大小,因爲比例必須保持不變。那麼,有該項目的一些樣品:PHP - 使用imagecopyresampled()裁剪圖像?

enter image description here

容量調整腳本(簡體)是這樣的:

$src_img=imagecreatefromjpeg($photoTemp);  
list($width,$height)=getimagesize($photoTemp); 
$dst_img=imagecreatetruecolor(191, 90); 
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height); 

的$ newImage [ '莊稼']陣列包括:

['x'] => $_POST['inp-x'] 
['y'] => $_POST['inp-x'] 
['width'] => $_POST['inp-width'] 
['height'] => $_POST['inp-height'] 

但我得到的是:

enter image description here

有人看到,我在做什麼錯?

謝謝,邁克。

回答

0

好吧,我發現自己的問題,代碼應該是這樣的:

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 
1

嘗試

<?php 

$dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']); 

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height); 
+0

試用身份證,我得到的只是一個黑色的圖像。 =/ – Mike 2011-02-11 15:09:56

3

你可以做到這一點,我自己做了,而且它的工作原理

(x1,y1)=>其中作物開始

(x2,y2)=>其中作物結束

$filename = $_GET['imageurl']; 
$percent = 0.5; 

list($width, $height) = getimagesize($filename); 

$new_width = $_GET['x2'] - $_GET['x1']; 
$new_height = $_GET['y2'] - $_GET['y1']; 

$image_p = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($filename); 

imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height); 

// Outputs the image 
header('Content-Type: image/jpeg'); 
imagejpeg($image_p, null, 100); 
+0

這一個作品! – 2014-10-20 22:10:36

0

另外還有imagecrop功能,它允許你在一個數組傳遞與xywidthheight值。