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 4

Recommended Posts

Script para inserir legenda em imagem

 

<?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 )
       );
}
/**
*  Insere texto na imagem
*  @param resource $image O identificador da imagem
*  @param string $text A string a ser escrita
*  @param int $font_size Pode ser 1, 2, 3, 4, 5 para as fontes embutidas na
*  codificação latin2 (aonde números maiores correspondem a fontes mais largas)
*  ou qualquer um dos seus própios identificadores de fonte registrado com imageloadfont( )
*  @param int|string $x Coordenada x superior à esquerda
*  @param int|string $y Coordenada y superior à esquerda
*  @param string $text_color A cor em hexadecimal da string a ser escrita
*  @param string $background [opcional] A cor de fundo em hexadecimal
*  @param string $font [opcional] O caminho para o arquivo .ttf
*  @return resource O identificador da imagem modificada
*/
function subtitle( $image, $text, $font_size = 5, $x = 0, $y = 0, $text_color = '#000', $background = null, $font = null )
{
   error_image( $image );
   $x           = ( is_numeric( $x ) ) ? ( int ) $x : ( string ) $x;
   $y           = ( is_numeric( $y ) ) ? ( int ) $y : ( string ) $y;
   $font_size   = ( int ) ( $font_size > 5 ) ? $font_size : 5;
   $text        = ( string ) $text;
   $font        = ( ! is_null( $font ) ) ? ( string ) $font : null;
   $text_color  = hex_rgb( $text_color );
   $text_color  = imagecolorallocate( $image, $text_color[ 'r' ], $text_color[ 'g' ], $text_color[ 'b' ] );

   if( ! is_null( $font ) )
   {
       $dimen_text  = imagettfbbox( $font_size, 0, $font, $text );
       $width_text  = $dimen_text[ 4 ];
       $height_text = $font_size; 
   }
   else
   {
       $width_text  = ( imagefontwidth( $font_size ) * strlen( $text ) );
       $height_text = imagefontheight( $font_size );  
   }

   if( is_string( $x ) and is_string( $y ) )
   {
       $image_width  = imagesx( $image );
       $image_height = imagesy( $image );

       switch( strtoupper( sprintf( '%s-%s', $x, $y ) ) )
       {
           case( 'TOP-LEFT' ):
               $x = 0;
               $y = 0;
               break;
           case( 'TOP-CENTER' ):
               $x = ( ( $image_width - $width_text ) / 2 );
               $y = 0;
               break;
           case( 'TOP-RIGHT' ):
               $x = ( $image_width - $width_text );
               $y = 0;
               break;
           case( 'MIDDLE-LEFT' ):
               $x = 0;
               $y = ( ( $image_height / 2 ) - ( $height_text / 2 ) );
               break;
           case( 'MIDDLE-CENTER' ):
               $x = ( ( $image_width - $width_text ) / 2 );
               $y = ( ( $image_height / 2 ) - ( $height_text / 2 ) );
               break;
           case( 'MIDDLE-RIGHT' ):
               $x = ( $image_width - $width_text );
               $y = ( ( $image_height / 2) - ( $height_text / 2 ) );
               break;
           case( 'DOWN-LEFT' ):
               $x = 0;
               $y = ( $image_height - $height_text );
               break;
           case( 'DOWN-CENTER' ):
               $x = ( ( $image_width - $width_text ) / 2 );
               $y = ( $image_height - $height_text );
               break;
           case( 'DOWN-RIGHT' ):
               $x = ( $image_width - $width_text );
               $y = ( $image_height - $height_text );
               break;
           default:
               $x = 0;
               $y = 0;
               break;
       }
   }

   $x   = ( int ) $x;
   $y   = ( int ) $y;
   $img = imagecreatetruecolor( $width_text, $height_text );

   if( ! is_null( $background ) )
   {
       $background  = hex_rgb( $background );
       $background  = imagecolorallocate( $img, $background[ 'r' ], $background[ 'g' ], $background[ 'b' ] );
       imagefill( $img, 0, 0, $background );
   }

   ( is_null( $background ) ) ? imagecopymerge( $image, $img, $x, $y, 0, 0, $width_text, $height_text, 0 ) : imagecopy( $image, $img, $x, $y, 0, 0, $width_text, $height_text );
   ( is_null( $font ) ) ? imagestring( $image, $font_size, $x, $y, $text, $text_color ) : imagettftext( $image, $font_size, 0, $x, ( $y + $font_size ), $text_color, $font, $text );

   return $image;
}
?>

 

Teste:

<?php

$image = subtitle( imagecreatefromjpeg( 'Chibi_Kakashi.jpg' ), 'Test', 50, 'MIDDLE', 'CENTER', '#00FF00', null, 'arial.ttf' );
// ou
//$image = subtitle( imagecreatefromjpeg( 'Chibi_Kakashi.jpg' ), 'Test', 20, 100, 50, '#CCC', '#FFF' );

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 = subtitle( imagecreatefromjpeg( 'Chibi_Kakashi.jpg' ), 'Test', 20, 'MIDDLE', 'CENTER', '#000', '#FFF', 'arial.ttf' );
imagejpeg( $image, 'caminho/nome_da_imagem.jpg', 100 );
?>

 

Na parte 5 vamos ver como inserir marca d'água na imagem

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.