Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/01/2010, 09:52
xfer2
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Pregunta una consulta para redimencionar imagenes

buenas a todos. una consulta. estuve investigando en google y en foros del web como redimensionar imagenes. vi unos ejemplos en foros del web y uno que hice por mismo, de como se redimensiona las imagenes.

supongamos que tengo una imagen de 1280 x 1240 px y quiero redimensionarlo a 500 x 500 px. subo el archivo y el codigo que hice me lo redimensiona. pero me di con la sorpresa que cuando lo he asignado los tamaños correspondientes, los redimensiona a tamaños distintos.

esta es una clase para la redimension de las imagenes. lo saque de la pagina de phpclasses.org

Código PHP:
Ver original
  1. <?php
  2. ##############################################
  3. # Shiege Iseng Resize Class
  4. # 11 March 2003
  5. # shiegege_at_yahoo.com
  6. # View Demo :
  7. #   http://shiege.com/scripts/thumbnail/
  8. /*############################################
  9. Sample :
  10. $thumb=new thumbnail("./shiegege.jpg");         // generate image_file, set filename to resize
  11. $thumb->size_width(100);                // set width for thumbnail, or
  12. $thumb->size_height(300);               // set height for thumbnail, or
  13. $thumb->size_auto(200);                 // set the biggest width or height for thumbnail
  14. $thumb->jpeg_quality(75);               // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
  15. $thumb->show();                     // show your thumbnail
  16. $thumb->save("./huhu.jpg");             // save your thumbnail to file
  17. ----------------------------------------------
  18. Note :
  19. - GD must Enabled
  20. - Autodetect file extension (.jpg/jpeg, .png, .gif, .wbmp)
  21.   but some server can't generate .gif / .wbmp file types
  22. - If your GD not support 'ImageCreateTrueColor' function,
  23.   change one line from 'ImageCreateTrueColor' to 'ImageCreate'
  24.   (the position in 'show' and 'save' function)
  25. */############################################
  26.  
  27.  
  28. class thumbnail
  29. {
  30.     var $img;
  31.  
  32.     function thumbnail($imgfile)
  33.     {
  34.         //detect image format
  35.         $this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
  36.         $this->img["format"]=strtoupper($this->img["format"]);
  37.         if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
  38.             //JPEG
  39.             $this->img["format"]="JPEG";
  40.             $this->img["src"] = ImageCreateFromJPEG ($imgfile);
  41.         } elseif ($this->img["format"]=="PNG") {
  42.             //PNG
  43.             $this->img["format"]="PNG";
  44.             $this->img["src"] = ImageCreateFromPNG ($imgfile);
  45.         } elseif ($this->img["format"]=="GIF") {
  46.             //GIF
  47.             $this->img["format"]="GIF";
  48.             $this->img["src"] = ImageCreateFromGIF ($imgfile);
  49.         } elseif ($this->img["format"]=="WBMP") {
  50.             //WBMP
  51.             $this->img["format"]="WBMP";
  52.             $this->img["src"] = ImageCreateFromWBMP ($imgfile);
  53.         } else {
  54.             //DEFAULT
  55.             echo "Not Supported File";
  56.             exit();
  57.         }
  58.         @$this->img["lebar"] = imagesx($this->img["src"]);
  59.         @$this->img["tinggi"] = imagesy($this->img["src"]);
  60.         //default quality jpeg
  61.         $this->img["quality"]=75;
  62.     }
  63.  
  64.     function size_height($size=100)
  65.     {
  66.         //height
  67.         $this->img["tinggi_thumb"]=$size;
  68.         @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
  69.     }
  70.  
  71.     function size_width($size=100)
  72.     {
  73.         //width
  74.         $this->img["lebar_thumb"]=$size;
  75.         @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
  76.     }
  77.  
  78.     function size_auto($size=100)
  79.     {
  80.         //size
  81.         if ($this->img["lebar"]>=$this->img["tinggi"]) {
  82.             $this->img["lebar_thumb"]=$size;
  83.             @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
  84.         } else {
  85.             $this->img["tinggi_thumb"]=$size;
  86.             @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
  87.         }
  88.     }
  89.  
  90.     function jpeg_quality($quality=75)
  91.     {
  92.         //jpeg quality
  93.         $this->img["quality"]=$quality;
  94.     }
  95.  
  96.     function show()
  97.     {
  98.         //show thumb
  99.         @Header("Content-Type: image/".$this->img["format"]);
  100.  
  101.         /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
  102.         $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
  103.             @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);
  104.  
  105.         if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
  106.             //JPEG
  107.             imageJPEG($this->img["des"],"",$this->img["quality"]);
  108.         } elseif ($this->img["format"]=="PNG") {
  109.             //PNG
  110.             imagePNG($this->img["des"]);
  111.         } elseif ($this->img["format"]=="GIF") {
  112.             //GIF
  113.             imageGIF($this->img["des"]);
  114.         } elseif ($this->img["format"]=="WBMP") {
  115.             //WBMP
  116.             imageWBMP($this->img["des"]);
  117.         }
  118.     }
  119.  
  120.     function save($save="")
  121.     {
  122.         //save thumb
  123.         if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]);
  124.         /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
  125.         $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
  126.             @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);
  127.  
  128.         if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
  129.             //JPEG
  130.             imageJPEG($this->img["des"],"$save",$this->img["quality"]);
  131.         } elseif ($this->img["format"]=="PNG") {
  132.             //PNG
  133.             imagePNG($this->img["des"],"$save");
  134.         } elseif ($this->img["format"]=="GIF") {
  135.             //GIF
  136.             imageGIF($this->img["des"],"$save");
  137.         } elseif ($this->img["format"]=="WBMP") {
  138.             //WBMP
  139.             imageWBMP($this->img["des"],"$save");
  140.         }
  141.     }
  142. }
  143. ?>

el codigo que redimensiona la imagen.

Código PHP:
Ver original
  1. <?php
  2.  
  3.     include("includes/resize.php");
  4.    
  5.     $imagen = $_FILES['imagen']['name'];
  6.     $uploadtempname = $_FILES['imagen']['tmp_name'];
  7.     $nuevo_imagen = str_replace(' ','_',$imagen);
  8.    
  9.         $test = explode(".",$imagen);
  10.         if(strtoupper($test[1])=="JPG" || strtoupper($test[1])=="JPEG" || strtoupper($test[1])=="GIF" || strtoupper($test[1])=="PNG"){
  11.    
  12.             #video de la noticia.          
  13.             $tamano = $_FILES['imagen']['size']; // Leemos el tamaño del fichero
  14.             $tamano_max = "4194304"; // Tamaño maximo permitido                       
  15.             if($tamano <= $tamano_max){ // Comprovamos el tamaño*/
  16.                    
  17.                 $path = "imagenes/";
  18.                 $path_thumbnail = "thumbs/";
  19.                 move_uploaded_file($uploadtempname,$path.$nuevo_imagen);
  20.                 $thumb=new thumbnail($path.$nuevo_imagen); // prepare to generate "shiegege.jpg" in directory "/www/imagenes"
  21.                 $thumb->size_width(500);           // set width for thumbnail with 500 pixels
  22.                 $thumb->size_height(400);          // set the biggest width or height for thumbnail
  23.                 $thumb->jpeg_quality(75);                // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
  24.                 $thumb->save($path_thumbnail."thb_".$nuevo_imagen);       // save my  thumbnail to file "huhu.jpg" in directory "/www/thumbs               
  25.                
  26.                 echo "El archivo ".$nuevo_archivo." se ha subido corectamente.";
  27.                
  28.             }else{
  29.                 echo "<span align = 'center'>El archivo que intenta grabar, excede el peso requerido. El máximo de peso es 4MB.</span>";
  30.                 echo "<br /><br />";
  31.                 echo "<a href='javascript:history.back()'>Clic aqui para regresar </a>";
  32.             }
  33.            
  34.         }else{
  35.             echo "<span align = 'center'>El archivo que intenta grabar, debe ser de formato jpg, jpeg, gif y png</span>";
  36.             echo "<br /><br />";
  37.             echo "<a href='javascript:history.back()'>Clic aqui para regresar </a>";
  38.        
  39.         }      
  40.            
  41.    
  42. ?>

mi duda es la siguiente:

1. en la clase que instancia que coloco los tamaños correspondientes.

Código PHP:
Ver original
  1. $thumb->size_width(500);           // set width for thumbnail with 500 pixels
  2. $thumb->size_height(400);          // set the biggest width or height for thumbnail

no tendria que redimensionar y mostrar los tamaños (500 x 400 px) que le he asignado ?

2. vi en los temas del foro que preguntan sobre redimensionar imagenes proporcionalmente. eso se refiere a que si uno coloca los tamaños ya definidos ?.

aclarenme esa duda por favor.

Última edición por xfer2; 05/01/2010 a las 13:02 Razón: correccion