2014-11-24 82 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