Foros del Web » Programando para Internet » PHP »

duda para redimensionar imagenes y escalarlo a un tamaño fijo

Estas en el tema de duda para redimensionar imagenes y escalarlo a un tamaño fijo en el foro de PHP en Foros del Web. buenas a todos. estuve investigando en foros del web y vi ejemplos de como se redimensionar imagenes. consegui una aplicacion para que redimensione una imagen. ...
  #1 (permalink)  
Antiguo 09/01/2010, 15:59
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Pregunta duda para redimensionar imagenes y escalarlo a un tamaño fijo

buenas a todos. estuve investigando en foros del web y vi ejemplos de como se redimensionar imagenes. consegui una aplicacion para que redimensione una imagen. pero tengo unas dudas que quiero que me respondan por favor.

supongamos que tengo una imagen de 1280 x 1240 px y quiero redimensionarlo a 500 x 400 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, es decir, no lo redimensiona a los tamaños que le asignado.

esta es la 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. ?>
  #2 (permalink)  
Antiguo 09/01/2010, 15:59
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Respuesta: duda para redimensionar imagenes y escalarlo a un tamaño fijo

continuo con mi tema.

mi duda es la siguiente:

1. en la clase que instancia cuando 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 el tamaño (500 x 400 px) que le he asignado ?. tendria que aplicar una condicion de que si el ancho es mayor al alto de la imagen, lo redimensiona al nuevo tamaño ?.

3. preguntando a unos compañeros sobre las redimension de una imagen, me di cuenta que cuando coloco un ancho y un alto fijo a la clase que redimensiona la imagen (de 500 x 400 px).

Código PHP:
Ver original
  1. $path = "imagenes/";
  2. $path_thumbnail = "thumbs/";
  3. move_uploaded_file($uploadtempname,$path.$nuevo_imagen);
  4. $thumb=new thumbnail($path.$nuevo_imagen); // prepare to generate "$nuevo_imagen" in directory "/www"
  5. $thumb->size_width(500);           // set width for thumbnail with 500 pixels
  6. $thumb->size_height(400);         // set the biggest width or height for thumbnail
  7. $thumb->jpeg_quality(75);         // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
  8. $thumb->save($path_thumbnail."thb_".$nuevo_imagen);       // save my  thumbnail to file "thb_".$nuevo_imagen" in directory "/www/thumbs

las proporciones de los tamaños de las imagenes no son las mismas cuando se ha querido redimensionar con el alto y ancho fijo.

4. se puede redimensionar una imagen de tamaño 1280 x 1240 px de dimensiones 500 x 400 px, es decir, escalarlo a un tamaño fijo ?.

y si muestro solamente la funcion que solo redimensionara el ancho de la imagen y no ponerle las dimensiones fijas para que la imagen se vea bien.

Código PHP:
Ver original
  1. $path = "imagenes/";
  2. $path_thumbnail = "thumbs/";
  3. move_uploaded_file($uploadtempname,$path.$nuevo_imagen);
  4. $thumb=new thumbnail($path.$nuevo_imagen); // prepare to generate "$nuevo_imagen" in directory "/www"
  5. $thumb->size_width(500);           // set width for thumbnail with 500 pixels
  6. $thumb->jpeg_quality(75);          // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
  7. $thumb->save($path_thumbnail."thb_".$nuevo_imagen);       // save my  thumbnail to file "thb_".$nuevo_imagen" in directory "/www/thumb

esa es mi duda con respecto a la redimension de una imagen y escalarlo a un tamaño fijo.

mi intencion no fue duplicar mi tema, sino no estaba especificado bien mi tema desde el principio. y aparte tambien que me mostro un mensaje de error que solo se permiten hasta 10000 caracteres.

por favor, aclarenme mis dudas que tengo por favor.

saludos.

Etiquetas: fijo, imagenes, redimensionar, tamaño
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 22:36.