Boas. Ao nível de direitos sobre o código, devo remeter-vos para o meu primeiro artigo desta rubrica.
Nesta semana apresento-vos uma função cujo objectivo é receber uma imagem e redimensioná-la, caso necessário, com possibilidade de manter transparência caso a original a possua. Também podem redimensioná-la de forma a manter a sua proporção ou não.
<?php
function saveImage($file,$newfile,$maxwidth = 250,$maxheight = 250,$proportional = true)
{
$original = $file['tmp_name'];//-- Original Image
$ext_tmp = getimagesize($original);
switch($ext_tmp[2])
{
case 1 : $ext = "gif"; break;
case 2 : $ext = "jpg"; break;
case 3 : $ext = "png"; break;
default : $ext = "jpg";
}
switch ($ext)
{
case 'png' : $header = "image/png"; break;
case 'gif' : $header = "image/gif"; break;
case 'jpg' :
case 'jpeg' :
default : $header = "image/jpeg";
}
header('Content-type: '.$header);
list($width, $height) = getimagesize($original);
if ($proportional)
{
if ($width < $maxwidth && $height < $maxheight)
{
$newwidth = $width;
$newheight = $height;
} else {
if ($width < $height)
{
if ($height >= $maxheight)
{
$size = round($height/$maxheight,2);
$newwidth = round($width/$size);
$newheight = round($height/$size);
} else {
$size = round($width/$maxwidth,2);
$newwidth = round($width/$size);
$newheight = round($height/$size);
}
} else {
if ($width >= $maxwidth)
{
$size = round($width/$maxwidth,2);
$newwidth = round($width/$size);
$newheight = round($height/$size);
} else {
$size = round($height/$maxheight,2);
$newwidth = round($width/$size);
$newheight = round($height/$size);
}
}
}
} else {
$newwidth = $maxwidth;
$newheight = $maxheight;
}
$img = imagecreatetruecolor($newwidth, $newheight);
switch ($ext)
{
case 'png' :
{
$image = imagecreatefrompng($original);
$transparency = true;
} break;
case 'gif' :
{
$image = imagecreatefromgif($original);
$transparency = true;
} break;
case 'jpg' :
case 'jpeg' :
default :
{
$image = imagecreatefromjpeg($original);
$transparency = false;
} break;
}
if($transparency)
{
if ($ext == "png")
{
imagealphablending($img, false);
$colorTransparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $colorTransparent);
imagesavealpha($img, true);
} elseif ($ext == "gif") {
$trnprt_indx = imagecolortransparent($image);
if ($trnprt_indx >= 0)
{
$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
$trnprt_indx = imagecolorallocate($img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($img, 0, 0, $trnprt_indx);
imagecolortransparent($img, $trnprt_indx);
}
}
} else {
imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));
}
imagecopyresampled($img, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
switch ($ext)
{
case 'png' :
{
if (imagepng($img, $newfile))
{
//header('Content-Type: text/html; charset=iso-8859-1');
return true;
}
} break;
case 'gif' : {
if (imagegif($img, $newfile))
{
//header('Content-Type: text/html; charset=iso-8859-1');
return true;
}
} break;
case 'jpg' :
case 'jpeg' :
default :
{
if (imagejpeg($img, $newfile, 90))
{
//header('Content-Type: text/html; charset=iso-8859-1');
return true;
}
} break;
}
//header('Content-Type: text/html; charset=iso-8859-1');
return false;
}
?>
Modo de utilização:
É bastante simples e intuitivo, segue um exemplo de como pegar numa imagem de que se fez upload através de um formulário para 150×150 px, mantendo as proporções da imagem.
<?php $file = $_FILES['imagem']; $newfile = “/caminho/para/imagem.png”;//-- Estou a supôr que é .png, podemos e devemos obviamente determinar se o é antes de lhe dar o nome que vai ter no servidor. if (saveImage($file,$newfile,150,150)) echo “OK”; else echo “ERRO”; ?>
NOTA: A função só aceita imagens .jpg, .jpeg, .gif e .png
Qualquer dúvida ou sugestão, estão completamente à vontade.
Como vou entrar em exames, etc., se quiserem enviar as vossas propostas para esta rubrica, agradeço, pois será difícil escrevê-la até ao final de Julho.