Controller Logic..
public function updateImage(Request $req){
$req->validate([
'ProfileImage' => 'required|image|mimes:jpg,png,gif|max:2048'
]);
//Process Image
$file = $req->file('ProfileImage');
//get filename with extension
$filenamewithextension = $file->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $file->getClientOriginalExtension();
//filename to store
$newnaam = $filenametostore = $filename.'_'.uniqid().'.'.$extension;
// $destinationPath = public_path('/images/profile');
// echo "<br>";
// $new_path_n_name = $file->move($destinationPath,$filenametostore);
// echo "<br>";
//-------------------------------------------------------
//previous code;
// $path = 'images/profile';
// $req->file('ProfileImage')->move($path, $filenametostore);
// $imgFile = Image::make($file->getRealPath());
// $imgFile->text( '© 2016-2021 Realestate - All Rights Reserved', 10, 40, function($font) {
// $font->file(public_path('ttffontfile/RobotoMono-Italic-VariableFont_wght.ttf'));
// $font->size('20');
// $font->color('#000000');
// $font->align('center');
// $font->valign('bottom-right');
// //$font->angle(90);
// })->save(public_path('images/profile').'/'.$filenametostore);
//Image-waterMark
// $thumbnail_images_Path = 'images/'.$filenametostore;
// Image::make($file)->resize($thumb_width,$thumb_height)->save($thumbnail_images_Path);
$watermark = Image::make(public_path('images/packageplan.png'));
$img = Image::make($file->getRealPath());
//way1
//$watermarkSize = $img->width() / 2; //half of the image size
//way2
//$watermarkSize = $img->width() - 20; //size of the image minus 20 margins
//way3
$resizePercentage = 85;//70% less then an actual image (play with this value)
$watermarkSize = round($img->width() * ((100 - $resizePercentage) / 100), 2); //watermark will be $resizePercentage less then the actual width of the image
// resize watermark width keep height auto
$watermark->resize($watermarkSize, null, function ($constraint) {
$constraint->aspectRatio();
});
//insert resized watermark to image center aligned
$img->insert($watermark, 'top-right');
// $img = Image::make($file->getRealPath());
// // directly get main image(but im getting image from form)
// //$img = Image::make(public_path('images/background.png'));
// /* insert watermark at bottom-right corner with 10px offset */
// $img->insert(public_path('images/packageplan.png'), 'top-right', 10, 10);
$img->save(public_path('images/profile/'.$newnaam));
$img->encode('png');
$type = 'png';
$new_image = 'data:image/' . $type . ';base64,' . base64_encode($img);
//------------------------------------------------------
// $image_name = substr($newnaam, 15);
$image_name = $newnaam;
//get old image
$OldImage = Profile::select('ProfileImage')->where('UserID',Auth::id())->first();
$old_name = $OldImage->ProfileImage;
//delete old image
if($OldImage->ProfileImage!=''){
//echo "not empty";
$destinationPath = 'images/profile';
File::delete($destinationPath.'/'.$old_name);
}
//Save new image
Profile::where('UserID',Auth::user()->id)->update([
'ProfileImage'=>$image_name,
]);
return redirect('/profile')->with('status', 'Profile Image updated successfully'); //is this actually OK?
}
thank for feed back
ReplyDeleteNice blog
ReplyDeleteanother reply
ReplyDelete