Page 1 of 1
PHP - Get image data from URL and display it
Posted: 18 Feb 2015, 06:51
by PloMar
Hello
I use the following PHP code to get the image data from an external URL.
Code: Select all
$url = $_GET['url'];
$img = file_get_contents($url); // get image data from $url
The image data is stored as string in the $img variable. Now, how can I display it in the web page?
And, I want to check the image extension, if it is GIF, JPG, or PNG to display it, otherwise to return a message.
Thanks
PHP - Get image data from URL and display it
Posted: 18 Feb 2015, 06:56
by Admin
Hello
1. You can save the image into a folder in your server, using file_put_contents().
Example (the folder must have CHMOD rewrite permisions: 0777 or 0755)
Code: Select all
$url = $_GET['url'];
$allow = ['gif', 'jpg', 'png']; // allowed extensions
$img = file_get_contents($url); // get image data from $url
$url_info = pathinfo($url);
// if allowed extension
if(in_array($url_info['extension'], $allow)) {
$save_to = 'imgs/'. $url_info['basename']; // add image with the same name in 'imgs/' folder
if(file_put_contents($save_to, $img)) {
$re = '<img src="'. $save_to .'" title="'. $url_info['basename'] .'" />';
}
else $re = 'Unable to save the file';
}
else $re = 'Invalid extension: '. $url_info['extension'];
echo $re; // output $re data
2. Or, if you don't want to save the image on server, you can add and display the image in the web page using base64 data.
Example:
Code: Select all
$url = $_GET['url'];
$allow = ['gif', 'jpg', 'png']; // allowed extensions
$img = file_get_contents($url);
$url_info = pathinfo($url);
// if allowed extension
if(in_array($url_info['extension'], $allow)) {
// Format the image SRC: data:{mime};base64,{img_data_base64};
$src = 'data:image/'. $url_info['extension'] .';base64,'. base64_encode($img);
// add the base64 image into a <img> to display it
$re = '<img src="'. $src .'" alt="'. $url_info['basename'] .'" />';
}
else $re = 'Invalid extension: '. $url_info['extension'];
echo $re; // output $re data
- The method with base64 will enlarge the image data by 33%, but it's OK even if the image has a few MB.
PHP - Get image data from URL and display it
Posted: 02 Mar 2016, 00:34
by nadera
Useful code.. thank you very much. but I have one question.
how we can check : if the downloaded image exist,don't download the image again ?
PHP - Get image data from URL and display it
Posted: 02 Mar 2016, 07:06
by Admin
Hello
You can use the file_exists() function in php to check if a file exists on server. So, you can use this code to check if the image exists; if not, it will download and save it on server.
Code: Select all
$url = $_GET['url'];
$allow = ['gif', 'jpg', 'png']; //allowed extensions
$url_info = pathinfo($url);
$save_to ='imgs/'. $url_info['basename']; //set image path with the same name in 'imgs/'
$re =''; //for data to output
//if allowed extension
if(in_array($url_info['extension'], $allow)){
//if the file not exists on server, gets its data from url, and saves it
if(!file_exists($save_to)){
$img = file_get_contents($url);
if(!file_put_contents($save_to, $img)) $re ='Unable to save the file';
}
}
else $re ='Invalid extension: '. $url_info['extension'];
//if no errors ($re empty), set the html code with image to output
if($re =='') $re ='<img src="'. $save_to .'" title="'. $url_info['basename'] .'" />';
echo $re; //output $re data
PHP - Get image data from URL and display it
Posted: 02 Mar 2016, 12:52
by nadera
thanks...
another question going to be :
Images of remote server has too long names, is there any way to rename them ?
PHP - Get image data from URL and display it
Posted: 02 Mar 2016, 13:23
by Admin
How you want the images to be renamed, a random name with letters and numbers, or a number of characters from the original name?
PHP - Get image data from URL and display it
Posted: 02 Mar 2016, 13:59
by nadera
a random name with letters and numbers
PHP - Get image data from URL and display it
Posted: 02 Mar 2016, 14:19
by Admin
You can use the getRandomStr() function from this example to get a random string of 10 characters:
Code: Select all
function getRandomStr($length = 10) {
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
$save_to ='imgs/'. getRandomStr() .'.'. $url_info['extension'];
But using a name with random numbers and letters, you will not know if that image has been downloaded because it is checcked the name saved on server.
Or better, you can use substr() to get a portion of string from the original name.
Example:
Code: Select all
//get name with the first 10 chracters from original name
$save_to ='imgs/'. substr($url_info['filename'], 0, 10) .'.'. $url_info['extension'];
PHP - Get image data from URL and display it
Posted: 02 Mar 2016, 14:51
by nadera
problem solved... it works like a charm ... thank you very much dear <3