Wednesday, 01 Nov, 2023 -495

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: 1889
img
Xampp Tutorial Create Your Own Local Test Server
02 Apr, 2024 view: 770
img
How to Install Laravel
02 Apr, 2024 view: 759
img
PHP is not recognized as an internal or external command
02 Apr, 2024 view: 558
img
How to Install Composer on Windows
01 Apr, 2024 view: 543
img
Summernote image upload
01 Nov, 2023 view: 495
img
How to clear select2 selected value in js
01 Nov, 2023 view: 377
img
Zipping and Unzipping Files in Linux
02 Apr, 2024 view: 343
img
How to add Datepicker in Bootstrap
26 Apr, 2024 view: 336
img
How to Import a MySQL Database using Command Line
02 Apr, 2024 view: 327
img
How to Installation Laragon
01 Apr, 2024 view: 179
img
How to Installing an SSL certificate on your server using cPanel
06 Apr, 2024 view: 147
img
Paypal Payment Gateway Integration With Laravel
06 Apr, 2024 view: 120
img
How to Use Laravel Middleware to Protect Your Application
09 Apr, 2024 view: 106
img
Laravel Cache Clear Command Php Artisan Optimize
09 Apr, 2024 view: 106
img
why important website development learn
10 Apr, 2024 view: 104
img
Laravel Authentication Tutorial
09 Apr, 2024 view: 103
img
How to Integrate Stripe Payment in Laravel
06 Apr, 2024 view: 101
img
laravel qr code generator package
18 Apr, 2024 view: 94
Trending