2017-10-26 4 views
0

아래 스크립트는 업로드 된 이미지를 처리하고 크기를 조정하여 최대 높이 또는 너비 (둘 중 더 긴 쪽)가 200 픽셀이되도록 잘 작동합니다. 완벽 정사각형 이미지 또는 200x140 또는 140x200 등 PHP에서 정사각형이 아닌 사진 주위에 공백을 추가하면 항상 200x200 픽셀이됩니다.

if(isset($_FILES['image'])) { 
     $img = $_FILES['image']['name']; 
     $tmp = $_FILES['image']['tmp_name']; 

     // get uploaded file's extension 
     $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION)); 

     //checking if image exists for this pool and removing if so, before adding new image in its place 
     if(file_exists("uploads/".$poolid.".png")) { 
     unlink("uploads/".$poolid.".png"); 
     } 

     // checks valid format 
     if(in_array($ext, $valid_extensions)) { 
     //re-size the image and make it a PNG before sending to server 
     $final_image = $poolid . ".png"; 
     $path = "uploads/".strtolower($final_image); 
     $size = getimagesize($tmp); 
     $ratio = $size[0]/$size[1]; // width/height 
     if($ratio > 1) { 
      $width = 200; 
      $height = 200/$ratio; 
     } 
     else { 
      $width = 200*$ratio; 
      $height = 200; 
     } 
     $src = imagecreatefromstring(file_get_contents($tmp)); 
     $dst = imagecreatetruecolor($width,$height); 
     imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); 
     imagedestroy($src); 
     imagepng($dst, $path); // adjust format as needed 
     imagedestroy($dst); 

     $_SESSION['image_uploaded']="yes"; 
     echo $path ."?".rand(1,32000); 
     } else { 
      echo 'invalid file'; 
     } 
    } 

지금, 오픈 그래프를 사용하여 페이스 북 공유가 최소 200x200 크기로 이미지가 필요하다면 그래서 200x200 크기를 수 있습니다. 따라서 140x200 이미지는 공유 기능으로 작동하지 않습니다.

어쨌든 비 정사각형 이미지를 좋아하지 않으므로 이미지를 찍고 싶습니다. 이미 사각형이 아닌 경우 측면 (위/아래) 및 공백을 추가하고 싶습니다. 매주 완벽한 200x200 제곱으로 저장하십시오.

나는 아래이 시도하지만 (어떤 이미지가 전혀 생성되지됩니다) 작동하지 않습니다. 내가 뭘하려고했는데 뭐가 잘못 되었 니? 이것은 지나치게 복잡하지는 않지만 명확하게 나는 뭔가를 놓치고있다.

if(isset($_FILES['image'])) { 
    $img = $_FILES['image']['name']; 
    $tmp = $_FILES['image']['tmp_name']; 

    // get uploaded file's extension 
    $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION)); 

    //checking if image exists for this pool and removing if so, before adding new image in its place 
    if(file_exists("uploads/".$poolid.".png")) { 
    unlink("uploads/".$poolid.".png"); 
    } 

    // checks valid format 
    if(in_array($ext, $valid_extensions)) { 
    //re-size the image and make it a PNG before sending to server 
    $final_image = $poolid . ".png"; 
    $path = "uploads/".strtolower($final_image); 
    $size = getimagesize($tmp); 
    $ratio = $size[0]/$size[1]; // width/height 
    if($ratio > 1) { 
     $width = 200; 
     $height = 200/$ratio; 
    } 
    else { 
     $width = 200*$ratio; 
     $height = 200; 
    } 
    $src = imagecreatefromstring(file_get_contents($tmp)); 
    $dst = imagecreatetruecolor($width,$height); 
    $orig_img=imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); 
    imagedestroy($src); 

    // create new image and fill with background colour 
    $new_img = imagecreatetruecolor($output_w, $output_h); 
    $bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red 
    imagefill($new_img, 0, 0, $bgcolor); // fill background colour 

    // copy and resize original image into center of new image 
    $final_img=imagecopyresampled($new_img, $orig_img, 0, 0, 0, 0, 200, 200, $width, $height); 
     imagepng($final_img, $path); // adjust format as needed 

    imagedestroy($dst); 
    $_SESSION['image_uploaded']="yes"; 
    echo $path ."?".rand(1,32000); 
    } else { 
     echo 'invalid file'; 
    } 
} 
+0

'imagecopyresampled'는 다른 이미지 자원이 아니라 부울을 반환합니다. – Matey

답변

1

임시 중간 이미지가 필요하지 않습니다. 리샘플링 된 원본 이미지를 배경으로 채우면 대상 이미지에 바로 붙여 넣을 수 있습니다. 여기를 참조하십시오 :

$src = imagecreatefromstring(file_get_contents($tmp)); 

    // Create new image and fill it with background color 
    $dst = imagecreatetruecolor($output_w,$output_h); 
    $bgcolor = imagecolorallocate($dst, 255, 0, 0); 
    imagefill($dst, 0, 0, $bgcolor); 

    // Copy resampled src image into dst 
    if ($ratio > 1) 
    imagecopyresampled($dst, $src, 0, ($output_h - $height)/2, 0, 0, $width, $height, $size[0], $size[1]); 
    else 
    imagecopyresampled($dst, $src, ($output_w - $width)/2, 0, 0, 0, $width, $height, $size[0], $size[1]);  

    imagepng($dst, $path); // adjust format as needed 

    imagedestroy($src); 
    imagedestroy($dst); 
관련 문제