2014-07-07 3 views
1

다음 코드를 사용하여 원하는 높이로 이미지의 크기를 조정합니다. PHP가 이미지의 크기를 조정하고 s3에 업로드

function resizeToHeight($height) { 

     $ratio = $height/$this->getHeight(); 
     $width = $this->getWidth() * $ratio; 
     $this->resize($width,$height); 
    } 

그러나 내 예전의 코드에 나는 동일한 서버에 이미지를 저장했다 - 나는 이후 아마존에서 S3에 대해 조금 배운 그에게 크기가 조정 된 이미지를 업로드하고 싶습니다. 나는 내가 지금은 일반적으로

$image = new SimpleImage(); 
     $image->load($targetFile); 
     $image->resizeToHeight(80); 
     $image->save(rtrim($targetPath,'/') . '/' . md5($_FILES['Filedata']['name']) . '-x-h80.' . $ext); 



function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image,$filename,$compression); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image,$filename); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     imagepng($this->image,$filename); 
     } 
     if($permissions != null) { 

     chmod($filename,$permissions); 
     } 
    } 
function load($filename) { 

     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 
     if($this->image_type == IMAGETYPE_JPEG) { 

     $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 

     $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 

     $this->image = imagecreatefrompng($filename); 
     } 
    } 
이 방법으로 이미지를 저장할 것 다시 첫 번째 코드로가는

function uploadmedia(){ 
       include('s3upload/image_check.php'); 
       $name = $_FILES['file']['name']; 
       $size = $_FILES['file']['size']; 
       $tmp = $_FILES['file']['tmp_name']; 
       $ext = getExtension($name); 

       if(in_array($ext,$valid_formats)) 
        { 
         if($size < 1048576) 
         { 
         include('s3upload/s3_config.php'); 
         //Rename image name. 
         $savename = base64_encode($name); 
         $actual_image_name = $savename.time().".".$ext; 
          if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ)) 
          { 
           $s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name; 
           } 
          else 
          $msg = "S3 Upload Fail."; 
         } 
         else 
         $msg = "Image size Max 1 MB"; 
        } 
        else 
        { 
        $msg = "Invalid file, please upload image file."; 
        } 
    } 

이었다 다음 내가 할 S3에 이미지를 업로드하려면 현재 경우

누구든지 이미지를 이전 파일 시스템에 저장하는 대신에 80x80 크기로 이미지를 다시 업로드하고 s3에 업로드하는 방법을 알고 있다면 궁금합니다.

감사합니다.

여기에 당신은 당신과 함께 이미 솔루션을 전체 이미지 크기 조정 스크립트

class SimpleImage { 

    var $image; 
    var $image_type; 

    function load($filename) { 

     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 
     if($this->image_type == IMAGETYPE_JPEG) { 

     $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 

     $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 

     $this->image = imagecreatefrompng($filename); 
     } 
    } 
    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image,$filename,$compression); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image,$filename); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     imagepng($this->image,$filename); 
     } 
     if($permissions != null) { 

     chmod($filename,$permissions); 
     } 
    } 
    function output($image_type=IMAGETYPE_JPEG) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     imagepng($this->image); 
     } 
    } 
    function getWidth() { 

     return imagesx($this->image); 
    } 
    function getHeight() { 

     return imagesy($this->image); 
    } 
    function resizeToHeight($height) { 

     $ratio = $height/$this->getHeight(); 
     $width = $this->getWidth() * $ratio; 
     $this->resize($width,$height); 
    } 

    function resizeToWidth($width) { 
     $ratio = $width/$this->getWidth(); 
     $height = $this->getheight() * $ratio; 
     $this->resize($width,$height); 
    } 

    function scale($scale) { 
     $width = $this->getWidth() * $scale/100; 
     $height = $this->getheight() * $scale/100; 
     $this->resize($width,$height); 
    } 

    function resize($width,$height) { 
     $new_image = imagecreatetruecolor($width, $height); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    }  

} 





// Define a destination 
$targetFolder = '/uploads'; // Relative to the root 


if (!empty($_FILES["Filedata"])) { 
    $name = $_FILES['Filedata']['name']; 
    $ext = end(explode(".", $name));  

    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; 
    $targetFile = rtrim($targetPath,'/') . '/' . md5($_FILES['Filedata']['name']) . '.' . $ext; 


    // Validate the file type 
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions 
    $fileParts = pathinfo($_FILES['Filedata']['name']); 
    $link = array(); 
    $link['large'] = "http://www.ipetfindr.com/petuploads/". md5($_FILES['Filedata']['name']) . '.' . $ext; 
    $link['small'] = "http://www.ipetfindr.com/petuploads/". md5($_FILES['Filedata']['name']) . '-x-h80.' . $ext; 

     move_uploaded_file($tempFile,$targetFile); 
     $image = new SimpleImage(); 
     $image->load($targetFile); 
     $image->resizeToHeight(80); 
     $image->save(rtrim($targetPath,'/') . '/' . md5($_FILES['Filedata']['name']) . '-x-h80.' . $ext); 




     echo '1'; 
} 
else 
    {print "did not work";} 

답변

1

입니다.

부분이되는 80x80

$s3->putObjectFile($targetFile, $bucket , $name_in_s3, S3::ACL_PUBLIC_READ); 

에 대상 파일의 크기를 조절 위의 위의 부분은 S3 버킷에는 targetfile 저장 그건

$image = new SimpleImage(); 
    $image->load($targetFile); 
    $image->resize(80,80); 
    $image->save($url); 

unlink("/path/to/targetFile"); 

컴퓨터에는 targetfile을 삭제하려면 위의 부분, 일단 s3에 업로드하면 이 부분을 잊어 버리면 시스템이 크기가 조정 된 이미지로 채워집니다.

+1

파일을 로컬에서 먼저 저장 한 다음 크기를 조정하고 s3에 업로드 한 후 파일의 링크를 해제하는 것이 좋습니다. 그러나 서버의 폴더 (임시 폴더 제외)에 파일 업로드를 피하려고합니다. – RussellHarrower

관련 문제