Ver Mensaje Individual
  #7 (permalink)  
Antiguo 25/05/2009, 05:05
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Reescalar imagen - Guardarla luego

continuar

Código PHP:
    /**
     *  Resizes the image given as {@link sourceFile} and outputs the resulted image as {@link targetFile}
     *  while following user specified properties
     *
     *  @return boolean     TRUE on success, FALSE on error.
     *                      If FALSE is returned, check the {@link error} property to see what went wrong
     */
    
function resize()
    {

        
// tries to create an image from sourceFile
        
$result $this->create_image_from_source_file();
        
        
// if operation was successful
        
if (is_array($result)) {
        
            list(
$sourceImageIdentifier$sourceImageWidth$sourceImageHeight$sourceImageType) = $result;
        
            
// if aspect ratio needs to be maintained
            
if ($this->maintainAspectRatio) {
            
                
// calculates image's aspect ratio
                
$aspectRatio =

                    
$sourceImageWidth <= $sourceImageHeight ?

                        
$sourceImageHeight $sourceImageWidth :

                        
$sourceImageWidth $sourceImageHeight;

                
$targetImageWidth $sourceImageWidth;

                
$targetImageHeight $sourceImageHeight;

                
// if resizeToWidth is set
                
if ($this->resizeToWidth >= 0) {

                    
// set the (temporary) width of target image
                    // this can be later altered by the script but we need it for
                    // the case when the script skips the following if() block
                    
$lockedTargetImageWidth $this->resizeToWidth;

                }

                
// if width of image is greater than resizeToWidth property and resizeIfGreater property is TRUE
                // or width of image is smaller than resizeToWidth property and resizeIfSmaller property is TRUE
                
if (

                    (
$this->resizeToWidth >= && $targetImageWidth $this->resizeToWidth && $this->resizeIfGreater) ||

                    (
$this->resizeToWidth >= && $targetImageWidth $this->resizeToWidth && $this->resizeIfSmaller)

                ) {
                
                    
// set the width of target image
                    
$targetImageWidth $this->resizeToWidth;
                    
                    
// set the height of target image so that the image will keep its aspect ratio
                    
$targetImageHeight =

                        
$sourceImageWidth <= $sourceImageHeight ?

                            
$targetImageWidth $aspectRatio :

                            
$targetImageWidth $aspectRatio;
                            
                    
// saves the got width in case the next section wants to change it
                    
$lockedTargetImageWidth $targetImageWidth;
                    
                }
                
                
// if height of image is greater than resizeToHeight property and resizeIfGreater property is TRUE
                // or height of image is smaller than resizeToHeight property and resizeIfSmaller property is TRUE
                
if (

                    (
$this->resizeToHeight >= && $targetImageHeight $this->resizeToHeight && $this->resizeIfGreater) ||

                    (
$this->resizeToHeight >= && $targetImageHeight $this->resizeToHeight && $this->resizeIfSmaller)

                ) {
                
                    
// set the height of target image
                    
$targetImageHeight $this->resizeToHeight;
                    
                    
// set the width of target image so that the image will keep its aspect ratio
                    
$targetImageWidth =

                        
$sourceImageWidth <= $sourceImageHeight ?

                            
$targetImageHeight $aspectRatio :

                            
$targetImageHeight $aspectRatio;
                            
                    
// if maximum width was already set but has changed now
                    
if (

                        isset(
$lockedTargetImageWidth) &&

                        
$targetImageWidth $lockedTargetImageWidth

                    
) {
                    
                        
// adjust the height so that the width remains as set before
                        
while ($targetImageWidth $lockedTargetImageWidth) {
                        
                            
$targetImageHeight--;

                            
$targetImageWidth =

                                
$sourceImageWidth <= $sourceImageHeight ?

                                    
$targetImageHeight $aspectRatio :

                                    
$targetImageHeight $aspectRatio;

                        }
                        
                    }
                    
                }
                
            
// if aspect ratio does not need to be maintained
            
} else {
            
                
$targetImageWidth = ($this->resizeToWidth >= $this->resizeToWidth $sourceImageWidth);

                
$targetImageHeight = ($this->resizeToHeight >= $this->resizeToHeight $sourceImageHeight);
                
            }
            
            
// prepares the target image
            
$targetImageIdentifier $this->create_target_image_identifier($targetImageWidth$targetImageHeight);

            
// resizes image
            // but first if source image is png take care of transparency
            // this is to maintain transparency of png24 files
            
if ($sourceImageType == 3) {
            
                
imagealphablending($targetImageIdentifierfalse);

                
imagecopyresampled($targetImageIdentifier$sourceImageIdentifier0000$targetImageWidth$targetImageHeight$sourceImageWidth$sourceImageHeight);

                
imagesavealpha($targetImageIdentifiertrue);
                
            
// if image is something other than png
            
} else {
            
                
imagecopyresampled($targetImageIdentifier$sourceImageIdentifier0000$targetImageWidth$targetImageHeight$sourceImageWidth$sourceImageHeight);
                
            }
            
            
// writes image
            
return $this->output_target_image($targetImageIdentifier);
            
        
// if new image resource could not be created
        
} else {
        
            
// return false
            // note that we do not set the error level as it has been already set
            // by the create_image_from_source_file() method earlier
            
return false;
            
        }
    }
    
    
/**
     *  Crops a portion of the source file and puts it in target file
     *
     *  @param  integer     $src_x  x coordinate to start cropping from
     *  @param  integer     $src_y  y coordinate to start cropping from
     *  @param  integer     $dst_x  x coordinate where to end the cropping
     *  @param  integer     $dst_y  y coordinate where to end the cropping
     *
     *  @since  1.0.4
     *
     *  @return boolean     TRUE on success, FALSE on error.
     *                      If FALSE is returned, check the {@link error} property to see what went wrong
     */
    
function crop($src_x$src_y$dst_x$dst_y)
    {
    
        
// tries to create an image from sourceFile
        
$result $this->create_image_from_source_file();

        
// if operation was successful
        
if (is_array($result)) {

            list(
$sourceImageIdentifier$sourceImageWidth$sourceImageHeight) = $result;

            
// prepares the target image
            
$targetImageIdentifier $this->create_target_image_identifier($dst_x $src_x$dst_y $src_y);

            
// crops the image
           
imagecopyresampled($targetImageIdentifier$sourceImageIdentifier00$src_x$src_y$dst_x $src_x$dst_y $src_y$dst_x $src_x$dst_y $src_y);

            
// writes image
            
return $this->output_target_image($targetImageIdentifier);
            
        }

    }

    
/**
     *  Flips horizontally the image given as {@link sourceFile} and outputs the resulted image as {@link targetFile}
     *
     *  @return boolean     TRUE on success, FALSE on error.
     *                      If FALSE is returned, check the {@link error} property to see what went wrong
     */
    
function flip_horizontal()
    {
    
        
// tries to create an image from sourceFile
        
$result $this->create_image_from_source_file();

        
// if operation was successful
        
if (is_array($result)) {

            list(
$sourceImageIdentifier$sourceImageWidth$sourceImageHeight) = $result;
        
            
// prepares the target image
            
$targetImageIdentifier $this->create_target_image_identifier($sourceImageWidth$sourceImageHeight);
            
            
// flips image horizontally
            
for ($x 0$x $sourceImageWidth$x++) {

               
imagecopyresampled($targetImageIdentifier$sourceImageIdentifier$x0$sourceImageWidth $x 101$sourceImageHeight);

            }
            
            
// writes image
            
return $this->output_target_image($targetImageIdentifier);
            
        
// if new image resource could not be created
        
} else {
        
            
// return false
            // note that we do not set the error level as it has been already set
            // by the create_image_from_source_file() method earlier
            
return false;
            
        }
        
    }