Foros del Web » Programando para Internet » PHP »

Redimensionar imagen

Estas en el tema de Redimensionar imagen en el foro de PHP en Foros del Web. Hola, bueno el trabajo con imagenes en php siempre a sido mi gran problema... espero me puedan ayudar :( Lo que pasa es que yo ...
  #1 (permalink)  
Antiguo 08/04/2008, 23:39
 
Fecha de Ingreso: marzo-2008
Mensajes: 166
Antigüedad: 18 años, 5 meses
Puntos: 5
Redimensionar imagen

Hola, bueno el trabajo con imagenes en php siempre a sido mi gran problema... espero me puedan ayudar :(

Lo que pasa es que yo cree un upload para subir solo jpg... todo funciona bien y ps lo que quiero es que una vez que se suba cree un thumbnail... pero no se como hacerlo... la imagen la subo correctamente, pero de ahi ya no se como redimensionarla a un tamaño especifico...

Saludos.
  #2 (permalink)  
Antiguo 08/04/2008, 23:51
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 18 años, 5 meses
Puntos: 2534
Re: Redimensionar imagen

mira: te compaño con una clase simple de gd2 ((que ya tienes, supongo...)) funciona simple... haber que sale

Código PHP:
class Image {
        var 
$imagefile = '';
        var 
$imageinfo = array();
        var 
$imagebuffer = '';
        var 
$imagesource = '';
        
        
        function 
info($file) {
            if (!
is_file($file))
            {
                return;
            }
            
            
            
$ext = strtolower(substr($file, strrpos($file, '.') + 1));
            if (!
preg_match('/^(jpe?g|png|gif)$/is', $ext))
            {
                return;
            }
            
            if (!
function_exists('getimagesize'))
            {
                return;
            }
            
$data = getimagesize($file);
            
            if (
is_array($data))
            {
                return array(
                    
'extension' => $ext,
                    
'width' => $data[0],
                    
'height' => $data[1],
                    
'mime' => $data['mime'],
                    
'size' => filesize($file)
                );
            }
        }
        
        
        function 
_image_open($file) {
            if (!
is_file($file))
            {
                return;
            }
            
            
$this->imageinfo = $this->info($file);
            
$fn = 'imageCreateFrom' .
                    
str_replace('jpg', 'jpeg', $this->imageinfo['extension']);
            
            
$this->imagesource = $fn($file);
            
$this->imagefile = $file;
        }
        function 
_image_close($out, $q = 75) {
            
$fn = 'image' . str_replace('jpg', 'jpeg',
                    
$this->imageinfo['extension']);
            
            if (
preg_match('/^jpe?g$/is', $this->imageinfo['extension']))
            {
                
$fn($this->imagebuffer, $out, $q);
            }
            else
            {
                
$fn($this->imagebuffer, $out);
            }
            
            
imagedestroy($this->imagesource);
            
imagedestroy($this->imagebuffer);
        }
        
        
        
        
        
        function 
_image_apply_alpha($r = 0, $g = 0, $b = 0, $alpha = 127) {
            if (
$this->imageinfo['extension'] == 'png')
                {
                
$this->imageinfo['alpha'] = imageColorAllocateAlpha($this->imagebuffer,
                            
$r, $g, $b, $alpha);
                
imagealphablending($this->imagebuffer, FALSE);
                
imagefilledrectangle($this->imagebuffer, 0, 0,
                        
imagesx($this->imagebuffer), imagesy($this->imagebuffer),
                        
$this->imageinfo['alpha']);
                
                
imagealphablending($this->imagebuffer, TRUE);
                
imagesavealpha($this->imagebuffer, TRUE);
            }
        }
        
        
        
        
        function 
resize($input, $output, $width, $height) {
            
$this->_image_open($input);
            
$this->_image_source_resize($width, $height);
            
$this->_image_close($output);
        }
        function 
_image_source_resize($width, $height) {
            
$this->imagebuffer = imagecreatetruecolor($width, $height);
            
            
$this->_image_apply_alpha();
            
            
imagecopyresampled($this->imagebuffer,
                    
$this->imagesource, 0, 0, 0, 0,
                    
$width, $height, $this->imageinfo['width'], $this->imageinfo['height']);
        }
        
        
        
        
        function 
rotate($input, $output, $deg = 90) {
            
$this->_image_open($input);
            
$this->_image_source_rotate($deg);
            
$this->_image_close($output);
        }
        function 
_image_source_rotate($deg, $bg = 0x000000) {
            if (!
function_exists('imageRotate'))
            {
                return;
            }
            
$this->imagebuffer =
                    
imageRotate($this->imagesource, $deg, $bg);
        }
        
        
        
        
        
        function 
thumb($input, $output, $width, $height = FALSE) {
            if (
$height === FALSE)
            {
                
$height = $width;
            }
            
            
            
$this->_image_open($input);
            
$test = max($width / $this->imageinfo['width'],
                    
$height / $this->imageinfo['height']);
            
            
            
$this->_image_source_resize($this->imageinfo['width'] * $test,
                    
$this->imageinfo['height'] * $test);
            
            
            
$x = round(($this->imageinfo['width'] * $test - $width) / 2);
            
$y = round(($this->imageinfo['height'] * $test - $height) / 2);
            
            
$this->imagesource = $this->imagebuffer;
            
            
$this->_image_source_copy($width, $height, $x, $y);
            
$this->_image_close($output);
        }
        
        
        function 
scale($input, $output, $width, $height = FALSE) {
            
$this->_image_open($input);
            
            
            if (
$height === FALSE)
            {
                
$height  = round(($width * $this->imageinfo['height']) / 100, 0);
                
$width = round(($width * $this->imageinfo['width']) / 100, 0);
            }
            else {
                
$test = $this->imageinfo['height'] / $this->imageinfo['width'];
                
                if (
$test < $height / $width)
                {
                    
$width = (int)min($width, $this->imageinfo['width']);
                    
$height = (int)round($width * $test);
                }
                else {
                    
$height = (int)min($height, $this->imageinfo['height']);
                    
$width = (int)round($height / $test);
                }
            }
            
            
$this->_image_source_resize($width, $height);
            
$this->_image_close($output);
        }
        
        
        
        
        function 
crop($input, $output, $width, $height, $x = 0, $y = 0) {
            
$this->_image_open($input);
            
$this->_image_source_copy($width, $height, $x, $y);
            
$this->_image_close($output);
        }
        function 
_image_source_copy($width, $height, $x = 0, $y = 0) {
            
$this->imagebuffer = imageCreateTrueColor($width, $height);
            
$this->_image_apply_alpha();
            
            
imagecopy($this->imagebuffer, $this->imagesource,
                    
0, 0, $x, $y, $width, $height);
        }
    } 
Ahora solo toka usarla...

Código PHP:
$img = new Image();

$img->thumb('grande.jpg', 'thumb-grande.jpg', 96, 96); 
util?
  #3 (permalink)  
Antiguo 09/04/2008, 13:22
 
Fecha de Ingreso: marzo-2008
Mensajes: 166
Antigüedad: 18 años, 5 meses
Puntos: 5
Re: Redimensionar imagen

Muchas gracias eh!

Me va a ser de gran utilidad... además de que me ahorraste muchas cosas :D

Saludos.
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 17:10.