我旋轉和裁剪圖像的PHP,但我得到的黑色邊框顯示,我知道你可以改變背景顏色,但我想旋轉和裁剪圖像填充整個圖像。基本上類似於background-size: cover;
(左)在CSS中與background-size: contain;
(右)類似。旋轉和裁剪
看到下面的圖片,右邊是我現在得到的,左邊是我想要達到的。要旋轉的度數是動態的,要生成的圖像和源圖像都是正方形(200x200)。
編輯:這裏是我的快速和骯髒的代碼:
$degrees = rand(1,360);
$square_image = imagerotate($square_image, $degrees, 0);
imagejpeg($square_image,NULL,100);
有了這個:
$rotate = imagecreatefromjpeg($image);
// part of code created by www.thewebhelp.com, modified
$square_size = 200;
$original_width = imagesx($rotate);
$original_height = imagesy($rotate);
if($original_width > $original_height){
$new_height = $square_size;
$new_width = $new_height*($original_width/$original_height);
}
if($original_height > $original_width){
$new_width = $square_size;
$new_height = $new_width*($original_height/$original_width);
}
if($original_height == $original_width){
$new_width = $square_size;
$new_height = $square_size;
}
$new_width = round($new_width);
$new_height = round($new_height);
$smaller_image = imagecreatetruecolor($new_width, $new_height);
$square_image = imagecreatetruecolor($square_size, $square_size);
imagecopyresampled($smaller_image, $rotate, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
if($new_width>$new_height){
$difference = $new_width-$new_height;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0-$half_difference+1, 0, 0, 0, $square_size+$difference, $square_size, $new_width, $new_height);
}
if($new_height>$new_width){
$difference = $new_height-$new_width;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0, 0-$half_difference+1, 0, 0, $square_size, $square_size+$difference, $new_width, $new_height);
}
if($new_height == $new_width){
imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
}
$degrees = rand(1,360);
$square_image = imagerotate($square_image, $degrees, 0);
imagejpeg($square_image,NULL,100);
顯示您的代碼。 – sachleen 2013-04-27 17:45:31
我認爲你的「背景大小」比喻不符合你的要求。 – Ejaz 2013-04-27 17:57:20
http://stackoverflow.com/a/22511805/2106820 – 2014-03-19 16:12:04