Friday, January 7, 2011

Does San Diego Zoo Sale Beer



Today I show you a script I've done in php which given a url (asking first), download all images (public) of that page to our server (if we test from localhost obviously download our PC). The 1 st stored in a path all the images (taking the html img tag.)
The second creates a copy for each image in the local machine within the images folder. Without further ado I leave the code:

\u0026lt;? Php function
imagenesHTML
(
$ url, $ norepetidos



= true



)
{$ content = @
file ( $ url); $ content = array_map (

"trim"
, $ content); $ content = implode
( " , $ content); if (preg_match_all (
'/ \u0026lt;img ([^\u0026lt;>]+)>/ i' , $ content, $ match )) { foreach ($ match [

1] as $ attribute ) {if ( preg_match ( '/ src ="([^"]+)"/ i' ,
$ attributes, $ matchpaths )) {$ pathimgs [] = $
matchpaths
[ 1]; } elseif (preg_match ( '/ src = ([^] +) / i' , $ attributes
, $ matchpaths )) {$ pathimgs [] =
$matchpaths
[ 1 ]; } unset( $matchpaths ); } } if ( !empty(
$pathimgs
) ) { if ( $norepetidos ) { return array_unique (

$pathimgs
); } else { return

$pathimgs
; } } else { return
false ; }
}
function guarda_imagen ($ img
,
$ name) {$ size


= @
getimagesize ($ img

) if ($ size ! = '') {$ width
= $ size [0 ]
$ high = $ size [
1]; $ thumbnail = imagecreatetruecolor ($ width
, $ high ); switch ($ size [
'mime'
]) { almost 'image / jpeg ' : $ type = . "jpg" ;
$ src_img = imagecreatefromjpeg ($ img
);
imagecopyresampled ( $ thumbnail ,
$ src_img
, 0 , 0 , 0
, 0 , $ width, $ high , $ size [0 ] $ size [ 1]); imagejpeg ($ thumbnail , "images /" . $ name. . "jpg" ) break; almost
'image / png' : $ type = . "png" ; $ src_img =
imagecreatefromjpeg ($ img
);
imagecopyresampled ( $ thumbnail, $
src_img
, 0, 0 ,
0, 0 , $ width , $ high, $ size [ 0], $ size [ 1]); imagepng ($ thumbnail , "images /" . $ name. . "png" ) break;
case 'image / gif' : $ type = . "gif" ; $ src_img = imagecreatefromgif
(
$ img);
imagecopyresampled ($ thumbnail ,
$ src_img
, 0, 0 ,
0, 0 , $ width, $ high , $ size [0 ] $ size [ 1]); imagegif ($ thumbnail , "images /" . $ name. . "Gif" ) break; default:
ImageDestroy ( $ thumbnail) return false;} ImageDestroy (


$ thumbnail) return
$ type;} else return false
;}

?> \u0026lt;html> \u0026lt;head> \u0026lt;title> capture images from a URL \u0026lt;/ title> \u0026lt;/ head>
\u0026lt; ; body>
\u0026lt;h1> Image Capture \u0026lt;/ h1> \u0026lt;form action = "
\u0026lt;?

php echo $ _SERVER
[

'PHP_SELF'
]
?>
"method =" post ">
URL: \u0026lt;input type="text" name="url"
value=""/> \u0026lt;input type="submit" name="BOTON"
value="Capturar" /> \u0026lt;/ form> ;


\u0026lt;?
php if ($ _POST [ 'BUTTON' ] == 'Capture' ) {


$ images = @
imagenesHTML
(
$ _POST [ 'url' ]); if ($ images ! =
'') {if (! is_dir ( '. / Images' )) mkdir (
"images"
) $ i =
0
; foreach ($ images as $ image ) {$ img = split
( '/' ,
$ image); if ( count ($ img
) \u0026lt;= 1) {$ url = split (
'/'
, $ _POST [ 'url' ]);
$ ori $ image = ; $ image ='' ; $ cap =
count ( $ url) -
1
; for ($ j =
0
; $ j \u0026lt;$ limit ; $ j + +)
$ image .= $ url [ $ j]. '/' ; $ image .= $ ori
; } $ ok = guarda_imagen ($ image ,
'imagen_' . $ i); if (

$ ok == false ) echo ' Failed to download image: ' . $ image. '\u0026lt;br/>' ; else {echo
'\u0026lt;img src = "images / imagen_' . $ i . $ ok . '"/>\u0026lt; br /> ' ; $ i + +;}

}}} ?> \u0026lt;/ body> \u0026lt;/ html>

0 comments:

Post a Comment