Laman

Compress Image While Upload

Sharing source code compress image JPG/JPEG/PNG/GIF

the simple code
$tmp_name = $_FILES["file"]["tmp_name"];
$dir = "images";
$dest = $dir."/".$_FILES["file"]["name"];
$quality=95;//percentage quality compress image

//check image type
if ($_FILES["file"]["type"] == 'image/jpeg'){
  $image = imagecreatefromjpeg($tmp_name);
}elseif ($_FILES["file"]["type"] == 'image/gif'){
  $image = imagecreatefromgif($tmp_name);
}elseif ($_FILES["file"]["type"] == 'image/png'){
  $image = imagecreatefrompng($tmp_name);
}

//compress and save file to jpg
imagejpeg($image, $dest, $quality);
imagecreatefromjpeg($dest);

Download full source (dropbox)

No comments:

Post a Comment

Silahkan