Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
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 );
}
} 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 )
);
} 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