Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP » Extract images from a page

Extract images from a page

06/08/2011 by Dux   -   3.7/5 (3 ratings)

Getting all IMG sources and ALT from a local or remote page that returns an array.
This function uses the DOM API from PHP 5.

get_i( file ) = array( att, value )

source code



 
<?php
// PHP 5 required

function get_i($file) {
   
   
// verify file exists
   
if (!@fopen($file, "r")) return false;
   
   
// Create DOM Document
   
$ddoc = new DOMDocument();
   
$ddoc->loadHTMLFile($file);
   
$tags = $ddoc->getElementsByTagName('img');
   
   foreach (
$tags as $tag) {
       
// Get all href attribute
       
$att = $tag->getAttribute('src');
       
// Convert ALT into html entities
       
$value = htmlentities(utf8_decode($tag->getAttribute('alt')));
       
// If not empty, add to return tab
       
if (!empty($att) && !empty($value)) $return[] = array( 'att' =>  $att,'value' =>  $value);
   }
   
   return (
is_array($return)) ? $return : false;
}

// Example of use
if ($tab = get_i('http://www.mac-free.com/')) {
   foreach(
$tab as $display) echo $display['att'].' (ALT = '.$display['value'].")<br />\n";
} else {
   die(
'no image at this address');
}


?>
Rate this code     |     Copied : 97x

Comments for Extract images from a page

No comment
Add a comment Nickname
ZOOM


Javascript opacity
Adjust the rate of image colors
Dynamic image resizing according to location
Link confirmation
Simple image gallery PHP
Zoom with CSS3
Create virtual folders using htaccess
Scroll div with javascript
Automatically adjust brightness contrast of an image
Div following the scrolling of the page