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


Resize the uploaded image using php

我需要调整上传图像的大小并以给定的分辨率保存。假设用户只上传一张图像,完成上传后我将其保存为 35x35、100x100 和 512x512。最后,他的一次上传在我的文件夹中保存为3张不同分辨率的图像。到目前为止,我已经使用拉拉维尔完成了...

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 --------//
    }

所以请帮我这个。

以下是我为以给定分辨率保存上传的图像所做的工作:

//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();