PHP调整图像大小并上传到s3

时间:2014-07-07 02:27:36

标签: php image file-upload amazon-s3

我正在使用以下代码将图像重新调整为我想要的高度。

function resizeToHeight($height) {

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

然而,在我的旧代码中,我将图像保存在同一台服务器上 - 我从亚马逊学到了一些关于s3的内容,并希望将调整后的图像上传到该服务器。

目前,如果我想将图像上传到S3,请执行以下操作

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.";
                    }
    }

现在回到第一个代码我通常会通过这个方法保存图像

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

我想知道是否有人知道如何将图像保存到旧文件系统,而是将图像重新调整为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 个答案:

答案 0 :(得分:1)

您已经拥有了解决方案。

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

上面的部分将目标文件的大小调整为80X80

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

以上部分将目标文件保存到s3存储桶

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

上面的部分是在您上传到s3后删除计算机上的targetFile。如果您忘记了此部分,系统将填充调整大小的图像。