today I will show the Crop image before upload using jQuery croppie with ajax. I will show crop and resize image before uploading to database in Codeigniter.
In this tutorial, we will learn crop image in PHP. this tutorial uses jQuery for image upload with ajax in Codeigniter 4 project. image show a preview of crop and resize image before save in database using Ajax. And without refreshing and reloading web page of your CodeIgniter project.
<?php namespace App\Controllers;
use CodeIgniter\Controller;
class CropImageUpload extends Controller
{
public function index()
{
return view('crop-image-upload-form');
}
public function store()
{
helper(['form', 'url']);
$db = \Config\Database::connect();
$builder = $db->table('crop_images');
$data = $_POST["image"];
$image_array_1 = explode(";", $data);
$image_array_2 = explode(",", $image_array_1[1]);
$data = base64_decode($image_array_2[1]);
$imageName = time() . '.png';
file_put_contents($imageName, $data);
$image_file = addslashes(file_get_contents($imageName));
$save = $builder->insert(['title' => $image_file]);
$response = [
'success' => true,
'data' => $save,
'msg' => "Crop Image has been uploaded successfully in codeigniter"
];
return $this->response->setJSON($response);
}
}
Top comments (0)