elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Entrar al Canal Oficial Telegram de elhacker.net


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  como hacer tumbs de imagenes en php
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: como hacer tumbs de imagenes en php  (Leído 4,169 veces)
antoni_anemi

Desconectado Desconectado

Mensajes: 4


Ver Perfil
como hacer tumbs de imagenes en php
« en: 15 Julio 2012, 17:37 pm »

NECESITO AYUDA ESTOY HACIENDO UN PROGRAMA EN PHP QUE CREE IMAGENES TUMBS Y QUE LOS GUARDE EN UN DIRECTORIO ME HACE TODO ME CREA EL DIRECTORIO Y LA IMAGEN TUMBS PERO SOLO ME CREA UNA IMAGEN EN NEGRO NO ME COPIA LA IMAGEN ORIGINAL ESTE ES MI CODIGO OJALA ME PUEDAN AYUDAR



<?php
/**
 * @author antonio de jesus cortes lagunes
 * @copyright 2012
 * este es puro codigo php si desea verlo en el explorador tienes que meterlo en codigo html y
 * ademas debes tener instalado php para que pueda ser interpretado.
 *
 * Ejercicio 4.10: ídem al anterior, pero que si no existe la miniatura de una foto debe de crearla.
 * VERSION PARA WINDOWS
 *
 */

echo "<h1>Galeria de imagenes con thumbnails</h1>";



function png($foto){
        if (!is_dir('fotos/tumbs')){
        mkdir ('fotos/tumbs', 0777);
    }
    header('Content-Type: image/gif');
$fuente = @imagecreatefrompng($foto);
$imgAncho = imagesx ($fuente);
$imgAlto =imagesy($fuente);
$imagen = imagecreatetruecolor(50,50);

imagecopyresampled($imagen,$fuente,0,0,0,0,50,50,$imgAncho,$imgAlto);


imageGif($imagen,"fotos/tumbs/MINI-$foto");
   
}



function jpg($foto){
    if (!is_dir('fotos/tumbs')){
        mkdir ('fotos/tumbs', 0777);
    }
header('Content-Type: image/jpeg');
$fuente = @imagecreatefromjpeg($foto);
$imgAncho = imagesx($fuente);
$imgAlto =imagesy($fuente);
$imagen = imagecreatetruecolor(50,50);

imagecopyresampled($imagen,$fuente,0,0,0,0,100,100,$imgAncho,$imgAlto);



imagejpeg($imagen,"fotos/tumbs/MINI-$foto");
   
}

function gif($foto){
        if (!is_dir('fotos/tumbs')){
        mkdir ('fotos/tumbs', 0777);
    }
    header('Content-Type: image/gif');
$fuente = @imagecreatefromgif($foto);
$imgAncho = imagesx ($fuente);
$imgAlto =imagesy($fuente);
$imagen = imagecreatetruecolor(50,50);

imagecopyresampled($imagen,$fuente,0,0,0,0,50,50,$imgAncho,$imgAlto);


imageGif($imagen,"fotos/tumbs/MINI-$foto");
   
}




function valida_foto($fotos)
{
$rdo=0;
if (ereg("[Jj][Pp][Gg]$", $fotos)) {
    $rdo=1;
    jpg($fotos);
}
if (ereg("[Gg][Ii][Ff]$", $fotos)) {
    $rdo=1;
    gif($fotos);
}
if (ereg("[Pp][Nn][Gg]$", $fotos)){
    $rdo=1;
    png($fotos);
}
if (ereg("[Bb][Mm][Pp]$", $fotos)) {
    $rdo=1;
    bmp($fotos);
}
return $rdo;
}




echo "<table border=1>";
$puntero = opendir('fotos');
$i=1;
while (false !== ($foto = readdir($puntero)))
{
if ($foto!="." && $foto!=".." && valida_foto($foto))
{

if ($i==1)
echo "<tr>";
echo "<td><a href='fotos/tumbs/MINI-$foto'>";
echo "<img src='fotos/$foto' width=100 height=100></img>";

echo "</a></td>";
if ($i==4)
{echo "</tr>"; $i=0;}
$i++;
}
}
closedir($puntero);
echo "</table>";



?>


En línea

#!drvy
Moderador
***
Desconectado Desconectado

Mensajes: 5.850



Ver Perfil WWW
Re: como hacer tumbs de imagenes en php
« Respuesta #1 en: 17 Julio 2012, 22:51 pm »

Hola

Para empezar.... no uses ereg. Es una función marcada como obsoleta a partir de PHP 5.3. Te recomiendo que uses preg_match.
Total, POSIX ERE y PCRE son muy similares.. veras que es fácil.

Otro problema que tienes, es que defines los headers. No hace falta definir los. Y es que, imprimes un echo antes de definir el header (incluso aunque lo hagas después estaría mal...) y eso entra en conflicto pues el navegador primero mira el header y luego el contenido.



El problema principal es que, en las funciones (jpg,bmp,gif,png).. no indicas donde se encuentra la foto original. Solo indicas su nombre. Por tanto, al intentar abrir la foto, PHP busca en el directorio donde esta alojado el script y no en la carpeta fotos.
Código
  1. function jpg($foto){
  2.    if (!is_dir('fotos/tumbs')){mkdir ('fotos/tumbs', 0777);}
  3.    $fuente = imagecreatefromjpeg($foto);
  4.    $imgAncho = imagesx($fuente);
  5.    $imgAlto =imagesy($fuente);
  6.    $imagen = imagecreatetruecolor(50,50);
  7.    imagecopyresampled($imagen,$fuente,0,0,0,0,100,100,$imgAncho,$imgAlto);
  8.    imagejpeg($imagen,"fotos/tumbs/MINI-$foto");
  9. }

Te dejo a continuación el código funcional.
Código
  1. <?php
  2. /**
  3.  * @author antonio de jesus cortes lagunes
  4.  * @copyright 2012
  5.  * este es puro codigo php si desea verlo en el explorador tienes que meterlo en codigo html y
  6.  * ademas debes tener instalado php para que pueda ser interpretado.
  7.  *
  8.  * Ejercicio 4.10: ídem al anterior, pero que si no existe la miniatura de una foto debe de crearla.
  9.  * VERSION PARA WINDOWS
  10.  *
  11.  */
  12.  
  13. echo "<h1>Galeria de imagenes con thumbnails</h1>";
  14.  
  15. function png($foto){
  16.    if (!is_dir('fotos/tumbs')){mkdir ('fotos/tumbs', 0777);}
  17.    $fuente = @imagecreatefrompng('fotos/'.$foto);
  18.    $imgAncho = imagesx ($fuente);
  19.    $imgAlto =imagesy($fuente);
  20.    $imagen = imagecreatetruecolor(50,50);
  21.    imagecopyresampled($imagen,$fuente,0,0,0,0,50,50,$imgAncho,$imgAlto);
  22.    imageGif($imagen,"fotos/tumbs/MINI-$foto");
  23. }
  24.  
  25. function jpg($foto){
  26.    if (!is_dir('fotos/tumbs')){mkdir ('fotos/tumbs', 0777);}
  27.    $fuente = imagecreatefromjpeg('fotos/'.$foto);
  28.    $imgAncho = imagesx($fuente);
  29.    $imgAlto =imagesy($fuente);
  30.    $imagen = imagecreatetruecolor(50,50);
  31.    imagecopyresampled($imagen,$fuente,0,0,0,0,100,100,$imgAncho,$imgAlto);
  32.    imagejpeg($imagen,"fotos/tumbs/MINI-$foto");
  33. }
  34.  
  35. function gif($foto){
  36.    if (!is_dir('fotos/tumbs')){mkdir ('fotos/tumbs', 0777);}
  37.    $fuente = @imagecreatefromgif('fotos/'.$foto);
  38.    $imgAncho = imagesx ($fuente);
  39.    $imgAlto =imagesy($fuente);
  40.    $imagen = imagecreatetruecolor(50,50);
  41.    imagecopyresampled($imagen,$fuente,0,0,0,0,50,50,$imgAncho,$imgAlto);
  42.    imageGif($imagen,"fotos/tumbs/MINI-$foto");
  43. }
  44.  
  45. function valida_foto($fotos){
  46.    $rdo=0;
  47.    if (preg_match("/[Jj][Pp][Gg]/", $fotos)){
  48.        $rdo=1;
  49.        jpg($fotos);
  50.    }
  51.    if (preg_match("/[Gg][Ii][Ff]/", $fotos)){
  52.        $rdo=1;
  53.        gif($fotos);
  54.    }
  55.    if (preg_match("/[Pp][Nn][Gg]/", $fotos)){
  56.        $rdo=1;
  57.        png($fotos);
  58.    }
  59.    if (preg_match("/[Bb][Mm][Pp]/", $fotos)){
  60.        $rdo=1;
  61.        bmp($fotos);
  62.    }
  63.    return $rdo;
  64. }
  65.  
  66. echo "<table border=1>";
  67. $puntero = opendir('fotos');
  68. $i=1;
  69. while (false !== ($foto = readdir($puntero))){
  70.    if ($foto!="." && $foto!=".." && valida_foto($foto)){
  71.        if ($i==1){
  72.            echo "<tr>";
  73.            echo "<td><a href='fotos/tumbs/MINI-$foto'>";
  74.            echo "<img src='fotos/$foto' width=100 height=100></img>";
  75.            echo "</a></td>";
  76.            if ($i==4){echo "</tr>"; $i=0;}
  77.            $i++;
  78.        }
  79.    }
  80. }
  81. closedir($puntero);
  82. echo "</table>";
  83.  
  84. ?>

Saludos


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines