2014-03-30 2 views
2

사각형 이미지를 PHP의 사각형 모양의 아바타로 어떻게 변경합니까? 업로드 된 이미지의 해상도가 무엇이든 관계없이 중앙 42x42 픽셀 아바타로 크기를 조정할 수 있습니다. 이것은 내가 사용하고있는 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로 조정하면됩니다. 이런 식으로 뭔가 작동합니다 : 우리는 직사각형 이미지 100x80이있는 경우

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); 

그래서, 코드가 큰 사각형 크기가 80 알아낼 것이다, 오프셋 X Y 오프셋, 10 대략 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 제곱 이미지가 있어야했습니다.

소스는 모든 직사각형 이미지 일 수 있으며 원래 이미지의 비율을 유지하고 원래 직사각형 이미지의 중심을 600x600 제곱의 흰색 이미지로 중앙에 배치해야했습니다.

는 여기가

  • src_file입니다 : 소스 이미지의 URL
  • destination_file : 대상 파일이 저장 될 경로.
  • 평방 _dimensions (픽셀). 나는 600을 필요로했지만 어떤 값이 될 수있다.
  • jpeg_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

관련 문제