Foros del Web » Programando para Internet » PHP »

Como crear thumbnails o miniaturas.

Estas en el tema de Como crear thumbnails o miniaturas. en el foro de PHP en Foros del Web. Necesito una función php para crear thumbnails o miniaturas pero que haga lo siguiente: - Que recorte la imagen si esta en vertical es decir ...
  #1 (permalink)  
Antiguo 11/06/2011, 06:07
Avatar de DerlisRD  
Fecha de Ingreso: junio-2010
Mensajes: 60
Antigüedad: 13 años, 10 meses
Puntos: 0
Busqueda Como crear thumbnails o miniaturas.

Necesito una función php para crear thumbnails o miniaturas pero que haga lo siguiente:

- Que recorte la imagen si esta en vertical es decir por ejemplo:
que cree la miniatura 100x100 y que no se deforme sino nada mas recortándola.

Me explico ?

les dejo este link de como no quiero que quede

http://www.explota.net/2011/explota21p1/

y otro como quiero que quede habiendo fotos inclusive en vertical sin deformarla

http://saybber.com/web2009/galeria.php?id=473&tipo=gal

gracias por pasar, doy karma si responden a lo que yo necesito.
__________________
<?php print "hola mundo"; ?>

www.explota.net.
  #2 (permalink)  
Antiguo 11/06/2011, 08:33
Avatar de Naahuel  
Fecha de Ingreso: marzo-2011
Ubicación: localhost
Mensajes: 796
Antigüedad: 13 años, 1 mes
Puntos: 192
Respuesta: Como crear thumbnails o miniaturas.

Esta es una función que encontré hace un tiempo, creo que en el mismo manual de PHP. Te puse un ejemplo de cómo usarla y cómo guardar la miniatura para usar después.

Sólo soporta jpeg, pero es sencillo (digamos) modificarla para que soporte otros formatos. Acá la ves en funcionamiento:

http://nahueljose.com.ar/thumbnail/
Código PHP:
Ver original
  1. <?php
  2.  
  3. $imagen_original = 'homero.jpg';
  4. $imagen_th = 'th_' . $imagen_original;
  5. $ancho_th = 100;
  6. $alto_th = 100;
  7. $calidad = 80;
  8.  
  9. //redimensionar imagen
  10. $imagenjpeg = CroppedThumbnailJPEG($imagen_original,$ancho_th,$alto_th);
  11. //guardar en directorio actual
  12. imagejpeg($imagenjpeg,$imagen_th,$calidad);
  13. imagedestroy($imagenjpeg);
  14. //mostrar
  15. echo "<p>Imagen original: </p><img src='$imagen_original' /></p>";
  16. echo "<p>Imagen miniatura: </p><img src='$imagen_th' /></p>";
  17.  
  18.  
  19.  
  20. function CroppedThumbnailJPEG($imgSrc,$thumbnail_width,$thumbnail_height) {
  21.     //getting the image dimensions  
  22.     list($width_orig, $height_orig) = getimagesize($imgSrc);  
  23.     $myImage = imagecreatefromjpeg($imgSrc);
  24.     $ratio_orig = $width_orig/$height_orig;
  25.    
  26.     if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
  27.        $new_height = $thumbnail_width/$ratio_orig;
  28.        $new_width = $thumbnail_width;
  29.     } else {
  30.        $new_width = $thumbnail_height*$ratio_orig;
  31.        $new_height = $thumbnail_height;
  32.     }
  33.    
  34.     $x_mid = $new_width/2;  //horizontal middle
  35.     $y_mid = $new_height/2; //vertical middle
  36.    
  37.     $process = imagecreatetruecolor(round($new_width), round($new_height));
  38.    
  39.     imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
  40.     $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
  41.     imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
  42.  
  43.     imagedestroy($process);
  44.     imagedestroy($myImage);
  45.     return $thumb;
  46. }
  47. ?>
__________________
nahueljose.com.ar
  #3 (permalink)  
Antiguo 11/06/2011, 11:10
Avatar de DerlisRD  
Fecha de Ingreso: junio-2010
Mensajes: 60
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: Como crear thumbnails o miniaturas.

nooooo !!

mas claro imposible ! muchisimas gracias !
__________________
<?php print "hola mundo"; ?>

www.explota.net.
  #4 (permalink)  
Antiguo 11/06/2011, 14:49
Avatar de Naahuel  
Fecha de Ingreso: marzo-2011
Ubicación: localhost
Mensajes: 796
Antigüedad: 13 años, 1 mes
Puntos: 192
Respuesta: Como crear thumbnails o miniaturas.

Cita:
Iniciado por DerlisRD Ver Mensaje
nooooo !!

mas claro imposible ! muchisimas gracias !
De nada! Acá está una versión levemente mejorada que directamente guarda la imagen:
Crear thumbnail en PHP
__________________
nahueljose.com.ar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:05.