2

Great to be part of this forum.

I am looking for a compression event that has a trigger and execute during upload process itself - here I am trying to compress a photo during upload using 'file-upload' of jansy bootstrap.

Function:

function compress($source, $destination, $quality) {
   $info = getimagesize($source);
    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);
    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);
    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);
    imagejpeg($image, $destination, $quality);
    return $destination;
}

jansy-bootstrap code:

 $photo_name = time();
  $photo_type = $_FILES['photo']['name'];
  $photo_size = $_FILES['photo']['size'];
  $photo_tmp_name = $_FILES['photo']['tmp_name'];


   $errors     = array();
    $maxsize    = 2097152;
    $acceptable = array(
        'image/jpeg',
        'image/jpg',
        'image/gif',
        'image/png'
          );

    if(($_FILES['photo']['size'] >= $maxsize) || ($_FILES["photo"]["size"] == 0)) {
        $errors[] = 'File too large. File must be less than 2 megabytes.';
            ?><script type="text/javascript">window.history.back();</script><?php      
    }

       if(!in_array($_FILES['photo']['type'], $acceptable))    {
        $errors[] = 'Invalid file type. Only JPG, GIF and PNG types are accepted.';
         ?><script type="text/javascript">window.history.back();</script><?php   
        }

    if(count($errors) === 0) {

        $source_img = $photo_name.'.jpg';
        $source = $source_img;
        $destination_img = $photo_name.'_c.jpg';

        $d = compress($source_img, $destination_img, 90);

        move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$d );

    } else {
        foreach($errors as $error) {
            echo '<script>alert("'.$error.'");</script>';

        }

        die(); //Ensure no more processing is done
    }

Unable to figure our what went wrong - any help would be grateful...

Bala Krishnan D
  • 131
  • 1
  • 2
  • 11

0 Answers0