Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Kakashi_Hatake

Manipulando e redimensionando imagens com PHP - Parte 2

Recommended Posts

Agora algumas funções para redimensionar e rotacionar uma imagem podendo adicionar uma cor de fundo.

 

<?php
/**
*  Verifica se é um resource identificador da imagem
*  @param resource $image O identificador da imagem
*  @return void
*/
function error_image( $image )
{    
   if( ! is_resource( $image ) )
   {
       trigger_error( sprintf( '%s is not an image resource identifier!', $image ), E_USER_ERROR );
   }   
}
/**
*  Converte as cores de hexadecimal para RGB
*  @param string $color A cor em hexadecimal da string a ser escrita
*  @return array Um array associativo com as cores RGB
*/
function hex_rgb( $color = '#FFFFFF' )
{
   if( ! is_string( $color ) )
   {
       trigger_error( 'Insert hexadecimal string!', E_USER_ERROR );
   }

   $color = substr( preg_replace( '/[^0-9A-Fa-f]/', null, ( string ) $color ), 0, 6 );

   if( strlen( $color ) == 3 )
   {
       list( $r, $g, $b ) = array_map( 'str_repeat', str_split( $color ), array_fill( 0, 3, 2 ) );
   }
   elseif( strlen( $color ) <= 6 )
   {
       list( $r, $g, $b ) = str_split( str_pad( $color, 6, 'F' ), 2 );
   }

   return array(
       'r' => hexdec( $r ),
       'g' => hexdec( $g ),
       'b' => hexdec( $b )
       );
}
/**
*  Redimensiona a imagem recortando
*  @param resource $image O identificador da imagem
*  @param int $new_width A nova largura
*  @param int $new_height A nova altura
*  @return resource O identificador da imagem modificada
*/
function resize_crop( $image, $new_width, $new_height )
{
   error_image( $image );
   $new_width  = ( int ) $new_width;
   $new_height = ( int ) $new_height;
   $old_width  = imagesx( $image );
   $old_height = imagesy( $image );
   $hm         = ( $old_height / $new_width );
   $wm         = ( $old_width / $new_height );
   $h_height   = ( $new_height / 2 );
   $h_width    = ( $new_width / 2 );

   if( $wm > $hm )
   {
       $position_crop[ 2 ] = ( $old_width / $hm );
       $position_crop[ 3 ] = $new_height;
       $position_crop[ 0 ] = ( ( $position_crop[ 2 ] / 2 ) - $h_width );
       $position_crop[ 1 ] = 0;
   }
   elseif( $wm <= $hm )
   {
       $position_crop[ 2 ] = $new_width;
       $position_crop[ 3 ] = ( $old_height / $wm );
       $position_crop[ 0 ] = 0;
       $position_crop[ 1 ] = ( ( $position_crop[ 3 ] / 2 ) - $h_height );
   }

   $img = imagecreatetruecolor( $new_width, $new_height );
   imagecopyresampled( $img, $image, - $position_crop[ 0 ], - $position_crop[ 1 ], 0, 0, $position_crop[ 2 ], $position_crop[ 3 ], $old_width, $old_height );

   return $img;
}
/**
*  Redimensiona a imagem proporcionalmente
*  @param resource $image O identificador da imagem
*  @param int $new_width A nova largura
*  @param int $new_height A nova altura
*  @return resource O identificador da imagem modificada
*/
function resize_relative( $image, $new_width, $new_height )
{
   error_image( $image );
   $new_width  = ( int ) $new_width;
   $new_height = ( int ) $new_height;
   $old_width  = imagesx( $image );
   $old_height = imagesy( $image );
   $ratio_orig = ( $old_width / $old_height );

   if( $new_width / $new_height > $ratio_orig )
   {
       $dif_w = ( $new_height * $ratio_orig );
       $dif_h = $new_height;
   }
   else
   {
       $dif_w = $new_width;
       $dif_h = ( $new_width / $ratio_orig );
   }

   $img = imagecreatetruecolor( $dif_w, $dif_h );
   imagecopyresampled( $img, $image, 0, 0, 0, 0, $dif_w, $dif_h, $old_width, $old_height );

   return $img;
}
/**
*  Redimensionamento com preenchimento
*  de cor de fundo definida
*  @param resource $image O identificador da imagem
*  @param int $new_width A nova largura
*  @param int $new_height A nova altura
*  @param string $background [opcional] A cor de fundo em hexadecimal
*  @return resource O identificador da imagem modificada
*/
function resize_fill( $image, $new_width, $new_height, $background = '#FFFFFF' )
{
   error_image( $image );
   $new_width  = ( int ) $new_width;
   $new_height = ( int ) $new_height;
   $old_width  = imagesx( $image );
   $old_height = imagesy( $image );
   $img        = imagecreatetruecolor( $new_width, $new_height );
   $RGB        = hex_rgb( $background );
   $background = imagecolorallocate( $img, $RGB[ 'r' ], $RGB[ 'g' ], $RGB[ 'b' ] );
   imagefill( $img, 0, 0, $background );
   $dif_x = $dif_w = $new_width;
   $dif_y = $dif_h = $new_height;

   if( ( $old_width / $new_width ) > ( $old_height / $new_height ) )
   {
       $factor = ( $old_width / $new_width );
   }
   else
   {
       $factor = ( $old_height / $new_height );
   }

   $dif_w = ( $old_width / $factor );
   $dif_h = ( $old_height / $factor );
   $dif_x = ( ( $dif_x - $dif_w ) / 2 );
   $dif_y = ( ( $dif_y - $dif_h ) / 2 );
   imagecopyresampled( $img, $image, $dif_x, $dif_y, 0, 0, $dif_w, $dif_h, $old_width, $old_height );

   return $img;
}
/**
*  Redimensionamento simples
*  @param resource $image O identificador da imagem
*  @param int $new_width A nova largura
*  @param int $new_height A nova altura
*  @return resource O identificador da imagem modificada
*/
function resize( $image, $new_width, $new_height )
{
   error_image( $image );
   $new_width  = ( int ) $new_width;
   $new_height = ( int ) $new_height;
   $old_width  = imagesx( $image );
   $old_height = imagesy( $image );
   $img        = imagecreatetruecolor( $new_width, $new_height );
   imagecopyresampled( $img, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height );

   return $img;
}
/**
*  Rotaciona uma imagem com um ângulo dado
*  @param resource $image O identificador da imagem
*  @param int $degrees O grau de rotação da imagem
*  @param string $background [opcional] A cor de fundo em hexadecimal
*  @return resource O identificador da imagem modificada
*/
function rotation( $image, $degrees, $background = '#FFFFFF' )
{
   error_image( $image );
   $RGB        = hex_rgb( $background );
   $background = imagecolorallocate( $image, $RGB[ 'r' ], $RGB[ 'g' ], $RGB[ 'b' ] );
   $image      = imagerotate( $image, ( int ) $degrees, $background );
   imagealphablending( $image, true );
   imagesavealpha( $image, true );

   return $image;
}
?>

 

Teste:

<?php

$image = rotation( resize_fill( imagecreatefromjpeg( 'Chibi_Kakashi.jpg' ), 300, 300, '#F00' ), 120, '#00FF99' );

header( 'Content-type: image/jpg' );
imagejpeg( $image, null, 100 );
imagedestroy( $image );
exit;

?>

 

Outra coisa, essas funções vão te retornar apenas o resource, se você quiser salvar a imagem alterada vai ter que usar:

:seta: imagejpeg

:seta: imagepng

:seta: imagegif

 

Exemplo de uso:

<?php

$image = rotation( resize_fill( imagecreatefromjpeg( 'Chibi_Kakashi.jpg' ), 300, 300, '#F00' ), 120, '#00FF99' );
imagejpeg( $image, 'caminho/nome_da_imagem.jpg', 100 );

?>

 

Na parte 3 vamos utilizar filtros

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.