martes, 14 de diciembre de 2010

Descargar todas las imágenes de una web con php

Hoy quiero mostraros un script que he hecho en php el cual dada una url (que pide primero), descarga todas las imágenes (públicas) de esa página a nuestro servidor (que si lo probamos desde localhost obviamente lo descarga en nuestro PC).

El script, básicamente consiste en 2 funciones:
La 1ª almacena en un path todas las imágenes (cogiendo la etiqueta img de html).
Y la segunda crea una copia para cada imagen en nuestra máquina local, dentro de la carpeta images.

Sin más dilación os dejo el código:


<?phpfunction imagenesHTML($url, $norepetidos = true)
{
$contenido = @file($url);
$contenido = array_map("trim", $contenido);
$contenido = implode(" ", $contenido);

if (
preg_match_all('/<img([^<>]+)>/i', $contenido, $match) ) {
foreach(
$match[1] as $atributos) {
if (
preg_match('/src="([^"]+)"/i', $atributos, $matchpaths) ) {
$pathimgs[] = $matchpaths[1];
} elseif (
preg_match('/src=([^ ]+)/i', $atributos, $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, $nombre){
$size =@getimagesize ($img);
if(
$size!=''){
$ancho = $size[0];
$alto = $size[1];
$thumbnail = ImageCreateTrueColor( $ancho, $alto );
switch (
$size['mime']){
case
'image/jpeg':
$tipo=".jpg";
$src_img = ImageCreateFromJPEG( $img );
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $ancho, $alto, $size[0], $size[1] );
ImageJPEG( $thumbnail, "images/".$nombre.".jpg" );
break;
case
'image/png':
$tipo=".png";
$src_img = imagecreatefrompng( $img );
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $ancho, $alto, $size[0], $size[1] );
imagepng ( $thumbnail, "images/".$nombre.".png" );
break;
case
'image/gif':
$tipo=".gif";
$src_img = imagecreatefromgif ( $img );
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $ancho, $alto, $size[0], $size[1] );
imagegif ( $thumbnail, "images/".$nombre.".gif" );
break;
default:
ImageDestroy( $thumbnail );
return
false;
}
ImageDestroy( $thumbnail );
return
$tipo;
}
else
return
false;

}
?> <html>
<head>
<title>Captura Imagenes de una URL</title>
</head>
<body>

<h1>Capturador de Imagenes</h1>
<form action="
<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
URL: <input type="text" name="url" value=""/>
<input type="submit" name="BOTON" value="Capturar" />
</form>
<?php
if ($_POST['BOTON']=='Capturar'){
$imagenes = @imagenesHTML($_POST['url']);
if (
$imagenes!=''){
if (!
is_dir('./images')) mkdir("images");
$i=0;
foreach (
$imagenes as $imagen){
$img = split ('/', $imagen);
if (
count($img)<=1){
$url= split('/', $_POST['url']);
$ori=$imagen;
$imagen='';
$tope=count($url)-1;
for (
$j=0; $j<$tope; $j++)
$imagen.=$url[$j].'/';
$imagen.=$ori;
}
$ok = guarda_imagen ($imagen, 'imagen_'.$i);
if (
$ok==false) echo 'no se ha podido descargar la imagen: '.$imagen.'<br/>';
else{
echo
'<img src="images/imagen_'.$i.$ok.'"/><br/>';
$i++;
}
}
}
}
?> </body>
</html>

martes, 7 de diciembre de 2010

html imagen con enlace

Como poner en html una imagen que sea un enlace:

< a href="http://www.blogger.com/ruta_web">< img src="http://www.blogger.com/ruta_imagen">