Skip to main content

Upload Image with Image Watermark using Intervention Package.

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?

   

    }

Comments

Post a Comment

Popular posts from this blog

Install MariaDB Latest Version 11.4 in Red Hat Version 9

 This this post i will show you step by step the installation process of mariaDB in red hat version 9. Step1 Run the command to pull the latest updated packages on applications installed in your system. -dnf update If you get Kernal update than reboot the system -reboot Step2 Go to official mariaDB site Make mariadb repository in /etc/yum.repos.d Place the configuration in this file # MariaDB 11.4 RedHatEnterpriseLinux repository list - created 2024-09-24 11:12 UTC # https://mariadb.org/download/ [mariadb] name = MariaDB # rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details. # baseurl = https://rpm.mariadb.org/11.4/rhel/$releasever/$basearch baseurl = https://mirrors.aliyun.com/mariadb/yum/11.4/rhel/$releasever/$basearch # gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB gpgkey = https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck = 1 Now install the mariaDB with its dependencies package...

Linux Commands

  Linux Commands 1.  OS-Release -cat /etc/os-release -cat /etc/redhat-release show os //kernal information -uname  show kernal middleware It is intermediator between hardware and software. -uname  -r what is process architect. -uname -p To show all information -uname -a 2.  Date-CAL -date -cal 3.  Booting in Linux (Run-Levels) Shutdown/Close pc -init 0  Single user mode -init 1 Multiple user mode -init 2 Multiple user mode with network plus full support Not use -init 4 Graphical mode init 5 Reboot the system -init 6 4.  Target command in Linux (systemctl) With the help of target we can manage system specific as well as user specific task. Target command is system Control (systemctl). Basically it is utility, which build to replace 'init' command. What systemctl can do ?  We can find its all commands with the help of single command. write systemctl enter twice TAB button. //it will list all its commands. Show current system mode - systemctl...