how to get images from an url

Discussion in 'PHP' started by free-designer, Feb 25, 2010.

  1. #1
    hello everybody,

    i want a way to get all images from a passed url,

    like digg.com, when you try to submit a new post it gives you about 4 or 3 images to choose a thumb for the post,

    so i want something like this, something that i can give a limit for images to be showing and resize them, but the most important thing, that when i give a link in a input or something, it give me some images from the url.

    thanks for anyhelp
     
    free-designer, Feb 25, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    
    /* url of which you want to get images from.. */
    $url = "http://forums.digitalpoint.com/showthread.php?t=1711164";
    
    if(preg_match_all("/(<img)\s (src=\"([a-zA-Z0-9\.;:\/\?&=_|\r|\n]{1,})\")/isxmU", file_get_contents($url), $images)){
    
    /* resize image */
    $width = "120";
    
    $height = "67";
    
    /* limit number of images shown */
    $limit = 2;
    
    $i=0;
    foreach($images[3] as $image_url){
    $i++;
    if($i != $limit){
    
    if(!parse_url($image_url, PHP_URL_HOST)){
    $image_url = preg_replace("/$\//", "", $image_url);
    $image_url = "http://".parse_url($url, PHP_URL_HOST)."/".$image_url;
    }
    
    echo "<img src=\"".$image_url."\" width=\"".$width."\" height=\"".$height."\"><br>";
    }
    }
    
    } else {
    
    echo "No images found!";
    
    }
    ?>
    PHP:
     
    Last edited: Feb 25, 2010
    danx10, Feb 25, 2010 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you so mush

    it's working well but can you tell me how can i customize some extension like jpeg jpg gif and png, and there is something else the limit is not working try cnn.com it is going to give you a lot of images,
    thanks
     
    free-designer, Feb 25, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <?php
    
    /* url of which you want to get images from.. */
    $url = "http://www.cnn.com";
    
    if(preg_match_all("/(<img)\s (src=\"([a-zA-Z0-9\.;:\/\?&=_|\r|\n]{1,})\")/isxmU", file_get_contents($url), $images)){
    
    /* resize image */
    $width = "120";
    
    $height = "67";
    
    
    /* limit number of images shown */
    $limit = 3;
    
    $i=0;
    foreach($images[3] as $image_url){
    $i++;
    if($i == $limit) break;
    
    if(!parse_url($image_url, PHP_URL_HOST)){
    $image_url = preg_replace("/$\//", "", $image_url);
    $image_url = "http://".parse_url($url, PHP_URL_HOST)."/".$image_url;
    }
    
    echo "<img src=\"".$image_url."\" width=\"".$width."\" height=\"".$height."\"><br>";
    }
    
    
    } else {
    
    echo "No images found!";
    
    }
    ?>
    PHP:
    Ok, sorry, limit issue is fixed :)

    Can you ellaborate on the 'customize' the file extension part?, do you want to change the file extension or simply want to retrieve the file extension?...
     
    danx10, Feb 25, 2010 IP
  5. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i want to set it to get only jpg and png extension files
     
    free-designer, Feb 25, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    <?php
    
    /* url of which you want to get images from.. */
    $url = "http://www.cnn.com";
    
    if(preg_match_all("/(<img)\s (src=\"([a-zA-Z0-9\.;:\/\?&=_|\r|\n]{1,}\.(jpg|png))\")/isxmU", file_get_contents($url), $images)){
    
    /* resize image */
    $width = "120";
    
    $height = "67";
    
    
    /* limit number of images shown */
    $limit = 3;
    
    $i=0;
    foreach($images[3] as $image_url){
    $i++;
    if($i == $limit) break;
    
    if(!parse_url($image_url, PHP_URL_HOST)){
    $image_url = preg_replace("/$\//", "", $image_url);
    $image_url = "http://".parse_url($url, PHP_URL_HOST)."/".$image_url;
    }
    
    echo "<img src=\"".$image_url."\" width=\"".$width."\" height=\"".$height."\"><br>";
    }
    
    
    } else {
    
    echo "No images found!";
    
    }
    ?>
    PHP:
     
    danx10, Feb 25, 2010 IP
  7. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    everything is working now but im really sorry for talking so mush,

    there is a little problem

    on digg if you going to submit new post with this url villaarts.com it's going to give you two thumbs but if you type in your code villaarts.com it's going to tell you no images found!

    what do you think

    these two images are png
     
    free-designer, Feb 25, 2010 IP