Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Neste tópico compartilho com vocês algumas funções que criei para facilitar o trabalho com imagens.
Críticas e sugestões seram muito bem vindas.
A utilização das funções é muito simples:
PHP
<?php//criando objeto que conterá a imagem
$image = imagecreatefromjpeg("imagem.jpg");
//executando a função para redimencionar
$image = imageResize($image,320,240);
//salvando a imagem
imagejpeg($image,"imagem.jpg",80);
?>
Simples não... http://forum.imasters.com.br/public/style_emoticons/default/natal_happy.gif
Redimencionar imagem
PHP
<?php/*
$origem = objeto que contém a imagem
$width = largura da nova imagem
$height = altura da nova imagem
*/
function imageResize($origem,$width,$height){
$originalWidth = imagesx($origem);
$originalHeight = imagesy($origem);
$escala=[min](http://br.php.net/min)($width/$originalWidth,$height/$originalHeight);
$novoWidth=[floor](http://br.php.net/floor)($escala*$originalWidth);
$novoHeight=[floor](http://br.php.net/floor)($escala*$originalHeight);
if(function_exists('imagecopyresampled')){
if(function_exists('imageCreateTrueColor')){
$novaImagem = imagecreatetruecolor($novoWidth,$novoHeight);
} else {
$novaImagem = imagecreate($novoWidth,$novoHeight);
}
if(!@imagecopyresampled($novaImagem,$origem,0,0,0,0,$novoWidth,$novoHeight,$originalWidth,$originalHeight)){
imagecopyresized($novaImagem, $origem, 0, 0, 0, 0, $novoWidth, $novoHeight,$originalWidth,$originalHeight);
}
} else {
$novaImagem = imagecreate($novoWidth,$novoHeight);
imagecopyresized($novaImagem,$origem,0,0,0,0,$novoWidth,$novoHeight,$originalWidth,$originalHeight);
}
return $novaImagem;
}
?>
Inserir texto em imagem
PHP
<?php/*
$srcImage = objeto que contém a imagem
$font = tamanho da fonte
$x = posição X, podendo ser um valor numérico ou "left","center","right"
$y = posição Y, podendo ser um valor numérico ou "top","middle","bottom"
$text = texto que será inserido
$str = espessura do contorno do texto
$cor1? = cor RGB para o texto
$cor2? = cor RGB para o contorno
*/
function imageText($srcImage,$font,$x,$y,$text,$str,$corR1,$corG1,$corB1,$corR2,$corG2,$corB2){
$w = imagesx($srcImage);
$h = imagesy($srcImage);
if(($x)){
switch ($x) {
case 'left':
$x = $str;
break;
case 'center':
$x = $w/2;
break;
case 'right':
$x = $w;
break;
default :
$x = $str;
break;
}
}
if(($y)){
switch ($y) {
case 'top':
$y = $str;
break;
case 'middle':
$y = $h/2;
break;
case 'bottom':
$y = $h;
break;
default :
$y = $str;
break;
}
}
$textWidth = [strlen](http://br.php.net/strlen)($text)*imagefontwidth($font);
$textHeight = imagefontheight($font);
if($x+$textWidth>$w){ $x -= $textWidth+$str; }
if($y+$textHeight>$h){ $y -= $textHeight+$str; }
$cor1 = imagecolorallocate($srcImage,$corR1,$corG1,$corB1);
$cor2 = imagecolorallocate($srcImage,$corR2,$corG2,$corB2);
for($i=0;$i<=$str;$i++){
imagestring($srcImage,$font,$x-$i,$y,$text,$cor2);
imagestring($srcImage,$font,$x+$i,$y,$text,$cor2);
imagestring($srcImage,$font,$x,$y-$i,$text,$cor2);
imagestring($srcImage,$font,$x,$y+$i,$text,$cor2);
imagestring($srcImage,$font,$x-$i,$y-$i,$text,$cor2);
imagestring($srcImage,$font,$x+$i,$y+$i,$text,$cor2);
imagestring($srcImage,$font,$x+$i,$y-$i,$text,$cor2);
imagestring($srcImage,$font,$x-$i,$y+$i,$text,$cor2);
}
imagestring($srcImage,$font,$x,$y,$text,$cor1);
return $srcImage;
}
?>
Juntar 2 imagens
PHP
<?php/*
$dst_image = objeto que contém a imagem original
$src_image = objeto que contém a imagem a ser inserida
$position = onde a imagem deverá ser inserida [top-right,top-left,bottom-right,bottom-left,center,top,bottom,left,right]
*/
function imageLogo($dst_image,$src_image,$position){
$dst_w = imagesx($dst_image);
$dst_h = imagesy($dst_image);
$src_w = imagesx($src_image);
$src_h = imagesy($src_image);
imagealphablending($dst_image,true);
imagealphablending($src_image,true);
switch ($position) {
case 'top-right':
imagecopy($dst_image,$src_image,($dst_w-$src_w), 0, 0, 0, $src_w, $src_h);
break;
case 'top-left':
imagecopy($dst_image, $src_image, 0, 0, 0, 0, $src_w, $src_h);
break;
case 'bottom-right':
imagecopy($dst_image, $src_image, ($dst_w-$src_w), ($dst_h-$src_h), 0, 0, $src_w, $src_h);
break;
case 'bottom-left':
imagecopy($dst_image, $src_image, 0 , ($dst_h-$src_h), 0, 0, $src_w, $src_h);
break;
case 'center':
imagecopy($dst_image, $src_image, (($dst_w/2)-($src_w/2)), (($dst_h/2)-($src_h/2)), 0, 0, $src_w, $src_h);
break;
case 'top':
imagecopy($dst_image, $src_image, (($dst_w/2)-($src_w/2)), 0, 0, 0, $src_w, $src_h);
break;
case 'bottom':
imagecopy($dst_image, $src_image, (($dst_w/2)-($src_w/2)), ($dst_h-$src_h), 0, 0, $src_w, $src_h);
break;
case 'left':
imagecopy($dst_image, $src_image, 0, (($dst_h/2)-($src_h/2)), 0, 0, $src_w, $src_h);
break;
case 'right':
imagecopy($dst_image, $src_image, ($dst_w-$src_w), (($dst_h/2)-($src_h/2)), 0, 0, $src_w, $src_h);
break;
}
return $dst_image;
}
?>
Alterar o contraste de uma imagem
PHP
<?php/*
$img = objeto que contém a imagem
$percent = valor de contraste podendo ser positivo ou negativo
*/
function imageContrast($img, $percent){
$x = imagesx($img);
$y = imagesy($img);
$dest = imagecreatetruecolor($x, $y);
$percent = $percent / 100;
for($i=0; $i<$x; $i++){
for($j=0; $j<$y; $j++){
$rgb = imagecolorat($img, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$r = $percent > 1 ? [min](http://br.php.net/min)([floor](http://br.php.net/floor)($r * $percent), 255) : [max](http://br.php.net/max)([ceil](http://br.php.net/ceil)($r * $percent), 0);
$g = $percent > 1 ? [min](http://br.php.net/min)([floor](http://br.php.net/floor)($g * $percent), 255) : [max](http://br.php.net/max)([ceil](http://br.php.net/ceil)($g * $percent), 0);
$b = $percent > 1 ? [min](http://br.php.net/min)([floor](http://br.php.net/floor)($b * $percent), 255) : [max](http://br.php.net/max)([ceil](http://br.php.net/ceil)($b * $percent), 0);
imagesetpixel($dest, $i, $j, imagecolorallocate($dest, $r, $g, $b));
}
}
return $dest;
}
?>Carregando comentários...