2011-01-10 2 views
1

동적으로 광고를 가장자리로 모서리를 그어 이미지를 특정 크기로 잘라내는 스크립트를 만들고 있습니다. 현재 스크립트는 사진에 둥근 모서리를 표시하지만 원본 이미지가 최종 출력 이미지 (140px x 120px)의 크기에 맞도록 크기가 조정되지 않습니다. 문제는 원본에 따라 원래 업로드 된 이미지가 측정 기준은 최종 PNG에 크기를 변경PHP 이미지가 동적으로 이미지 모서리 크기를 조정하고 둥근 모양이됩니다.

{ 
$image_file = $_FILES['image']['tmp_name']; 

    $corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // The default corner radius is set to 20px 
    $topleft = (isset($_GET['topleft']) and $_GET['topleft'] == "no") ? false : true; // Top-left rounded corner is shown by default 
    $bottomleft = (isset($_GET['bottomleft']) and $_GET['bottomleft'] == "no") ? false : true; // Bottom-left rounded corner is shown by default 
    $bottomright = (isset($_GET['bottomright']) and $_GET['bottomright'] == "no") ? false : true; // Bottom-right rounded corner is shown by default 
    $topright = (isset($_GET['topright']) and $_GET['topright'] == "no") ? false : true; // Top-right rounded corner is shown by default 
    $imagetype=$_FILES['image']['type']; 

    $endsize=$corner_radius; 
    $startsize=$endsize*3-1; 
    $arcsize=$startsize*2+1; 

    if (($imagetype=='image/jpeg') or ($imagetype=='jpg')) { 
    $image = imagecreatefromjpeg($image_file); 
    } else { 
    if (($imagetype=='GIF') or ($imagetype=='gif')) { 
    $image = imagecreatefromgif($image_file); 
    } else { 
    $image = imagecreatefrompng($image_file); 
    } 
    } 

    $forecolor ='#ffffff'; 
    $size = getimagesize($image_file); 
    // Top-left corner 
    $background = imagecreatetruecolor($size[0],$size[1]); 
    imagecopymerge($background, $image, 0, 0, 0, 0, $size[0], $size[1], 100); 
    $startx=$size[0]*2-1; 
    $starty=$size[1]*2-1; 
    $im_temp = imagecreatetruecolor($startx,$starty); 
    imagecopyresampled($im_temp, $background, 0, 0, 0, 0, $startx, $starty, $size[0], $size[1]); 
    $bg = imagecolorallocate($im_temp, 255,255,255); 
    $fg = imagecolorallocate($im_temp, 255,255,255); 

    if ($topleft == true) { 
    if(!imagearc($im_temp, $startsize, $startsize, $arcsize, $arcsize, 180,270,$bg))echo "nope"; 
    imagefilltoborder($im_temp,0,0,$bg,$bg); 
    } 
    // Bottom-left corner 

    // Top-right corner 
    if ($topright == true) { 
    imagearc($im_temp, $startx-$startsize, $startsize,$arcsize, $arcsize, 270,360,$bg); 
    imagefilltoborder($im_temp,$startx,0,$bg,$bg); 
    } 


    $image = imagecreatetruecolor(140,120); 
    imagecopyresampled($image, $im_temp, 0, 0, 0, 0, $size[0],$size[1],$starty+1310,$startx+1500); 


    // Output final image 


    if(!imagepng($image,'hello.png')) echo "boo"; 
    if(!imagedestroy($image)) echo "2"; 
    if(!imagedestroy($background)) echo "3"; 
    if(!imagedestroy($im_temp)) echo "4"; 

    } 

EDIT :

질문이 reized 그렇게는 둥근 가장자리 처리되는 140 X 120 이미지에 맞는 orginal 한 화상을 얻는 방법이다?

+2

그러나 귀하의 궁금한 점은 무엇입니까? –

+5

크기를 조정 한 후 반올림을 추가하지 않겠습니까? – DampeS8N

답변

0

여기에는 가로 세로 비율을 유지하기 위해 crop-to-fit 또는 letterboxing을 사용하여 이미지의 크기를 임의의 크기로 조정하는 PHP 함수에 대한 링크가 있습니다. 꽤 철저한 설명이 있습니다. 그러나 크기를 조정 한 후에 둥근 모서리를 추가 할 수 있습니다.

http://www.spotlesswebdesign.com/blog.php?id=1