无法将调整大小的图像移动到S3

时间:2015-12-02 07:35:24

标签: php image amazon-s3 image-resizing

我想在亚马逊s3桶中上传我的图片。我的原始图片上传成功但缩略图没有上传。我从stackflow中提到了一些解决方案..没有什么能满足这个解决方案..上传多个图像..原始图像可以成功上传..

这是我试过的代码

$fileCount = count($_FILES["image"]['name']);
            //echo $fileCount;
            for($i=0; $i < $fileCount; $i++)
            {
                $ImageName      = str_replace(' ','-',strtolower($_FILES['image']['name'][$i]));
                $tmp = $_FILES["image"]["tmp_name"][$i];
                $actual_image_name = "slider".time().$ImageName;
                $new = "slider_new".time().$ImageName;
                //echo $actual_image_name."<br>";
                $data=array('slider_image'=>$NewImageName);             
                if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ))
                {   
                    $targetFile ='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
                    $image = new SimpleImage(); 
                    $image->load($targetFile);
                    $fileTempName=$image->resize(80,80); 
                    $image->save('http://'.$bucket.'.s3.amazonaws.com/');
                        if($s3->putObjectFile($fileTempName, $bucket , $new, S3::ACL_PUBLIC_READ))
                        {
                        //insert query to database
                        }
                }

            }

以下是调整图像大小的类

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);
         // echo"jpg";

      } elseif( $this->image_type == IMAGETYPE_GIF ) {

         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
        //echo"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;
      echo $this->image;
   }      

}

0 个答案:

没有答案