开发者社区> 问答> 正文

php图片压缩gif无损失动画

php图片压缩gif无损失动画 无损失的gif动画 这是减少图像大小而不损失质量的最佳PHP方法 显示代码段

可以在不损失图像质量的情况下缩小图像尺寸[关闭] *

<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>

展开
收起
社区秘书 2019-11-18 13:54:47 904 0
0 条回答
写回答
取消 提交回答
问答分类:
PHP
问答地址:
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-直面PHP微服务架构挑战-高驰涛 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载