Foros del Web » Programando para Internet » PHP »

una consulta para redimencionar imagenes

Estas en el tema de una consulta para redimencionar imagenes en el foro de PHP en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 05/01/2010, 09:52
(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
  #2 (permalink)  
Antiguo 05/01/2010, 09:54
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 11 meses
Puntos: 1517
Respuesta: una consulta para redimencionar imagenes

Se supone que sea 500 pixeles de ancho y alto. Y lo de proporcionalmente se refiere a que se pueda disminuir la imagen, el tamaño basado en el ancho y el alto de la imagen.
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #3 (permalink)  
Antiguo 05/01/2010, 09:57
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Respuesta: una consulta para redimencionar imagenes

gracias por responder abimaelrc. lo que mencionaste.

Y lo de proporcionalmente se refiere a que se pueda disminuir la imagen, el tamaño basado en el ancho y el alto de la imagen.

no entendi esa parte. me lo puedes explicar detalladamente por favor.

saludos.
  #4 (permalink)  
Antiguo 05/01/2010, 10:01
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 11 meses
Puntos: 1517
Respuesta: una consulta para redimencionar imagenes

Mira este ejemplo a la derecha, hay una imagen que menciona lo de proporcionalidad http://es.wikipedia.org/wiki/Proporcionalidad
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #5 (permalink)  
Antiguo 05/01/2010, 10:19
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Respuesta: una consulta para redimencionar imagenes

gracias por responder abimaelrc. lei un poco sobre proporcionalidad, y bueno, tendria que aplicar la regla de tres para redimencionar imagenes.

supongamos que yo defino los tamaños que son de 500 x 400 px.

Código PHP:
Ver original
  1. $ancho = 500;
  2. $alto = 400;

entonces tendria que aplicar una condicion de que si el ancho es mayor al alto de la imagen, lo redimensiona al nuevo tamaño.

pero que pasaraia si no se redimensiona a los tamaños definidos y, bueno, se redimensiona a 500 x 320.

que tendria que hacer alli ?.

saludos.
  #6 (permalink)  
Antiguo 05/01/2010, 10:52
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Respuesta: una consulta para redimencionar imagenes

alguna ayuda por favor. disculpen si estoy respondiendo 2 veces.

saludos.
  #7 (permalink)  
Antiguo 05/01/2010, 14:41
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Respuesta: una consulta para redimencionar imagenes

disculpen si estoy respondiendome a mi mismo. volviendo al tema, 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. al momento de mostrar las imagenes, me di con la sorpresa de que los tamaños de las imagenes que han sido redimensionadas no son las mismas.

mi pregunta es. 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

aclarenme esa duda por favor. ya que no he especificado bien al publicar mi tema.

saludos.

Última edición por xfer2; 05/01/2010 a las 15:07 Razón: correccion
  #8 (permalink)  
Antiguo 06/01/2010, 19:47
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Respuesta: una consulta para redimencionar imagenes

nadie me puede ayudar ?. especifique bien mi tema.

solo pido que me ayuden por favor.

Etiquetas: imagenes, redimencionar
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 07:43.