Zend - The PHP Company




Graphics

Add Code


Function to grab an image  

Type: code fragment
Added by: pris
Entered: 10/05/2002
Last modified: 05/12/2001
Rating: - (fewer than 3 votes)
Views: 5736
This function allows you to grab an image from a remote host and write it into a new file in the local host.


<?php

// May-2002
// pixie@nervesane.net
//
// Function: grab an image from a remote host and write it
// into a new file in the local host
//
// Make sure you set the correct permissions for 
// file writing.
//
// ARGUMENTS TO PASS:
// $url is the URL (quite obvious). If empty function will
// return false.
// $filename is an optional argument: if empty, filename 
// will be crated based on date and time.
// Use at your own risk.

function GrabImage($url,$filename="") {
  if(
$url==""):return false;endif;

  if(
$filename=="") {
    
$ext=strrchr($url,".");
    if(
$ext!=".gif" && $ext!=".jpg"):return false;endif;
    
$filename=date("dMYHis").$ext;
  }

  
ob_start();
  
readfile($url);
  
$img ob_get_contents();
  
ob_end_clean();
  
$size strlen($img);

  
$fp2=@fopen($filename"a");
  
fwrite($fp2,$img);
  
fclose($fp2);

  return 
$filename;
}

?>


Usage Example


$img=GrabImage("http://news.bbc.co.uk/olmedia/1975000/images/_1978837_detector_ap100.jpg","");
if($img):echo '<pre><img src="'.$img.'"></pre>';else:echo "false";endif;


Rate This Script





Search



This Category All Categories