How to fetch Image using PHP

Discussion in 'PHP' started by gulamkakoo, Mar 16, 2010.

  1. #1
    Hi

    I want to add some features in my new site. which fetch image and content automatically from the any url which is entered like digg. so can any one help me for that.
     
    gulamkakoo, Mar 16, 2010 IP
  2. maneetpuri

    maneetpuri Active Member

    Messages:
    152
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hi,

    On dig what happens is users share the information that gets stored in the database of dig and then shown on its website. You can do this or if you do not want to save the information in your database you can use the file and CURL functions of PHP that will read the content of a URL passed and using regular expressions fetch the details or data you need from that website.


    Cheers,

    ~Maneet
     
    maneetpuri, Mar 17, 2010 IP
  3. gulamkakoo

    gulamkakoo Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for reply

    will you pl provide me the script if you have
     
    gulamkakoo, Mar 17, 2010 IP
  4. cpaul

    cpaul Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's easy to download any webpage using curl:

    <?php
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://www.webpage.com");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec ($curl);
    curl_close ($curl);
    echo $result;
    ?>

    The output lays in $result, you can parse it further to extract any info you need.
     
    cpaul, Mar 17, 2010 IP
  5. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    or use file_get_contents() and parser the image tags
     
    guardian999, Mar 18, 2010 IP
  6. gulamkakoo

    gulamkakoo Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks for help
     
    gulamkakoo, Mar 19, 2010 IP
  7. amuthavalli

    amuthavalli Peon

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    amuthavalli, Mar 19, 2010 IP
  8. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #8
    for images (without content)

    <?php
    $sFile = "http://www.google.com/intl/en_ALL/images/logo.gif";
    $imagedata = GetFileData($sFile);
    ob_start();
    $length = strlen($imagedata);
    header('Last-Modified: '.date('r'));
    header('Accept-Ranges: bytes');
    header('Content-Length: '.$length);
    header('Content-Type: image/gif');
    print($imagedata);
    ob_end_flush();
    function GetFileData($sFilePath){
        $fp = fopen($sFilePath, 'rb') or die('404! Unable to open file!');
        $buf = '';
        while(!feof($fp)){
            $buf .= fgets($fp, 4096);
        }
        fclose($fp);
    return $buf;
    }
    ?>
    PHP:

    you can change the header to

    header('Content-Type: image/gif');

    to image/png or jpg (jpeg) etc for different pic formats :D






    ROOFIS
     
    ROOFIS, Mar 19, 2010 IP
  9. gulamkakoo

    gulamkakoo Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for help but i can't understand, how to apply in website

    if you can pl help me


     
    gulamkakoo, Mar 22, 2010 IP
  10. gulamkakoo

    gulamkakoo Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Nice coding, but what if i want to fetch images and content from other web page like digg and what should be added in mysql

    thanks for help

     
    gulamkakoo, Mar 22, 2010 IP
  11. Kenneth_Da

    Kenneth_Da Active Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #11
    You can use bellow code, it can download any files :
    function downloadFile($url, $path)
    {
      $newfname = $path;
      $file = fopen ($url, "rb");
      if ($file) {
        $newf = fopen ($newfname, "wb");
    
        if ($newf)
        while(!feof($file)) 
        {
          fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
        }
      }
    
      if ($file) 
      {
        fclose($file);
      }
    
      if ($newf) 
      {
        fclose($newf);
      }
    }
    downloadFile('http://i.dpstatic.com/icon/icon4.png','icon4.png');
    
    PHP:
     
    Kenneth_Da, Sep 9, 2012 IP