PHP - Get image data from URL and display it

Discuss coding issues, and scripts related to PHP and MySQL.
PloMar
Posts: 48

PHP - Get image data from URL and display it

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

Admin Posts: 805
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.

nadera Posts: 4
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 ?

Admin Posts: 805
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 

nadera Posts: 4
thanks...
another question going to be :
Images of remote server has too long names, is there any way to rename them ?

Admin Posts: 805
How you want the images to be renamed, a random name with letters and numbers, or a number of characters from the original name?

nadera Posts: 4
a random name with letters and numbers

Admin Posts: 805
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']; 

nadera Posts: 4
problem solved... it works like a charm ... thank you very much dear <3

Similar Topics