Ver Mensaje Individual
  #2 (permalink)  
Antiguo 29/12/2010, 08:39
Avatar de galitcin
galitcin
 
Fecha de Ingreso: septiembre-2010
Ubicación: Barcelona
Mensajes: 30
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: PHP Thumb [Ayuda]

Hola! mira si quieres puedes usar esto para hacer thumb, te hace uno igual pero en medidas que tu especifiques sin deformar...

yo lo tengo hecho en un bucle automatico ya que me hacia falta hacer todo el directorio que tenia de fotos, lo puedes sacar del bucle y te funcionaria sin problemas al cargar la foto otriginal!



Código PHP:
Ver original
  1. <?php
  2. /*EMPESAMOS HACER EL CROP*/
  3. $pathToImages = "userimgs/";
  4. $dir = opendir( $pathToImages );
  5. // bucle en busca de archivos jpg
  6. while (false !== ($source = readdir( $dir ))) {
  7.    $info = pathinfo($pathToImages . $source);
  8. // continua unicamente si la imagen es jpg
  9.  if ( strtolower($info['extension']) == 'jpg'){
  10.     echo "Creando thumbnail para {$source}<br>";
  11.  
  12. $nw = 150;
  13. $nh = 120;
  14. $stype = explode('.', $source);
  15. $stype = $stype[count($stype) -1];
  16. $stype = strtolower($stype);   
  17. $size = getimagesize("{$pathToImages}{$source}");
  18. $w = $size[0];
  19. $h = $size[1];
  20.  
  21.         switch($stype) {
  22.             case 'gif':
  23.                 $simg = imagecreatefromgif("{$pathToImages}{$source}");
  24.                 break;
  25.             case 'jpg':
  26.                 $simg = imagecreatefromjpeg("{$pathToImages}{$source}");
  27.                 break;
  28.                 case 'jpeg':
  29.                 $simg = imagecreatefromjpeg("{$pathToImages}{$source}");
  30.                 break;
  31.             case 'png':
  32.                 $simg = imagecreatefrompng("{$pathToImages}{$source}");
  33.                 break;
  34.         }
  35.    
  36.       $dimg = imagecreatetruecolor($nw, $nh);
  37.       $wm = $w/$nw;
  38.       $hm = $h/$nh;
  39.       $h_height = $nh/2;
  40.       $w_height = $nw/2;
  41.      
  42.       if($w> $h) {
  43.         $adjusted_width = $w / $hm;
  44.         $half_width = $adjusted_width / 2;
  45.         $int_width = $half_width - $w_height;
  46.         imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
  47.         } elseif(($w <$h) || ($w == $h)) {
  48.             $adjusted_height = $h / $wm;
  49.             $half_height = $adjusted_height / 2;
  50.             $int_height = $half_height - $h_height;
  51.         imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
  52.         } else {
  53.         imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
  54.       }
  55.         $dest ="thumbuserimgs/$source";
  56.        
  57.         imagejpeg($dimg,$dest,90);
  58.   }
  59. }  
  60. ////////////////////////*FIN del crop*/////////////////////////        
  61. ?>