Image Clip based on URL

Discussion in 'PHP' started by ridesign, Sep 4, 2008.

  1. #1
    I want to create a script where you enter a url and it gets the images from the url like in facebook, and you can choose which image you want.
     
    ridesign, Sep 4, 2008 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    I don't use facebook, so I don't know how it is done there, but anyway - what's your question?
    Do you need to extracrt all image urls from that page or you don't know how to download image from remote host or what?
     
    AsHinE, Sep 4, 2008 IP
  3. ridesign

    ridesign Peon

    Messages:
    294
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want to extract the links to all the images on a page, after I enter the url of a page.
    For example if I enter: http://www.google.com I want the script to extract all of the images on the page, and when when I click on an image, it lets me choose that image url as one of the text fields on the page.
     
    ridesign, Sep 4, 2008 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Try this. This is for 1 image, you can download as many as needed. The array below will be lost once page changes, so save to database, or some file instead. :) You can also get the image extension by exploding the url. I just typed .jpg

    <?php
    $a= array();
    if(isset($_POST['submit'])){
    $u= $_POST['u'];
    //check here if it's a valid url, then download
    $c= file_get_contents($u);
    $name= md5(time()). '.jpg';
    $a[]=$name;
    echo '<h1> Select image below </h1>';
    foreach($a as $v){
    echo '<a href="select.php?f='. $v. '"><img src="'. $v. '"></a>';
    }

    }
    print '
    <form action="script.php" method="post">
    URL: <input type="text" name="u">
    <input type="submit" name="submit" value=" get now">
    </form>
    ';
    ?>
     
    JEET, Sep 4, 2008 IP
  5. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #5
    Well,
    
    $page = file_get_contents($_GET['url']);
    
    PHP:
    will load remote page, passed as url param into $page variable
    preg_match_all('/<img.+?src="(.+?)"/', $page, $result, PREG_PATTERN_ORDER);
    $result = $result[1];
    PHP:
    will extract all image urls in $result variable
    Note, that image url may be a relative - so you have to prepend it with hostname or maybe path.
    That's genereally the beginnning.
    You make full image path and display these image. A little bit of javascript like
    <img onclick="showPath('');/>
    HTML:
    and its ready :)
     
    AsHinE, Sep 4, 2008 IP
  6. ridesign

    ridesign Peon

    Messages:
    294
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks,
    I have got the images path on the page, how do I make the script run after the url is typed in do I use javascript and something like onfocus?
     
    ridesign, Sep 5, 2008 IP
  7. ridesign

    ridesign Peon

    Messages:
    294
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Is there a way to sort the images e.g. based on filesize?
    I am not sure how facebook do it, but it only brings up 4 big images from the site, instead of all of them.
     
    ridesign, Sep 5, 2008 IP