Question:
I need help perfecting a script. I usually want to add to this script:
if(isset($_POST['action']) && $_POST['action'] == 'get_photo') {
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
$file_name = $_FILES['userImage']['name'];
$sourcePath = $_FILES['userImage']['tmp_name'];
preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $file_name, $ext);
// Gera um nome único para a imagem
$nome_imagem = md5(uniqid(time())) . "." . $ext[1];
// Caminho de onde ficará a imagem
$desired_dir="../image/posts/";
$caminho_imagem = $desired_dir.$nome_imagem;
if(move_uploaded_file($sourcePath,$caminho_imagem)) {
echo '<img src="/image/posts/'.$nome_imagem.'" width="100px" height="100px" />';
}
}
}
}
A system or way to reduce image quality, as I'm working on a site like Instagram and I need the images to be very light, as Facebook does. Captures the image and converts it to JPG and reduces the image size to be MB to KB
I thank you all…
Answer:
See this script, it converts images to jpeg.
The imagemOriginal
parameter is the path where the original image is, the imagemFinal
parameter is the final name after conversion and the qualidade
parameter is an integer value from 0 to 100, where 0 is low quality and 100 maximum quality, the closer to 100 , the "heavier" the image will be.
function converterImagem($imagemOriginal, $imagemFinal, $qualidade) {
// jpg, png, gif or bmp
$exploded = explode('.',$imagemOriginal);
$ext = $exploded[count($exploded) - 1];
if (preg_match('/jpg|jpeg/i',$ext))
$imagemTmp=imagecreatefromjpeg($imagemOriginal);
else if (preg_match('/png/i',$ext))
$imagemTmp=imagecreatefrompng($imagemOriginal);
else if (preg_match('/gif/i',$ext))
$imagemTmp=imagecreatefromgif($imagemOriginal);
else if (preg_match('/bmp/i',$ext))
$imagemTmp=imagecreatefrombmp($imagemOriginal);
else
return 0;
// O parâmetro "qualidade" é um valor de 0 (baixa) até 100 (alta)
imagejpeg($imagemTmp, $imagemFinal, $qualidade);
imagedestroy($imagemTmp);
return 1;
}