使用php调整上传图像的大小

时间:2014-11-24 20:09:49

标签: php file-upload laravel image-resizing

我需要调整上传的图片大小并以给定的分辨率保存。假设用户只上传了一张图片,我在完成上传后将其保存为35x35,100x100和512x512。最后他的一个上传保存在我的文件夹中作为3个不同分辨率的图像。我已经使用laravel完成了这一点...

public function postSingleUpload()
    {
        //create the relevant directory to add the user image
        //get the directory name (directory name equals to user id)
        $dirPath = sprintf("images/users/avatar/%s/", Auth::user()->id);
        //create the directory named by user id
        if (!file_exists($dirPath)) {
            mkdir($dirPath, 0700);
        }

        $file = Input::file('image');

        //save image with given resulutions
        //---- this part i need --------//
    }

所以请帮助我。

1 个答案:

答案 0 :(得分:0)

  

以下是我使用给定分辨率保存上传图像的方法:

//First Copy the uploaded image to some location
  Input::file('profilePic')->move('Users/'.$username.'/Wallpics/',$name)

  //Set this attribute for quality after resampling
  $quality = 90;
  $src  = 'Users/'.$username.'/Wallpics/'.$name;

  //Run this on recently saved uploaded image
  $img  = imagecreatefromjpeg($src);

  //get this values from user by submitting form ( either by crop or by textboxes)
  $width=(int)Input::get('w');
  $height=(int)Input::get('h');
  $x=(int)Input::get('x');
  $y=(int)Input::get('y');

  //This is the code to resample the image and generate a new to ur requirements

  $dest = ImageCreateTrueColor($width, $height);
  imagecopyresampled($dest, $img, 0, 0,$x,$y, $width, $height,$width,$height);
  imagejpeg($dest, 'Users/'.$username.'/profilePic/'.$name, $quality);

  //Set the path in database 
  $profile->profilePic=asset('Users/'.$username.'/profilePic/'.$name);
  $profile->save();