Search code examples
phpimagefile-uploadamazon-s3

PHP resize an image and upload to s3


I am using the following code which re-sizes an image to the height that I want.

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

However in my old code I was saving the image on the same server - I have since learned a little about S3 from Amazon and would like to upload the resized images to that.

Currently if I want to upload an image to S3 I do the following

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

Now going back to the first code I would normally save the image via this method

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

Instead of saving the image to the old file system, I would like to resize the image to 80x80 and upload it to s3.

Here is the full image resize script:

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

Solution

  • You have the solution already with you.

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

    That above part resizes the target file to 80X80

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

    The above part saves the targetfile to s3 bucket

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

    The above part to delete the targetFile on your machine, once you have uploaded to s3. If you forget this part, your system will be filled with resized images.