Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

karimali1337's avatar

Reduce image size while uploading

i've this script it helps me to move the uploaded image and its working well,i'm trying to find a solution to reduce the image size while uploading it is this available ? here's the code if someone can help

if ((isset($_FILES['photo']['name']) && !empty($_FILES['photo']['name']))) {
                        $ds = DIRECTORY_SEPARATOR;
                        $tempFile = $_FILES['photo']['tmp_name'];//3
                        $file_extension = pathinfo($_FILES['photo']['name'] , PATHINFO_EXTENSION);   /// get EXTENSION from file
                        $nameFile = rand() . '-' . strtotime(date("Y-m-d h:i:s")) . '.' . $file_extension;  /// filename with EXTENSION
                        $targetPath = realpath(dirname(_FILE_) . '/..'. '/..'. '/..') . $ds . 'includes/img/shops/';  // path file
                        $targetFile = $targetPath . $nameFile;  // path file with filename

                    
                        move_uploaded_file($tempFile, $targetFile); // move file to path folder
                    } else {
                        $nameFile = NULL;
                    }
0 likes
5 replies
aurelianspodarec's avatar

@sr57 Is this something like TinyPNG? I want to use TinyPNG or such, to minimalize my files, and instead of doing it manually to TinnyPng, might as well compress the image on uploading.

I wonder though, if I use something else than TinyPNG, that means the quality/size might be different as well right. So would need to do possibly test to get the best quality with the lowest file size I suppose.

Please or to participate in this conversation.