PHP Ajax Image Upload Resize Crop using jQuery

Today, We want to share with you PHP Ajax Image Upload Resize Crop using jQuery.In this post we will show you ajax image upload and crop with jquery and php, hear for Image Upload,Crop and Resize Using PHP, jQuery and Ajax we will give you demo and example for implement.In this post, we will learn about Live Image Upload, Crop and Resize using jQuery and PHP MySQL with an example.

PHP Ajax Image Upload Resize Crop using jQuery

There are the Following The simple About PHP Ajax Image Upload Resize Crop using jQuery Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP laravel crop image before upload, so the php image resize on upload for this example is following below.

Step 1: Javascript With Include External CDN

Javascript Source Code

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() 
{ 

$('body').on('change','#memberprofile', function()
 {
var A=$("#memberImgStatus");
var B=$("#memberloadbtn");

$("#memberFrm").ajaxForm({target: '#flimgpreview', 
beforeSubmit:function(){
A.show();
B.hide();
}, 
success:function(){
A.hide();
B.show();
}, 
error:function(){
A.hide();
B.show();
} }).submit();
});

}); 
</script>

Step 2 : HTML Interface

index.php

<?php
include('db.php');
session_start();
$member_session_profile='1'; // User login session value
?>
<div id='flimgpreview'>
</div>
<form id="memberFrm" method="post" enctype="multipart/form-data" action='memberprofileimg.php'>
Upload image: 
<div id='memberImgStatus' style='display:none'><img src="loader.gif" alt="Profile Uploading...."/></div>
<div id='memberloadbtn'>
<input type="file" name="memberprofile" id="memberprofile" />

</div>
</form>

Step 3 : CREATE A Table

Users MySQL Table

CREATE TABLE `members` (
`uid` int(11) AUTO_INCREMENT PRIMARY KEY,
`username` varchar(255) UNIQUE KEY,
`password` varchar(100),
`email` varchar(255) UNIQUE KEY,
`profile_image` varchar(200)
)

Step 4: jquery Ajax Call To server side

memberprofileimg.php

<?php
include('db.php');
session_start();
$member_session_profile='1'; 
$path = "uploads/";

$fileimg_validtype = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
include_once 'includes/getExtension.php';
$fileorimgnm = $_FILES['memberprofile']['name'];
$size = $_FILES['memberprofile']['size'];
if(strlen($fileorimgnm))
{
$fltypeimgext = strtolower(getExtension($fileorimgnm));
if(in_array($fltypeimgext,$fileimg_validtype))
{
if($size<(1024*1024)) 
{
$orignal_upload = time().$member_session_profile.".".$fltypeimgext;
$livefileUpload = $_FILES['memberprofile']['tmp_name'];

//Re-sizing image. 
include 'includes/resizewithCompressImg.php';
$widthArray = array(200,100,50); 
foreach($widthArray as $changewidthnew)
{
$filename=resizewithCompressImg($fltypeimgext,$livefileUpload,$path,$orignal_upload,$changewidthnew);
echo "<img src='".$filename."' class='img'/>";
}

//Original Image
if(move_uploaded_file($livefileUpload, $path.$orignal_upload))
{
//Insert upload image files names into user_uploads table
mysqli_query($db,"UPDATE members SET profile_image='$orignal_upload' WHERE uid='$member_session_profile';");
echo "<img src='uploads/".$orignal_upload."' class='flimgpreview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB"; 
}
else
echo "Invalid file format.."; 
}
else
echo "Please select image..!";
exit;
}
?>

Step 5 : Serever Side Resize with Compress an Image

resizewithCompressImg.php

Simple Your Images Re-sizing image into All the different some more pixel dimensions.

<?php
function resizewithCompressImg($fltypeimgext,$livefileUpload,$path,$orignal_upload,$changewidthnew)
{

if($fltypeimgext=="jpg" || $fltypeimgext=="jpeg" )
{
$src = imagecreatefromjpeg($livefileUpload);
}
else if($fltypeimgext=="png")
{
$src = imagecreatefrompng($livefileUpload);
}
else if($fltypeimgext=="gif")
{
$src = imagecreatefromgif($livefileUpload);
}
else
{
$src = imagecreatefrombmp($livefileUpload);
}

list($width,$height)=getimagesize($livefileUpload);
$changeheightnew=($height/$width)*$changewidthnew;
$tmp=imagecreatetruecolor($changewidthnew,$changeheightnew);
imagecopyresampled($tmp,$src,0,0,0,0,$changewidthnew,$changeheightnew,$width,$height);
$filename = $path.$changewidthnew.'_'.$orignal_upload;
imagejpeg($tmp,$filename,100);
imagedestroy($tmp);
return $filename;
}
?>

getExtension.php

function getExtension($captionstr)
{
$i = strrpos($captionstr,".");
if (!$i)
{
return "";
}
$l = strlen($captionstr) - $i;
$flext = substr($captionstr,$i+1,$l);
return $flext;
}

Step 6 : Database Config File

db.php

Simple Main Global file Database config file.

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'mypass');
define('DB_DATABASE', 'YOUR_DATABASE_NAME');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Ajax Image Upload Resize Crop using jQuery.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Vue.js Back to Top Component (vue smooth scroll)
  2.   Vue.js DatePicker Source Code Example with Demo
  3.   Convert object to Array in PHP Example
  4.   Best PHP 7 Tutorial For Complete Beginners
  5.   Multiple File Upload in laravel 7 Example Tutorial
  6.   PHP Crop Resize Image while Uploading using jquery plugin
  7.   How to upload Profile Images to Users using Laravel 6?
  8.   PHP image manipulation Fits and Fill
  9.   jQuery AJAX Multiple Files Upload using PHP
  10.   Requests has() and hasFile() in Laravel 6