【问题标题】:I need to compress the thumb images | PHP我需要压缩拇指图像 | PHP
【发布时间】:2022-01-27 04:43:14
【问题描述】:

你好 Stackoverflowers,

我已经成功压缩了上传的主图,但是不知道怎么用同样的功能压缩缩略图。

代码主要用途如下:

1- 上传图片

2- 重命名

3- 压缩

4- 创建缩略图

5- 压缩缩略图

我的问题是数字 5,我不知道如何压缩缩略图。

以下是我的 php 和 HTML 代码:

if (isset($_POST["submit"])) {
    if (is_array($_FILES)) {
        if ($_FILES['image']['size'] <= 5000000) {
            $file = $_FILES['image']['tmp_name'];

            $sourceProperties = getimagesize($file);
            $fileNewName = "[" . "999" . "]-" . time();
            $folderPath = "uploads/";
            $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
            $imageType = $sourceProperties[2];


            $data = getimagesize($file);
            $width = $data[0];
            $height = $data[1];

            switch ($imageType) {

                case IMAGETYPE_PNG:
                    $imageResourceId = imagecreatefrompng($file);
                    $targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
                    imagepng($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
                    break;

                case IMAGETYPE_GIF:
                    $imageResourceId = imagecreatefromgif($file);
                    $targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
                    imagegif($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
                    break;

                case IMAGETYPE_JPEG:
                    $imageResourceId = imagecreatefromjpeg($file);
                    $targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
                    imagejpeg($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
                    break;

                default:
                    echo "Invalid Image type.";
                    exit;
                    break;
            }

            compressImage($file, $folderPath . $fileNewName . "." . $ext, 60);

            echo "Image Resize Successfully.";
        } else {
            echo "too big!";
        }
    }
}

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


function imageResize($imageResourceId, $width, $height)
{

    $targetWidth = (20 / 100) * $width;

    $targetHeight = (20 / 100) * $height;

    echo $targetWidth . " x " . $targetHeight . " <br> ($width x $height)";

    $targetLayer = imagecreatetruecolor($targetWidth, $targetHeight);
    imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);

    return $targetLayer;
}

HTML


    <div class="container">
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="image" />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </div>

    <br><br>
    <a href="index-2.php">Reload Page</a>


请原谅我的愚蠢问题,因为我认为这是一个非常简单的问题,但我几个月前才开始使用 php 进行开发。

提前感谢您,非常感谢您的帮助。

【问题讨论】:

  • 与全尺寸图像相同的方式
  • 我知道同样的方法,我只是不知道如何,主图像由compressImage($file, $folderPath . $fileNewName . "." . $ext, 60);压缩和上传我不知道如何使用与创建相同的功能缩略图。
  • 好吧,您编写的函数只是降低了质量,您可以在案例语句中的每个 imageCreateFromXXX 调用上执行此操作

标签: php image file-upload gd image-compression


【解决方案1】:

这听起来像XY-Problem;因为你所做的只是改变质量。

如果您需要低质量的缩略图,则在创建缩略图时直接设置该设置,无需创建另一个文件。

但如果你坚持下去,那么只需将原始缩略图替换为 compressImage 函数创建的那个,比如:

$newFilePath = $folderPath . $fileNewName . "." . $ext;
compressImage($file, $newFilePath, 60);

// Replace.
unlink($file); 
rename($newFilePath, $file);

【讨论】:

    猜你喜欢
    • 2010-12-29
    • 2012-04-08
    • 2013-08-21
    • 1970-01-01
    • 2015-06-15
    • 2020-06-16
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多