如何使用PHP上传时压缩图像大

根据图像的数量,高质量的图像需要花费时间加载到网页上。

您可以在上传时创建多种尺寸的图像文件,并在需要时使用低质量和高质量的图像。

减小图像尺寸时,也会降低图像质量。

最好的例子是WordPress,每当上传图像文件时,它将生成不同大小的文件。这些图像将根据要求在主题中使用。

在本教程中,我将展示如何在使用PHP 上传时压缩图像大小。

下载地址:
https://makitweb.com/how-to-compress-image-size-while-uploading-with-php/

代码文件index.php 主要包括如下:

<!doctype html>
<html>
    <head>
        <title>How to Compress Image size while Uploading with PHP</title>
    </head>
	<body>
		<?php
        if(isset($_POST['upload'])){

            // Getting file name
            $filename = $_FILES['imagefile']['name'];
         
            // Valid extension
            $valid_ext = array('png','jpeg','jpg');

            // Location
            $location = "images/".$filename;

            // file extension
            $file_extension = pathinfo($location, PATHINFO_EXTENSION);
            $file_extension = strtolower($file_extension);

            // Check extension
            if(in_array($file_extension,$valid_ext)){  

                // Compress Image
                compressImage($_FILES['imagefile']['tmp_name'],$location,90);

            }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);

        }

        ?>

        <!-- Upload form -->
        <form method='post' action='' enctype='multipart/form-data'>
            <input type='file' name='imagefile' >
            <input type='submit' value='Upload' name='upload'>    
        </form>
	</body>
</html>

以及存放图片的images文件夹

效果如下:

代码分析:

1. HTML

在里面创建一个<input type='file'>元素<form>并提交按钮。

完成的代码

<form method ='post'action =''enctype ='multipart / form-data'>
  <input type ='file'name ='imagefile'>
  <input type ='submit'value ='Upload'name ='upload'> 
</ FORM>

2. PHP

创建images用于存储图像文件的目录。

自定义功能

创建一个compressImage()压缩JPEG,PNG和GIF图像的功能。

该功能需要3个参数 –
1.源
2.目标
3.文件质量

调用imagecreatefromjpeg($source)imagecreatefromgif($source)imagecreatefrompng($source)根据$info['mime']值创建新图像。

执行imagejpeg()将图像存储到目标的方法。这里,第三个参数quality是可选的。它取值为0 – 100,默认值为75。

表格提交

将位置设置为images目录并检查文件扩展名。如果它是有效的,则调用compressImage()函数,其中通$_FILES['imagefile']['tmp_name']$location60

完成的代码

<?php
if(isset($_POST['upload'])){

  // Getting file name
  $filename = $_FILES['imagefile']['name'];
 
  // Valid extension
  $valid_ext = array('png','jpeg','jpg');

  // Location
  $location = "images/".$filename;

  // file extension
  $file_extension = pathinfo($location, PATHINFO_EXTENSION);
  $file_extension = strtolower($file_extension);

  // Check extension
  if(in_array($file_extension,$valid_ext)){

    // Compress Image
    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);

}

?>

总结:

在示例中,我只存储了压缩图像,而不是原始尺寸图像。您可以在上传时存储两种版本的图像。

缩小尺寸时调整图像质量。

参考链接:

Leave a Reply

您的邮箱地址不会被公开。 必填项已用 * 标注