0

How can i compress the file size of each image file and move to folder using foreach. How can I achieve this with gd extension in php?

This is my code:

if($_POST['image_form_submit'] == 1) {
    $galleryGroup = filter_var($_POST['galleryGroup'], FILTER_SANITIZE_NUMBER_INT);

    $images_arr = array();

    foreach ($_FILES['images']['name'] as $key => $val)
    {
        $image_name = $_FILES['images']['name'][$key];
        $tmp_name   = $_FILES['images']['tmp_name'][$key];
        $size       = $_FILES['images']['size'][$key];
        $type       = $_FILES['images']['type'][$key];
        $error       = $_FILES['images']['error'][$key];

        //Upload Temporary Directory
        $target_dir = "../uploads/gallery/";

        $target_file = $target_dir . $_FILES['images']['name'][$key];




        if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $target_file))
        {
            $images_arr[] = $target_file;
        }
        $sqlMaxSO = "SELECT MAX(so) as maxSO FROM gallery_listing";

        $Maxres = mysqli_query($con, $sqlMaxSO);

        $row = mysqli_fetch_assoc($Maxres);

        $maxSO = $row['maxSO'];


        $sql = "INSERT INTO gallery_listing (idGroup, imgImage, so, active) VALUES (" . $galleryGroup . " , '" .  $image_name. "', '" . $maxSO . "', 1);";

        $res = mysqli_query($con, $sql);
    }
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • You need to create a new image based on the first. If you create it as a jpeg, you can not only resize the image, but also control the compression quality when you save it: http://php.net/manual/en/function.imagejpeg.php – M. Eriksson Jun 05 '17 at 11:15
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jun 05 '17 at 11:19

0 Answers0