2017-10-28 44 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