Compress / compress image after upload

Asked

Viewed 1,355 times

3

I need help perfecting a script.. In general I 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, because I’m working on a site like Instagram and I need the images to be very light, with Facebook does. Captures the image and converts she to JPG and reduces the size of the image MB for KB

From now on I thank you all...

1 answer

1


See this script, it converts images to jpeg.

The parameter imagemOriginal is the path where the original image is, the parameter imagemFinal is the final name after conversion and the parameter qualidade is an integer value from 0 to 100, where 0 is low quality and 100 maximum quality, the closer to 100, the more "heavy" 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;
}
  • Right, I used a script similar to this, but the script will be run in the folder localweb PROJECT Conn server.php and it saves the image in the same folder where php was run, when I need it to send to *localweb PROJECT image posts* , and another using your script, how to ask if it was sent to such a folder as mine if(move_uploaded_file($sourcePath,$caminho_imagem))

  • @When this script is set to the path for the image. If you want to save your image in a different location, use the function move_uploaded_file to change the directory image, after changing the location, call the script by passing the current image directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.