2014-11-24 79 views
0

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

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

所以请帮助我这个。

+0

http://www.white-hat-web-design.co.uk/blog/resizing -images-with-php/ – JMc 2014-11-24 20:37:49

+0

我使用包装干涉/图像。节省时间,效果很好。在packagist.org上获取它。 – Robbie 2014-11-24 20:45:49

回答

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(); 
+0

非常感谢你......这个作品是知府......再次感谢...... :) – 2014-11-28 07:16:05