2014-03-30 113 views
2

如何在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) ; 
?> 
+1

是否要約束圖像的寬高比? – Dai

+0

使用圓形半徑變換器 – Smash

+0

@Dai我相信OP想要裁剪一個矩形的中央正方形以獲得最大尺寸的平方(以獲得最有用的像素數據),然後將其重新縮放至42x42 ...或者,或者我只是浪費了10分鐘寫回答... – Shomz

回答

5

首先,您需要知道尺寸爲$ x和$ y的矩形的中心正方形在哪裏。

// horizontal rectangle 
if ($x > $y) { 
    $square = $y;    // $square: square side length 
    $offsetX = ($x - $y)/2; // x offset based on the rectangle 
    $offsetY = 0;    // y offset based on the rectangle 
} 
// vertical rectangle 
elseif ($y > $x) { 
    $square = $x; 
    $offsetX = 0; 
    $offsetY = ($y - $x)/2; 
} 
// it's already a square 
else { 
    $square = $x; 
    $offsetX = $offsetY = 0; 
} 

現在我們可以從它建立一個正方形,只需要調整它到42x42。像這樣的東西應該工作:

list($x, $y) = getimagesize($file); 

// code snippet from above goes here 
// so we get the square side and the offsets 

$endSize = 42; 
$tn = imagecreatetruecolor($endSize, $endSize); 
imagecopyresampled($tn, $image, 0, 0, $offsetX, $offsetY, $endSize, $endSize, $square, $square); 

所以,如果我們有一個矩形圖像100x80,代碼會找出大廣場大小爲80 x偏移量是10,y偏移爲0粗略地,它看起來像這樣:

100 
----------- 
|   | 
|   | 80 
|   | 
----------- 

    | 
    V 

    80 
---------    42 
|  |    ----- 
|  | 80 ---> | | 42 
|  |    ----- 
--------- 

我們裁剪從原來的長方形大廣場後,我們只需將它縮小到最終大小,這是你的情況42。


只是測試和完美的作品,一定要去除回聲線,如果你打算輸出圖像到瀏覽器(與頭部相結合)。

+0

感謝您的解釋! @shomz – user2192094

+0

不客氣,我很高興可以幫忙!你只需要分配$ x = $ width和$ y = $ height,它應該馬上工作。 – Shomz

+1

謝謝@shomz,它的工作原理完全正確! – user2192094

1

我不知道是否有人正在尋找這個,但我需要有一個600 x 600平方的圖像。

來源可以是任何矩形圖像,我需要維持原始圖像的比例並將原始矩形圖像置於600 x 600平方的白色圖像中。

  • SRC_FILE:源圖像的URL
  • destination_file:所在的路徑目標文件將被保存。
  • square_dimensions(像素)。我需要600,但可以是任何價值。
  • jpeg_quality:Quality(0,100)。我用默認的90

    function square_thumbnail_with_proportion($src_file,$destination_file,$square_dimensions,$jpeg_quality=90) 
    { 
        // Step one: Rezise with proportion the src_file *** I found this in many places. 
    
        $src_img=imagecreatefromjpeg($src_file); 
    
        $old_x=imageSX($src_img); 
        $old_y=imageSY($src_img); 
    
        $ratio1=$old_x/$square_dimensions; 
        $ratio2=$old_y/$square_dimensions; 
    
        if($ratio1>$ratio2) 
        { 
         $thumb_w=$square_dimensions; 
         $thumb_h=$old_y/$ratio1; 
        } 
        else  
        { 
         $thumb_h=$square_dimensions; 
         $thumb_w=$old_x/$ratio2; 
        } 
    
        // we create a new image with the new dimmensions 
        $smaller_image_with_proportions=ImageCreateTrueColor($thumb_w,$thumb_h); 
    
        // resize the big image to the new created one 
        imagecopyresampled($smaller_image_with_proportions,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    
        // *** End of Step one *** 
    
        // Step Two (this is new): "Copy and Paste" the $smaller_image_with_proportions in the center of a white image of the desired square dimensions 
    
        // Create image of $square_dimensions x $square_dimensions in white color (white background) 
        $final_image = imagecreatetruecolor($square_dimensions, $square_dimensions); 
        $bg = imagecolorallocate ($final_image, 255, 255, 255); 
        imagefilledrectangle($final_image,0,0,$square_dimensions,$square_dimensions,$bg); 
    
        // need to center the small image in the squared new white image 
        if($thumb_w>$thumb_h) 
        { 
         // more width than height we have to center height 
         $dst_x=0; 
         $dst_y=($square_dimensions-$thumb_h)/2; 
        } 
        elseif($thumb_h>$thumb_w) 
        { 
         // more height than width we have to center width 
         $dst_x=($square_dimensions-$thumb_w)/2; 
         $dst_y=0; 
    
        } 
        else 
        { 
         $dst_x=0; 
         $dst_y=0; 
        } 
    
        $src_x=0; // we copy the src image complete 
        $src_y=0; // we copy the src image complete 
    
        $src_w=$thumb_w; // we copy the src image complete 
        $src_h=$thumb_h; // we copy the src image complete 
    
        $pct=100; // 100% over the white color ... here you can use transparency. 100 is no transparency. 
    
        imagecopymerge($final_image,$smaller_image_with_proportions,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$pct); 
    
        imagejpeg($final_image,$destination_file,$jpeg_quality); 
    
        // destroy aux images (free memory) 
        imagedestroy($src_img); 
        imagedestroy($smaller_image_with_proportions); 
        imagedestroy($final_image); 
    } 
    
+0

太好了,但是我怎樣才能使它透明而不是白色填充? – Jameel