简体   繁体   English

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

[英]Resize the uploaded image using php

I need to resize the uploaded image and save it with given resolutions. 我需要调整上传图片的大小并以给定的分辨率保存它。 Assume user uploads just only one single image and I save it like 35x35, 100x100 and 512x512 after finishing the upload. 假设用户仅上传一张图片,上传完成后我将其保存为35x35、100x100和512x512。 finally his one upload save in my folder as 3 images with different resolutions. 最后,他上传的照片以3张不同分辨率的图像保存在我的文件夹中。 I've done up to this point using laravel... 至此我已经使用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 --------//
    }

so please help me for this. 所以请帮我

Here is what I have done to save the uploaded image with given resolutions: 这是我以给定的分辨率保存上传的图片所做的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM