Foros del Web » Programando para Internet » PHP »

Nuevo Problema...

Estas en el tema de Nuevo Problema... en el foro de PHP en Foros del Web. Un saludo a toda esta comunidad Tengo un nuevo problema, se trata que tengo que subir una imagen (upload) pero una vez subida tengo que ...
  #1 (permalink)  
Antiguo 08/09/2003, 15:23
 
Fecha de Ingreso: agosto-2003
Ubicación: Málaga, España
Mensajes: 419
Antigüedad: 20 años, 8 meses
Puntos: 1
Nuevo Problema...

Un saludo a toda esta comunidad

Tengo un nuevo problema, se trata que tengo que subir una imagen (upload) pero una vez subida tengo que sacar dos copias a diferente tamaño.

¿Alguien sabe de algún elemento, libreria, función... para poder redimensionar un archivo imagen (jpg,gif)?

Muchas Gracias.
Miguel Ángel.
  #2 (permalink)  
Antiguo 08/09/2003, 15:32
Ex Colaborador
 
Fecha de Ingreso: junio-2002
Mensajes: 9.091
Antigüedad: 21 años, 10 meses
Puntos: 16
Hola,

Es la libreria GD. Pero dependiendo de version, no soporta GIF, al menos de escritura (problemas de patentes).

Saludos.
__________________
Josemi

Aprendiz de mucho, maestro de poco.
  #3 (permalink)  
Antiguo 08/09/2003, 15:40
 
Fecha de Ingreso: agosto-2003
Ubicación: Málaga, España
Mensajes: 419
Antigüedad: 20 años, 8 meses
Puntos: 1
Gracias Josemi!
¿Sabes dónde puedo conseguirla?
  #4 (permalink)  
Antiguo 08/09/2003, 15:53
 
Fecha de Ingreso: febrero-2003
Ubicación: Alicante
Mensajes: 29
Antigüedad: 21 años, 2 meses
Puntos: 0
http://www.boutell.com/gd/ es la web oficial de la biblioteca GD, pero solo ofrecen de la versión 2.0 para arriba, sin soporte para GIF, para encontrar versiones inferiores tendrás que recurrir a mi amigo Google . Espero haber sido de ayuda

*EDIT*
Acabo de encontrar la última versión que soporta GIF, aqui tienes el link http://stein.cshl.org/WWW/software/G...GD-1.19.tar.gz
  #5 (permalink)  
Antiguo 08/09/2003, 23:06
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 3 meses
Puntos: 129
Pero .. a todo esto .. nuestro amigo/a bauhaus1975 usa PHP bajo Linux o Windows? ..

Lo comento por qué si es Windows .. las extensiones GD ya las tiens compildas en su directorio /extensions .. sólo hay que habilitar las php_gd2.dll en el php.ini ..


Un saludo,
__________________
Por motivos personales ya no puedo estar con Uds. Fue grato haber compartido todos estos años. Igualmente los seguiré leyendo.
  #6 (permalink)  
Antiguo 09/09/2003, 03:30
 
Fecha de Ingreso: agosto-2003
Ubicación: Málaga, España
Mensajes: 419
Antigüedad: 20 años, 8 meses
Puntos: 1
Hola
Pues uso PHP bajo windows

Un saludo
  #7 (permalink)  
Antiguo 09/09/2003, 03:48
Avatar de davidMS  
Fecha de Ingreso: abril-2003
Ubicación: Chepe
Mensajes: 202
Antigüedad: 21 años
Puntos: 0
Yo tengo un script

echo en gd, usa una clase y una funcion, se pasa el nombre del archivo, y adonde lo quiere guardar!

Lo puedo pasar poraqui si lo quieres?
  #8 (permalink)  
Antiguo 09/09/2003, 03:55
 
Fecha de Ingreso: agosto-2003
Ubicación: Málaga, España
Mensajes: 419
Antigüedad: 20 años, 8 meses
Puntos: 1
Pero ¿puedo cambiar el tamaño de la imagen?
  #9 (permalink)  
Antiguo 09/09/2003, 03:58
Avatar de davidMS  
Fecha de Ingreso: abril-2003
Ubicación: Chepe
Mensajes: 202
Antigüedad: 21 años
Puntos: 0
claro...
  #10 (permalink)  
Antiguo 09/09/2003, 06:38
 
Fecha de Ingreso: agosto-2003
Ubicación: Málaga, España
Mensajes: 419
Antigüedad: 20 años, 8 meses
Puntos: 1
Si fueras tan amable de pasarlo...

Muchas gracias y un saludo.
  #11 (permalink)  
Antiguo 10/09/2003, 00:09
Avatar de davidMS  
Fecha de Ingreso: abril-2003
Ubicación: Chepe
Mensajes: 202
Antigüedad: 21 años
Puntos: 0
esta es la clase:

Código PHP:
<?PHP


require("hft_image_errors.php"); // para editar los errores del lenguajes

    
    
class hft_image {
        var 
$image_original;
        var 
$file_original;
        var 
$image_original_width;
        var 
$image_original_height;
        var 
$image_original_type_code;
        var 
$image_original_type_abbr;
        var 
$image_original_html_sizes;

        var 
$image_resized;
        var 
$file_resized;
        var 
$image_resized_width;
        var 
$image_resized_height;
        var 
$image_resized_type_code;
        var 
$image_resized_type_abbr;
        var 
$image_resized_html_sizes;

        
//some settings
        
var $jpeg_quality;
        var 
$use_gd2;

        
        function 
hft_image($file_original){
            
//constructor of the class
            //it takes given file and creates image out of it
            
global $ERR;
            
$this->clear(); // clear all.

            
if(file_exists($file_original))    {
                
$this->file_original $file_original;
                
$this->image_original $this->imagecreatefromfile($file_original);
                if(!
$this->image_original){
                    
$this->error($ERR["IMAGE_NOT_CREATED_FROM_FILE"]." file=$file_original"); 
                    return 
false;
                }


            } else {
                
$this->error($ERR["FILE_DOESNOT_EXSIT"]." file=$file_original");
            }
        }

        function 
clear() {
            

                
$this->image_original            =    0;
                
$this->file_original            =    "";
                
$this->image_original_width        =    0;
                
$this->image_original_height    =    0;
                
$this->image_original_type_code    =    0;
                
$this->image_original_type_abbr    =    "";
                
$this->image_original_html_sizes=    "";

                
$this->image_resized            =    0;
                
$this->file_resized                =    "";
                
$this->image_resized_width        =    0;
                
$this->image_resized_height        =    0;
                
$this->image_resized_type_code    =    -1;
                
$this->image_resized_type_abbr    =    "";
                
$this->image_resized_html_sizes    =    "";

                
$this->set_parameters();

        }

        function 
set_parameters($jpeg_quality="85"$use_gd2=true) {

            
$this->jpeg_quality=$jpeg_quality;
            
$this->use_gd2=$use_gd2;
        }

        function 
error($msg){
            
            echo(
"<hr color='red'><font color='red'><b>$msg</b></font><br> file=<b>".__FILE__."</b><hr color='red'>");
        }
    

        function 
imagecreatefromfile($img_file){
            global 
$ERR;
            
$img=0;
            
$img_sz =  getimagesize$img_file );     ## returns array with some properties like dimensions and type;
            ####### Now create original image from uploaded file. Be carefull! GIF is often not supported, as far as I remember from GD 1.6
            
switch( $img_sz[2] ){
                case 
1:
                    
$img $this->_imagecheckandcreate("ImageCreateFromGif"$img_file);
                    
$img_type "GIF";
                break;
                case 
2
                    
$img $this->_imagecheckandcreate("ImageCreateFromJpeg"$img_file);
                    
$img_type "JPG";
                break;
                case 
3
                    
$img $this->_imagecheckandcreate("ImageCreateFromPng"$img_file);
                    
$img_type "PNG";
                break;
                
// would be nice if this function will be finally supported
                
case 4
                    
$img $this->_imagecheckandcreate("ImageCreateFromSwf"$img_file);
                    
$img_type "SWF";
                break;
                default:
                    
$img 0;
                    
$img_type "UNKNOWN";
                    
$this->error($ERR["IMG_NOT_SUPPORTED"]." $img_file");
                    break;
                
            }
//case
        
            
if($img){
                
$this->image_original_width=$img_sz[0];
                
$this->image_original_height=$img_sz[1];
                
$this->image_original_type_code=$img_sz[2];
                
$this->image_original_type_abbr=$img_type;
                
$this->image_original_html_sizes=$img_sz[3];

            }else {
                
$this->clear();

            }

            return 
$img;        
        }


        function 
_imagecheckandcreate($function$img_file) {
            global 
$ERR;
            if(
function_exists($function)) {
                
$img $function($img_file); 
            }else{
                
$img false;
                
$this->error($ERR["FUNCTION_DOESNOT_EXIST"]." ".$function);
            }

            return 
$img;
            
        }

        function 
resize($desired_width$desired_height$mode="-"){
            global 
$ERR;
            if(
$desired_width == "*" && $desired_height == "*"){
                
$this->image_resized $this->image_original;
                Return 
true;
            }
            switch(
$mode) {
                case 
"-":
                case 
'+':
                    if(
$desired_width != "*"$mult_x $desired_width $this->image_original_width;        
                    if(
$desired_height != "*"$mult_y $desired_height $this->image_original_height;                
                    
$ratio $this->image_original_width $this->image_original_height;

                    if(
$desired_width == "*"){
                        
$new_height $desired_height;
                        
$new_width $ratio $desired_height;
                    }elseif(
$desired_height == "*"){
                        
$new_height $desired_width $ratio;
                        
$new_width =  $desired_width;

                    }else{
                        if(
$mode=="-"){
                                if( 
$this->image_original_height $mult_x $desired_height ){
                                
                                    
$new_width $desired_width;
                                    
$new_height $this->image_original_height $mult_x;
                                }else{
                                    
                                    
$new_width $this->image_original_width $mult_y;
                                    
$new_height $desired_height;
                                }

                        }else{
                            
                            if( 
$this->image_original_height $mult_x $desired_height ){
                                
                                
$new_width $desired_width;
                                
$new_height $this->image_original_height $mult_x;
                            }else{
                                
                                
$new_width $this->image_original_width $mult_y;
                                
$new_height $desired_height;
                            }

                        }
                    }
                break;

                case 
'0':
                    
                    if(
$desired_width == "*"$desired_width $this->image_original_width;        
                    if(
$desired_height == "*"$desired_height $this->image_original_height;    
                    
$new_width $desired_width;
                    
$new_height $desired_height;
                    
                break;
                default: 
                    
$this->error($ERR["UNKNOWN_RESIZE_MODE"]."  $mode");
                break;
            }
            
            
            if( 
$this->use_gd2 ){
                if( 
function_exists("imagecreatetruecolor")){
                
                    
$this->image_resized imagecreatetruecolor($new_width$new_height) or $this->error($ERR["GD2_NOT_CREATED"]);
                }else {
                    
$this->error($ERR["GD2_UNAVALABLE"]." ImageCreateTruecolor()");
                }
            } else {
            
            
                
$this->image_resized imagecreate($new_width$new_height) or $this->error($ERR["IMG_NOT_CREATED"]);
            }

            
//Resize
            
if( $this->use_gd2 ){

                if( 
function_exists("imagecopyresampled")){
                    
$res imagecopyresampled($this->image_resized
                                              
$this->image_original
                                              
00,  //dest coord
                                              
00,            //source coord
                                              
$new_width$new_height//dest sizes
                                              
$this->image_original_width$this->image_original_height // src sizes
                                            
) or $this->error($ERR["GD2_NOT_RESIZED"]); 

                }else {
                    
$this->error($ERR["GD2_UNAVALABLE"]." ImageCopyResampled()");
                }
            } else {
            
                
$res imagecopyresized($this->image_resized
                                          
$this->image_original
                                          
00,  //dest coord
                                          
00,            //source coord
                                          
$new_width$new_height//dest sizes
                                          
$this->image_original_width$this->image_original_height // src sizes
                                        
) or $this->error($ERR["IMG_NOT_RESIZED"]); 
            }

        }
        
        function 
output_original($destination_file$image_type="JPG") { 
          
            return 
_output_image($destination_file$image_type$this->image_original); 
        } 
        
        function 
output_resized($destination_file$image_type="JPG") { 
          
            
$res $this->_output_image($destination_file$image_type$this->image_resized); 
            if(
trim($destination_file)){ 
                
$sz=getimagesize($destination_file); 
                
$this->file_resized $destination_file
                
$this->image_resized_width $sz[0]; 
                
$this->image_resized_height $sz[1]; 
                
$this->image_resized_type_code=$sz[2]; 
                
$this->image_resized_html_sizes=$sz[3]; 
                  
                switch(
$this->image_resized_html_sizes){ 
                    case 
0
                        
$this->image_resized_type_abbr "GIF"
                    break; 
                    case 
1
                        
$this->image_resized_type_abbr "JPG"
                    break; 
                    case 
2
                        
$this->image_resized_type_abbr "PNG"
                    break; 
                    case 
3
                        
$this->image_resized_type_abbr "SWF"
                    break; 
                    default: 
                        
$this->image_resized_type_abbr "UNKNOWN"
                    break; 
                } 
                 
            } 
            return 
$res
        } 

        function 
_output_image($destination_file$image_type$image){ 
          
            global 
$ERR
            
$destination_file trim($destination_file); 
            
$res false
            if(
$image){ 
                switch(
$image_type) { 
                    case 
'JPEG'
                    case 
'JPG'
                     
                        
$res ImageJpeg($image$destination_file$this->jpeg_quality); 
                    break; 
                    case 
'PNG'
                        
$res Imagepng($image$destination_file); 
                    break; 
                    default: 
                        
$this->error($ERR["UNKNOWN_OUTPUT_FORMAT"]." $image_type"); 
                    break; 
             
                } 
            }else{ 
                
$this->error($ERR["NO_IMAGE_FOR_OUTPUT"]); 
            } 
            if(!
$res$this->error($ERR["UNABLE_TO_OUTPUT"]." $destination_file"); 
            return 
$res
        } 



?>
  #12 (permalink)  
Antiguo 10/09/2003, 00:12
Avatar de davidMS  
Fecha de Ingreso: abril-2003
Ubicación: Chepe
Mensajes: 202
Antigüedad: 21 años
Puntos: 0
este es el archivo que pide la clase para el control de errores:

Código PHP:
<?PHP

    $ERR
["UNABLE_TO_OUTPUT"]        = "Unable to output: ";
    
$ERR["FILE_DOESNOT_EXSIT"]        = "This file does not exist: ";
    
$ERR["FUNCTION_DOESNOT_EXIST"]    = "This function does not exist: ";
    
$ERR["GD2_NOT_CREATED"]            =    "GD2 is installed, function ImageCreateTruecolor() exists, but image is not created";
    
$ERR["IMG_NOT_CREATED"]            =    "Image is not created ImageCreate(). {GD2 suppor is OFF}";
    
$ERR["GD2_UNAVALABLE"]            =    "You specified to use GD2, but not all GD2 functions are present.";
    
$ERR["GD2_NOT_RESIZED"]            =    "GD2 is installed, function ImageCopyResampled() exists, but image is not resized";
    
$ERR["IMG_NOT_RESIZED"]            =    "Image was not resized. {GD2 suppor is OFF}";
    
$ERR["UNKNOWN_OUTPUT_FORMAT"]    =    "This image format cannot bu output: ";
    
$ERR["NO_IMAGE_FOR_OUTPUT"]        =    "Image you are trying to output does not exist. ";
    
$ERR["IMG_NOT_SUPPORTED"]        =    "Can not create image. Sorry, this image type is not supported yet.";
?>

y esta es la funcion para llamar a la clase:

Código PHP:
function getjpg ($nombre,$imagen,$url)
{
    
//variables globales
    
global $tabla,$donde,$db;
    
//    
    //jala extencion del archivo (string)
    
function getFileExtension($str
    {
        
$i strrpos($str,".");
        if (!
$i) { return ""; }
        
$l strlen($str) - $i;
        
$ext substr($str,$i+1,$l);
        return 
$ext;
    }
    
//**//    
    //Chequea que el archivo sea un jpg o jpeg
    
$pext getFileExtension($nombre);
    
$pext strtolower($pext);
    if ((
$pext != "jpg")  && ($pext != "jpeg"))
    {
        print 
"<h1>ERROR</h1>Extension de imagen no compatible!!<br>";
        print 
"<p>Por favor solo use imagenes con extension .jpg o .jpeg<br><br>";
        print 
"El archivo que usted intento subir tenia la extension: <b>$pext</b></p>\n";
        exit();
    }
    
//ahora a cambiar el tamaño de la imagen
        
require("inc/hft_image.php"); /*aqui es donde esta la clase*/ 
    //se define ancho y largo deseado
        
$destination_width 250
        
$destination_height 132;
    
//**//
     
        
$image = new hft_image($imagen); 
        
$image->resize("*"$destination_width'-'); 
        
$image->output_resized($url"JPEG"); 
    
// terminado el resize borramos el archivo temporal       
        
unlink($imagen);
    
//ahora si, insertamos el nombre del archivo en la bd//
        
mysql_db_query ($db,'insert into '.$tabla.' ('.$donde.') values ("'.$url.'")') or die 
        (
'<div align="center"><font color="#FF0000" size="2"><strong>Error al procesar a la BD, intentelo despues!.
        </strong></font> </div></body>'
        
); 
    

  #13 (permalink)  
Antiguo 10/09/2003, 00:15
Avatar de davidMS  
Fecha de Ingreso: abril-2003
Ubicación: Chepe
Mensajes: 202
Antigüedad: 21 años
Puntos: 0
si quieres quitas lo ultimo que es un query...

entonces trabjaria asi:
un archivo con la clase /inc/hft_image.php
otro con los errores /inc/hft_image_errors.php

y un ejempo de la funcion seria mas o menos asi:
getjpg ($_FILES["imagen"]["name"],$imagen,"loco2.jpg");
  #14 (permalink)  
Antiguo 10/09/2003, 00:42
Avatar de nuevo  
Fecha de Ingreso: mayo-2003
Ubicación: Spain
Mensajes: 2.009
Antigüedad: 20 años, 11 meses
Puntos: 2
yo debo ser algo torpe, siempre me tira este error
ERROR
Extension de imagen no compatible!!

Por favor solo use imagenes con extension .jpg o .jpeg

El archivo que usted intento subir tenia la extension:

y el fichero es jpg....
__________________
3w.valenciadjs.com
3w.laislatv.com
  #15 (permalink)  
Antiguo 10/09/2003, 00:50
Avatar de davidMS  
Fecha de Ingreso: abril-2003
Ubicación: Chepe
Mensajes: 202
Antigüedad: 21 años
Puntos: 0
es que esta un poquillo personalizada, ahorita esta jalando desde un form de upload= "<form enctype="multipart/form-data" action="tmpaction.php" method="post">
Ubicación <input type="file" name="imagen">
<input type="submit" value="Enviar archivo">
</form>"

es nada mas de darle vuelta a la funcion, pero la funcion es nada mas para usar la clase, si quieres no usas la funcion, y utilisas directamente la funcion mas o menos solo esto : require("inc/hft_image.php"); /*aqui es donde esta la clase*/
//se define ancho y largo deseado
$destination_width = 250;
$destination_height = 132;
//**//

$image = new hft_image($imagen);
$image->resize("*", $destination_width, '-');
$image->output_resized($url, "JPEG");
// terminado el resize" donde $imagen es el path de tu imagen y $url el nombre que le vas a dar nuevo!

Pura vida!
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 19:19.