2011-09-06 2 views
1

로 자르고 난 사용자에게 프로필 사진을 변경의 기회를주고 싶다. 그러나 그들이 $ _POST를 통해 새로운 사진을 제출하면 사진의 크기를 다음과 같이 바 꾸면 :PHP는 업로드 후 이미지 크기를 조정하고 난 PHP에 사용자 프로필이 중심

높이 : 110px | 폭 : 높이와 관련된

폭 (폭이 높이보다 큰 경우) : 110px | 높이 : 폭과 관련이 있습니다 (높이가 너비보다 큰 경우)

크기 조정을 완료하면 사진을 자르려 고 110px x 110px가 되길 원합니다. 그러나 중심에 놓기를 원합니다.

예를 들어 사용자가 110px 너비 및 200px 높이 (크기 변경 후의 크기)로 사진을 업로드하면 자르기 이후의 새 이미지는 오른쪽에서 90x110x110 자릅니다. 이

기능이 .png, .gif.jpg 이미지를 받아 들일 것 중심됩니다에 상관없이 초기 형식이 무엇인지 만 JPG 형식의 새로운 이미지를 저장하지 않게 내가 원하는 오른쪽에서 왼쪽과 다른 45 픽셀에서 45 픽셀 자른 것입니다 .

나는 이러한 기능을 만들 많이 검색하고 난 대답을 찾을 수 있지만 시간이 좀 사소한 모든 것이 제대로 작동하지 않을 변경 atempt. 지금까지

내 코드 :

<?php 

$userfile_name = $_FILES["sgnIMG"]["name"]; 
$userfile_tmp = $_FILES["sgnIMG"]["tmp_name"]; 
$userfile_size = $_FILES["sgnIMG"]["size"]; 
$filename = basename($_FILES["sgnIMG"]["name"]); 
$file_ext = substr($filename, strrpos($filename, ".") + 1); 
$large_image_location = $target_path . $filename; 
$ext = ''; 

if ($file_ext == 'jpg') { 
    $ext = 1; 
} else if ($file_ext == 'gif') { 
    $ext = 2; 
} else if ($file_ext == 'png') { 
    $ext = 3; 
} else { 
    $ext = 0; 
} 

$target = $target_path . basename($_FILES["sgnIMG"]["name"]); 

if (move_uploaded_file($userfile_tmp, $target)) { 
    $newImg = resize110($target, $ext); 
    if (isset($_POST['imupd']) && ($_POST['imupd'] == 'up')) { 
     $sql = "UPDATE users SET avatar='" . str_replace('im/users/', '', $newImg) . "' WHERE id=" . $_SESSION['sesID'] . ""; 
     $result = mysql_query($sql); 
     if ($result) { 
      echo '<img src="' . $newImg . '" width="110" title="' . $file_ext . '"/>'; 
     } else { 
      echo '<img src="im/avatars/px.png" width="110" title="' . $file_ext . '"/>'; 
     } 
    } 
} else { 

} 

function getHeight($image) 
{ 
    $sizes = getimagesize($image); 
    $height = $sizes[1]; 
    return $height; 
} 

function getWidth($image) 
{ 
    $sizes = getimagesize($image); 
    $width = $sizes[0]; 
    return $width; 
} 

function resize110($image, $ext) 
{ 
    chmod($image, 0777); 
    $oldHeight = getHeight($image); 
    $oldWidth = getWidth($image); 
    if ($oldHeight < $oldWidth) { 
     $newImageHeight = 110; 
     $newImageWidth = ceil((110 * $oldWidth)/$oldHeight); 
     imagecopyresampled($newImage, $source, -ceil(($newImageWidth - 110)/2), 0, 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight); 
    } else { 
     $newImageHeight = ceil((110 * $oldHeight)/$oldWidth); 
     $newImageWidth = 110; 
     imagecopyresampled($newImage, $source, 0, -ceil(($newImageHeight - 110)/2), 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight); 
    } 
    $newImage = imagecreatetruecolor(110, 110); 
    chmod($image, 0777); 
    return $image; 
    switch ($ext) { 
     case 1; 
      $source = imagecreatefromjpeg($image); 
      break; 
     case 2; 
      $source = imagecreatefromgif($image); 
      break; 
     case 3; 
      $source = imagecreatefrompng($image); 
      break; 
    } 

    imagejpeg($newImage, $image, 90); 
    return $image; 
} 

답변

1

내가 많이 둘러 보았다 코드의 결합 다른 부분은 내가 발견했다. 그래서이 스크립트, PNG 이미지의 지프를 jpg이 걸릴 폭이 높이가 큰 경우 110px 높이의 큰 경우 110px 너비로 크기를 조정합니다. 가로 세로 비율은 남아있어 나머지 픽셀을 2로 나눠서 이미지를 가운데에 맞 춥니 다. 다른 크기

은 모든 곳에서 110로 변경합니다.

============================================== ====================================

<?php 

// pfpic -> the name of the <input type="file" name="pfpic"/> where user chooses file 

$target_path = "im/users/";        // the directory to store the uploaded and then resampled image 
$userfile_name = $_FILES["pfpic"]["name"];     // the name that the image file will have once uploaded 
$userfile_tmp = $_FILES["pfpic"]["tmp_name"];     // the temporary name the server uses to store the file 
$userfile_size = $_FILES["pfpic"]["size"];     // the size of the file that we want to upload 
$filename = basename($_FILES["pfpic"]["name"]);    // the full name of the file 
$file_ext = substr($filename, strrpos($filename, ".") + 1); // the file extension 
$large_image_location = $target_path.$filename;     // the full path to the file 
$ext=''; 


if($file_ext=='jpg') 
{ 
    $ext=1; 
} 
else if ($file_ext=='gif') 
{ 
    $ext=2; 
} 
else if ($file_ext=='png') 
{ 
    $ext=3; 
} 
else 
{ 
    $ext=0; 
} 

    $target = $target_path.basename(sha1($_SESSION['sesID']).'.'.'jpg'); 
    if($ext!=0) 
    { 
     if(move_uploaded_file($userfile_tmp,$target)) 
     { 
      $newImg=resize110($target,$ext); 
      echo '<img src="'.$newImg.'"/>'; 
     } 
     else 
     { 
      echo 'the file could not be uploaded, please try again'; 
     } 
    } 
    else 
    { 
     echo 'this file extension is not accepted, please use "jpg", "gif" or "png" file formats'; 
    } 

    function getHeight($image) 
    { 
     $sizes = getimagesize($image); 
     $height = $sizes[1]; 
     return $height; 
    } 

    function getWidth($image) 
    { 
     $sizes = getimagesize($image); 
     $width = $sizes[0]; 
     return $width; 
    } 


    function resize110($image,$ext) 
    { 
     chmod($image, 0777); 
     $oldHeight=getHeight($image); 
     $oldWidth=getWidth($image); 
     switch ($ext) 
     { 
      case 1; 
       $source = imagecreatefromjpeg($image); 
      break; 

      case 2; 
       $source = imagecreatefromgif($image); 
      break; 

      case 3; 
       $source = imagecreatefrompng($image); 
      break; 
     } 
     $newImage = imagecreatetruecolor(110,110); 
     $bgcolor = imagecolorallocate($newImage, 255, 255, 255); 
     imagefill($newImage, 0, 0, $bgcolor);  // use this if you want to have a white background instead of black 


     // we check tha width and height and then we crop the image to the center 
     if($oldHeight<$oldWidth) 
     { 
      $newImageHeight = 110; 
      $newImageWidth = ceil((110*$oldWidth)/$oldHeight); 
      imagecopyresampled($newImage,$source,-ceil(($newImageWidth-110)/2),0,0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight); 
     } 
     else 
     { 
      $newImageHeight = ceil((110*$oldHeight)/$oldWidth); 
      $newImageWidth = 110; 
      imagecopyresampled($newImage,$source,0,-ceil(($newImageHeight-110)/2),0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight); 
     } 

     //we save the image as jpg resized to 110x110 px and cropped to the center. the old image will be replaced 
     imagejpeg($newImage,$image,90); 

     return $image; 

    } 

?>

관련 문제