Skip to content

Instantly share code, notes, and snippets.

@rubemlrm
Created February 11, 2016 16:38
Show Gist options
  • Select an option

  • Save rubemlrm/62f367b86619e2f8ee66 to your computer and use it in GitHub Desktop.

Select an option

Save rubemlrm/62f367b86619e2f8ee66 to your computer and use it in GitHub Desktop.
public static function saveImage($file,$height = null, $width = "400"){
if ($file) {
$destinationPath = public_path() . '/uploads/images/';
$ext = $file->getClientOriginalExtension(); // Get real extension according to mime type
$fullname = $file->getClientOriginalName(); // Client file name, including the extension of the client
$hashname = md5($fullname . time()) . '.' .$ext; // Hash processed file name, including the real extension
$uploadSuccess = $file->move($destinationPath, $hashname);
$imagePath = $destinationPath . $hashname;
$img = \Image::make($imagePath);
$img->resize($height, $width, function ($constraint) {
$constraint->aspectRatio();
});
$thumbPath = public_path() . '/uploads/images/bin_' . $hashname;
try {
$img->save($thumbPath);
} catch (\Intervention\Image\Exception\NotWritableException $e) {
$this->logger->writeLog("error", "user " . $this->userRepository->getCurrentUserName() . " tried upload image <b>" . $thumbPath . "</b> and got the following error <code>". $e . "</code> ", "settings");
return back()->withInput()->withErrors(['errors'=>'Ocorreu um erro ao enviar a imagem relativa ao twittercard']);
}
return $hashname;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment