Foros del Web » Programando para Internet » PHP »

Cambiar tamaño de imagen

Estas en el tema de Cambiar tamaño de imagen en el foro de PHP en Foros del Web. tengo un script que remuestrea una imagen on-the-fly pero luego no sé como hacer para guardar la imagen resultante en el servidor. Código PHP: <?php ...
  #1 (permalink)  
Antiguo 08/03/2006, 05:19
Avatar de mrgubu  
Fecha de Ingreso: febrero-2002
Ubicación: Granada
Mensajes: 431
Antigüedad: 22 años, 2 meses
Puntos: 2
Cambiar tamaño de imagen

tengo un script que remuestrea una imagen on-the-fly pero luego no sé como hacer para guardar la imagen resultante en el servidor.

Código PHP:
<?php

    
    
    
/**
     * Image Resizer. 
     * @author : Harish Chauhan
     * @copyright : Freeware
     * About :This PHP script will resize the given image and can show on the fly or save as image file.
     * phpclass.com
     */
    


    
define("HAR_AUTO_NAME",1);    
    Class 
RESIZEIMAGE
    
{
        var 
$imgFile="";
        var 
$imgWidth=0;
        var 
$imgHeight=0;
        var 
$imgType="";
        var 
$imgAttr="";
        var 
$type=NULL;
        var 
$_img=NULL;
        var 
$_error="";
        
        
/**
         * Constructor
         *
         * @param [String $imgFile] Image File Name
         * @return RESIZEIMAGE (Class Object)
         */
        
        
function RESIZEIMAGE($imgFile="")
        {
            if (!
function_exists("imagecreate"))
            {
                
$this->_error="Error: GD Library is not available.";
                return 
false;
            }

            
$this->type=Array(=> 'GIF'=> 'JPG'=> 'PNG'=> 'SWF'=> 'PSD'=> 'BMP'=> 'TIFF'=> 'TIFF'=> 'JPC'10 => 'JP2'11 => 'JPX'12 => 'JB2'13 => 'SWC'14 => 'IFF'15 => 'WBMP'16 => 'XBM');
            if(!empty(
$imgFile))
                
$this->setImage($imgFile);
        }
        
/**
         * Error occured while resizing the image.
         *
         * @return String 
         */
        
function error()
        {
            return 
$this->_error;
        }
        
        
/**
         * Set image file name
         *
         * @param String $imgFile
         * @return void
         */
        
function setImage($imgFile)
        {
            
$this->imgFile=$imgFile;
            return 
$this->_createImage();
        }
        
/**
         * 
         * @return void
         */
        
function close()
        {
            return @
imagedestroy($this->_img);
        }
        
/**
         * Resize a image to given width and height and keep it's current width and height ratio
         * 
         * @param Number $imgwidth
         * @param Numnber $imgheight
         * @param String $newfile
         */
        
function resize_limitwh($imgwidth,$imgheight,$newfile=NULL)
        {
            list(
$width$height$type$attr) = @getimagesize($this->imgFile);
            if(
$width $imgwidth)
                
$image_per floor(($imgwidth 100) / $width);

            if(
floor(($height $image_per)/100)>$imgheight)
                
$image_per floor(($imgheight 100) / $height);

            
$this->resize_percentage($image_per,$newfile);

        }
        
/**
         * Resize an image to given percentage.
         *
         * @param Number $percent
         * @param String $newfile
         * @return Boolean
         */
        
function resize_percentage($percent=100,$newfile=NULL)
        {
            
$newWidth=($this->imgWidth*$percent)/100;
            
$newHeight=($this->imgHeight*$percent)/100;
            return 
$this->resize($newWidth,$newHeight,$newfile);
        }
        
/**
         * Resize an image to given X and Y percentage.
         *
         * @param Number $xpercent
         * @param Number $ypercent
         * @param String $newfile
         * @return Boolean
         */
        
function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL)
        {
            
$newWidth=($this->imgWidth*$xpercent)/100;
            
$newHeight=($this->imgHeight*$ypercent)/100;
            return 
$this->resize($newWidth,$newHeight,$newfile);
        }
        
        
/**
         * Resize an image to given width and height
         *
         * @param Number $width
         * @param Number $height
         * @param String $newfile
         * @return Boolean
         */
        
function resize($width,$height,$newfile=NULL)
        {
            if(empty(
$this->imgFile))
            {
                
$this->_error="File name is not initialised.";
                return 
false;
            }
            if(
$this->imgWidth<=|| $this->imgHeight<=0)
            {
                
$this->_error="Could not resize given image";
                return 
false;
            }
            if(
$width<=0)
                
$width=$this->imgWidth;
            if(
$height<=0)
                
$height=$this->imgHeight;
                
            return 
$this->_resize($width,$height,$newfile);
        }
        
        
/**
         * Get the image attributes
         * @access Private
         *         
         */
        
function _getImageInfo()
        {
            @list(
$this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile);
            
$this->imgType=$this->type[$type];
        }
        
        
/**
         * Create the image resource 
         * @access Private
         * @return Boolean
         */
        
function _createImage()
        {
            
$this->_getImageInfo($imgFile);
            if(
$this->imgType=='GIF')
            {
                
$this->_img=@imagecreatefromgif($this->imgFile);
            }
            elseif(
$this->imgType=='JPG')
            {
                
$this->_img=@imagecreatefromjpeg($this->imgFile);
            }
            elseif(
$this->imgType=='PNG')
            {
                
$this->_img=@imagecreatefrompng($this->imgFile);
            }            
            if(!
$this->_img || !@is_resource($this->_img))
            {
                
$this->_error="Error loading ".$this->imgFile;
                return 
false;
            }
            return 
true;
        }
        
        
/**
         * Function is used to resize the image
         * 
         * @access Private
         * @param Number $width
         * @param Number $height
         * @param String $newfile
         * @return Boolean
         */
        
function _resize($width,$height,$newfile=NULL)
        {
            if (!
function_exists("imagecreate"))
            {
                
$this->_error="Error: GD Library is not available.";
                return 
false;
            }

            
$newimg=@imagecreatetruecolor($width,$height);
            @
imagecopyresampled $newimg$this->_img0,0,0,0$width$height$this->imgWidth,$this->imgHeight);
            if(
$newfile===HAR_AUTO_NAME)
            {
                if(@
preg_match("/\..*+$/",@basename($this->imgFile),$matches))
                       
$newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0);            
            }
            elseif(!empty(
$newfile))
            {
                if(!@
preg_match("/\..*+$/",@basename($newfile)))
                {
                    if(@
preg_match("/\..*+$/",@basename($this->imgFile),$matches))
                       
$newfile=$newfile.$matches[0];
                }
            }

            if(
$this->imgType=='GIF')
            {
                if(!empty(
$newfile))
                    @
imagegif($newimg,$newfile);
                else
                {
                    @
header("Content-type: image/gif");
                    @
imagegif($newimg);
                }
            }
            elseif(
$this->imgType=='JPG')
            {
                if(!empty(
$newfile))
                    @
imagejpeg($newimg,$newfile);
                else
                {
                    @
header("Content-type: image/jpeg");
                    @
imagejpeg($newimg);
                }
            }
            elseif(
$this->imgType=='PNG')
            {
                if(!empty(
$newfile))
                    @
imagepng($newimg,$newfile);
                else
                {
                    @
header("Content-type: image/png");
                    @
imagepng($newimg);
                }
            }
            @
imagedestroy($newimg);
        }
    }
?>
Llamada:

Código PHP:
<?php

    
include_once("resizeimage.inc.php");
    
$rimg=new RESIZEIMAGE("021202.jpg");
    echo 
$rimg->error();
    
$rimg->resize_limitwh(120,120);
    
$rimg->close();
?>
  #2 (permalink)  
Antiguo 08/03/2006, 06:55
Avatar de claudiovega  
Fecha de Ingreso: octubre-2003
Ubicación: Puerto Montt
Mensajes: 3.667
Antigüedad: 20 años, 6 meses
Puntos: 11
Cuando llamas a imagepng() debes indicarle el archivo en que vas a dejar la imagen en el servidor, asi: @imagepng($newimg,"archivo.png");
  #3 (permalink)  
Antiguo 08/03/2006, 07:21
Avatar de mrgubu  
Fecha de Ingreso: febrero-2002
Ubicación: Granada
Mensajes: 431
Antigüedad: 22 años, 2 meses
Puntos: 2
No sé como hacerlo: Intento :

Código PHP:
<?php

    
include_once("resizeimage.inc.php");
    
$rimg=new RESIZEIMAGE("021202.jpg");
    echo 
$rimg->error();
    
$rimg->resize_limitwh(120,120);
@
imagejpeg($newimg,"archivo.jpg");
    
$rimg->close();
?>
Pero no va. La verdad es que de clases estoy todavía un poco pegado.
  #4 (permalink)  
Antiguo 08/03/2006, 07:36
Avatar de claudiovega  
Fecha de Ingreso: octubre-2003
Ubicación: Puerto Montt
Mensajes: 3.667
Antigüedad: 20 años, 6 meses
Puntos: 11
No me habia dado cuenta, la clase hace todo, sólo debes hacer esto en tu codigo:

Código PHP:
<?php
    
include_once("resizeimage.inc.php");
    
$rimg=new RESIZEIMAGE("021202.jpg");
    echo 
$rimg->error();
    
//aqui pasas el archivo en que quieres que quede la imagen
    
$rimg->resize_limitwh(120,120,"archivo.jpg");
    
$rimg->close();
?>
  #5 (permalink)  
Antiguo 10/03/2006, 12:44
Avatar de mrgubu  
Fecha de Ingreso: febrero-2002
Ubicación: Granada
Mensajes: 431
Antigüedad: 22 años, 2 meses
Puntos: 2
Pues no furula. Pensaba que era problema de permisos de escritura, pero ya he confirmado que la carpeta tiene permisos.

Al ejecutar la llamada ni se remuestrea la imagen ni se guarda.
  #6 (permalink)  
Antiguo 11/03/2006, 05:02
Avatar de mrgubu  
Fecha de Ingreso: febrero-2002
Ubicación: Granada
Mensajes: 431
Antigüedad: 22 años, 2 meses
Puntos: 2
Por favor alguien sabe como solucionar esto? O me puede decir alguna otra clase que funcione bien para remuestrear imágenes y guardarlas en el servidor ?
  #7 (permalink)  
Antiguo 13/03/2006, 11:59
Avatar de mrgubu  
Fecha de Ingreso: febrero-2002
Ubicación: Granada
Mensajes: 431
Antigüedad: 22 años, 2 meses
Puntos: 2
¿Por favor podría probar alguien el código para saber al menos si el fallo es mío o es del código?
  #8 (permalink)  
Antiguo 13/03/2006, 12:18
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 3 meses
Puntos: 129
Yo empezaría quitando todos los @ que tienen funciones en tu código .. así veras más claro si hay algún tipo de error que con la @ delante estás ocultandolo y si la classe (como es tu caso) no gestiona esos posibles errores .. te quedarás sin ver algún mensaje de error por el cual identificar donde puedas tener el problema.

Un saludo,
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 02:13.