Here is how exactly you can do it :
Take a look at link :
Paste Event Based Image Uploader Script : http://huntedhunter.com/uploadimage/
I just wrote this a little while ago..!
How It Works :
Just right when someone paste an HTTP site image link in the input field so the image url is posted to upload.php
through ajax post request file and the upload.php
file downloads the image and save it on local server and then then new image url is received in ajax response to the current page and hence you can do anything with the response..!
Here is the code :
HTML :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript">
$( document ).ready(function() {
$('#insert').on('input', function(e) {
var $val = $('#insert').val();
$.ajax({
url: 'upload.php',
type: 'POST',
data: { imageurl: $val} ,
success: function (response) {
$('#newimageurl').val(response);
$('#showimage').attr('src', response);
},
error: function () {
//your error code
}
});
});
});
</script>
<title></title>
</head>
<body>
<form class="form-horizontal" style="padding: 10%">
<div class="form-group">
<label class="control-label col-sm-2" for="ImageURL">Insert Image: </label>
<div class="col-sm-10">
<input class="form-control" id="insert" placeholder="Enter ImageURL" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="ImageURL">Your New Image URL:</label>
<div class="col-sm-10">
<input class="form-control" id="newimageurl" placeholder="Enter ImageURL" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="ImageURL">Your New Image:</label>
<div class="col-sm-10"><img id="showimage" src=""></div>
</div>
</form>
</body>
</html>
PHP : (upload.php)
<?php
$image_name = basename($_POST["imageurl"]);
$ch = curl_init($_POST["imageurl"]);
$fp = fopen("tmp/$image_name", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/', $url);
$dir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++)
{
$dir.= $parts[$i] . "/";
}
echo "http://" . $dir . "tmp/" . $image_name;
?>