gpt4 book ai didi

php 图像压缩 gif 无损动画

转载 作者:行者123 更新时间:2023-12-04 10:59:40 27 4
gpt4 key购买 nike

  • php 图像压缩 gif 无损动画
  • gif 无损动画
  • 哪个是在不损失质量的情况下减小图像大小的最佳 PHP 方法

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


  • 在不降低质量的情况下减小图像大小的方法 *

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

    最佳答案

    您正在从 gif 创建图像。这就是为什么它失去了动画。因为 gif 是一组图像,所以将它们压缩成图像只会保留该组的第一张图像。

    如果您想保持质量并压缩它,请选择 jpeg。阅读本文 post关于缩小图像尺寸并在确定技术后,使用 ImageMagick

    最后尝试通过保留动画来压缩 gif 图像。但是,当您压缩 gif 图像时,它们往往会降低质量。

    imagetruecolortopalette($img, false, 16);  //  compress to 16 colors in gif palette (change 16 to anything between 1-256)

    关于php 图像压缩 gif 无损动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58908048/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com