如何在PHP中將矩形圖像更改爲方形的頭像,以便無論上傳圖像的分辨率如何,都可以調整爲集中的42 x 42像素頭像。這是我使用的PHP代碼。任何人都可以建議。PHP - 如何將矩形圖像轉換爲方形圖像?
<?php
//Name you want to save your file as
$save = 'myfile1.jpg';
$file = 'original1.jpg';
echo "Creating file: $save";
$size = 0.45;
header('Content-type: image/jpeg') ;
list($width, $height) = getimagesize($file) ;
$modwidth = $width * $size;
$modheight = $height * $size;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
// Here we are saving the .jpg, you can make this gif or png if you want
//the file name is set above, and the quality is set to 100%
imagejpeg($tn, $save, 100) ;
?>
是否要約束圖像的寬高比? – Dai
使用圓形半徑變換器 – Smash
@Dai我相信OP想要裁剪一個矩形的中央正方形以獲得最大尺寸的平方(以獲得最有用的像素數據),然後將其重新縮放至42x42 ...或者,或者我只是浪費了10分鐘寫回答... – Shomz