2017-10-28 67 views
0

如何裁剪圖像而不是用Php拉伸圖像?例如,如果我上傳600,100的圖像,我希望它在圖像的CENTER中裁剪爲100x100。因此,從左側250像素。 顯然這些數字將取決於用戶上傳的圖像。Php |沒有拉伸的作物圖像

這裏是我當前的代碼:

$width = imagesx($base64); 
$height = imagesy($base64); 

$tmp = imagecreatetruecolor($newWidth, $newHeight); 
imagecopyresampled($tmp, $base64, 
    0, 0, 
    0, 0, 
    $newWidth, $newHeight, 
    $width, $height); 

imagejpeg($tmp, $path) 

如果我沒有記錯的話,這個代碼將採取600x100的圖像和伸展下來到100×100。反對種植。

回答

-2

請儘量使用this code

$new = imagecreatefromjpeg($uploadedfile); 

$crop_width = imagesx($new); 
$crop_height = imagesy($new); 

$size = min($crop_width, $crop_height); 

if($crop_width >= $crop_height) { 
    $newx= ($crop_width-$crop_height)/2; 
    $im2 = imagecrop($new, ['x' => $newx, 'y' => 0, 'width' => $size, 'height' => $size]); 
} 
else { 
    $newy= ($crop_height-$crop_width)/2; 
    $im2 = imagecrop($new, ['x' => 0, 'y' => $newy, 'width' => $size, 'height' => $size]); 
} 

imagejpeg($im2,$filename,90); 

您需要設置值$uploadedfile$crop_width$crop_height$filename變量。

+0

這是否解決了您的問題,@Wyatt? – camelsWriteInCamelCase