Reputation: 31
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method='post' action='' enctype='multipart/form-data'>
<input type='file' name='imagefile' >
<input type='submit' value='Upload' name='upload'>
</form>
<?
if(isset($_POST['upload'])){
$filename = $_FILES['imagefile']['name'];
$valid_ext = array('png','jpeg','jpg','gif');
$location = "images/".$filename;
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);
if(in_array($file_extension,$valid_ext)){
compressImage($_FILES['imagefile']['tmp_name'],$location,60);
}else{
echo "Invalid file type.";
}
}
// Compress image
function compressImage($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);
}
?>
</body>
</html>
thod to reduce the image size without losing quality [closed]*
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method='post' action='' enctype='multipart/form-data'>
<input type='file' name='imagefile' >
<input type='submit' value='Upload' name='upload'>
</form>
<?
if(isset($_POST['upload'])){
$filename = $_FILES['imagefile']['name'];
$valid_ext = array('png','jpeg','jpg','gif');
$location = "images/".$filename;
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);
if(in_array($file_extension,$valid_ext)){
compressImage($_FILES['imagefile']['tmp_name'],$location,60);
}else{
echo "Invalid file type.";
}
}
// Compress image
function compressImage($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);
}
?>
</body>
</html>
Upvotes: 2
Views: 711
Reputation: 2629
You are creating image from gif. that why its loosing its animation. because gifs are set of images altogether, compressing them into image will only keep the first image of that set.
If you want to keep the quality and compress it go for jpeg. Read this post regarding image size reduction and after deciding on the technique, use ImageMagick
and finally try this one to compress gif image by keeping its animation. however gif images are tend to lose quality when you compress them.
imagetruecolortopalette($img, false, 16); // compress to 16 colors in gif palette (change 16 to anything between 1-256)
Upvotes: 2