Wednesday, 01 Nov, 2023 -553

Summernote image upload

Javascript – Summernote image upload

I have a problem with editor Summernote. I want to upload images into a catalog on the server.
I have some script:
I tested this code and Works Javascript
   
$(function () {
    // Summernote
    var IMAGE_PATH = 'https://alaminsobuj.net/themeorigin-news/';
    $('#editor1').summernote({
       callbacks: {
	        onImageUpload: function(image) {
	            uploadImage(image[0]);
	        },
	        onMediaDelete : function(target) {
	            deleteImage(target[0].src);
	        }
		}
    })
    
   function uploadImage(image) {
	   var data = new FormData();
			    data.append("image", image);
			     //var CSRF_TOKEN = $("#CSRF_TOKEN").val();
			     //site_url
			    $.ajax({
			        url: "",
			        cache: false,
			        contentType: false,
			        processData: false,
			        data: data,
			        type: "POST",
			        success: function(url) {
    			     var image = IMAGE_PATH + url;
    		         $('#editor1').summernote("insertImage", image )
			        },
			        error: function(data) {
			            console.log(data);
			        }
			    });
			}

			function deleteImage(src) {
			 
			    $.ajax({
			        data: {src : src},
			        type: "POST",
			        url: "",
			        cache: false,
			        success: function(response) {
			            console.log(response);
			        }
			    });
			}		 
  })




#codeigniter code 

	function upload_image(){
        if (@$_FILES['image']['name']) {

          $config['upload_path']   = './assets/uploads/news/';
          $config['allowed_types'] = 'gif|jpg|jpeg|png';
          $config['overwrite']     = false;
          $config['max_size']      = 1024;
          $config['remove_spaces'] = true;
          $config['max_filename']   = 10;
          $config['file_ext_tolower'] = true;

          $this->load->library('upload', $config);

          if ( ! $this->upload->do_upload('image'))
          {
            	$this->upload->display_errors();
				return FALSE;
          } else {
          
           $data = $this->upload->data();
           $image = $config['upload_path'].$data['file_name'];
            #------------resize image------------#
            $this->load->library('image_lib');
            $config['image_library'] = 'gd2';
            $config['source_image'] = $config['upload_path'].$data['file_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            // $config['width']     = *;
            // $config['height']   = *;

            $this->image_lib->clear();
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            
            echo  $image;
            #-------------resize image----------#
          }

      }



	}
	
function delete_image(){
	     $src = $this->input->post('src');
    	$file_name = str_replace(base_url('/assets/uploads/news/'), '', $src);
    	
       $img_path = FCPATH .'/assets/uploads/news/'. $file_name;
        unlink($img_path);
        
		if(unlink($img_path)){
	        echo 'File Delete Successfully';
	    }
}

#PHP CODE
  if ($_FILES['image']['name']) {
  if (!$_FILES['image']['error']) {
    $name = md5(rand(100, 200));
    $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    $filename = $name.'.'.$ext;
    $destination = '/assets/images/'.$filename; //change this directory
    $location = $_FILES["file"]["tmp_name"];
    move_uploaded_file($location, $destination);
    echo 'http://test.yourdomain.al/images/'.$filename; //change this URL
  } else {
    echo $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['file']['error'];
  }
} 
  
Tags:
Tags
Most Popular
img
How to install WordPress
02 Apr, 2024 view: 1958
img
Xampp Tutorial Create Your Own Local Test Server
02 Apr, 2024 view: 885
img
How to Install Laravel
02 Apr, 2024 view: 819
img
PHP is not recognized as an internal or external command
02 Apr, 2024 view: 610
img
How to Install Composer on Windows
01 Apr, 2024 view: 608
img
Summernote image upload
01 Nov, 2023 view: 553
img
How to clear select2 selected value in js
01 Nov, 2023 view: 426
img
Zipping and Unzipping Files in Linux
02 Apr, 2024 view: 376
img
How to add Datepicker in Bootstrap
26 Apr, 2024 view: 372
img
How to Import a MySQL Database using Command Line
02 Apr, 2024 view: 366
img
How to Installation Laragon
01 Apr, 2024 view: 213
img
How to Installing an SSL certificate on your server using cPanel
06 Apr, 2024 view: 184
img
Paypal Payment Gateway Integration With Laravel
06 Apr, 2024 view: 153
img
How to Use Laravel Middleware to Protect Your Application
09 Apr, 2024 view: 145
img
Laravel Cache Clear Command Php Artisan Optimize
09 Apr, 2024 view: 144
img
why important website development learn
10 Apr, 2024 view: 136
img
How to Integrate Stripe Payment in Laravel
06 Apr, 2024 view: 134
img
Laravel Authentication Tutorial
09 Apr, 2024 view: 133
img
laravel qr code generator package
18 Apr, 2024 view: 132
Trending