2016-09-28 87 views
0

我正在編寫一個腳本,它需要一個箭頭圖像並將其旋轉一定的度數。使用下面的代碼,當角度是90的倍數時,圖像旋轉並按預期顯示。PHP imagerotate正在裁剪圖像

源圖像看起來像這樣(74 X 74):

enter image description here

圖像旋轉90°後:

enter image description hereenter image description here

圖像由任何其他數量(不轉動後90的倍數)例如45:

enter image description here

從圖像中可以看出,箭頭已從圖像中裁剪出來。誰能告訴我爲什麼會發生這種情況?再次,90的倍數很好,只是發生不尋常剪切的其他數字。

$props = ['w' => 74, 'h' => 74]; 
$angle = 360 - $_GET['angle']; 

$final_img = imagecreatetruecolor($props['w'], $props['h']); 
imagesavealpha($final_img, true); 
$transColor = imagecolorallocatealpha($final_img, 0, 0, 0, 127); 
imagefill($final_img, 0, 0, $transColor); 

$rotate = imagecreatefrompng('arrow.png'); 
$src = imagerotate($rotate, $angle, $transColor); //rotated my image 

$src_x = ImageSX($src); //find out new x width 
$src_y = ImageSY($src); //find out new y height 

$src_widthx = $src_x/2 - $props['w']/2; // divide each by 2 and then subtract desired end width from wider rotated width 
$src_heighty = $src_y/2 - $props['h']/2; // and again for height 

imagecopy($final_img, $src, 0, 0, $src_widthx, $src_heighty, $props['w'], $props['h']); 

header('Content-Type: image/png'); 
imagepng($final_img); 

回答

0

當旋轉通過的nXm像素的正方形可以說45度你會得到一個對角線(其比n或m和等於SQRT(N^2 + M^2)更大的)圖像是新的旋轉圖像的寬度和高度。

該函數使用圖像的原始尺寸(即n和m)裁剪旋轉後的圖像。

解決這個問題的一種方法是通過使用合適的大小sqrt(width_original_image^2 + height_original_image^2)來創建更大的空白圖像,然後使用imagecopy將原始圖像複製到新圖像。之後,您可以在新圖像上使用imagerotate

+0

謝謝你的回答Amitay。你能推薦一種解決方案來實現我想以另一種方式或通過修改我的代碼? – jskidd3

+0

什麼是'n',什麼是'm'?爲了讓我接受你的答案,我將需要更多的細節 – jskidd3

+0

我嘗試了你的建議。不幸的是,更大的空白圖像不能解決問題,不管創建的空白圖像的大小如何,箭頭總是會出現裁剪。 – jskidd3

0

我安裝並使用了ImageMagick PHP庫,並且旋轉顯示未被剪裁,無論旋轉的程度如何。