2017-10-28 12 views
0

Phpで伸ばしずに画像を切り取るにはどうすればよいですか?Php |伸張せずに画像を切り取る

例えば、600,100画像をアップロードした場合、画像の中央に100x100に切り抜いてください。右から250px。明らかにこれらの数値は、ユーザーがアップロードする画像に依存します。ここで

は私の現在のコードです:私は間違っていないよ場合

$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

関連する問題