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