在PHP中调整图像大小的最简单方法

时间:2015-12-04 03:04:34

标签: php

foreach ($task->getDefects() as $defect)
{
        $photo = ($defect->getPhoto() != '') ? $path($defect->getPhoto()) : null;

所以在这一点上,我可以假设,除非$photonull,否则它包含我服务器上图像的完整路径。

我想做的是:

  • 如果图像高于某个文件大小,比如100kb;然后
  • 将图像大小调整为当前大小的一小部分。比如,如果尺寸为400kb,则将其减小到当前尺寸的25%。如果是200kb,则将其减小到当前尺寸的50%等。

在PHP中执行此操作的最简单方法是什么?

如果可能的话,我喜欢这样做,而无需启用任何组件或触摸php.ini,但我明白这是否是不可避免的......

由于

1 个答案:

答案 0 :(得分:2)

这不是最简单的解决方案,但使用Imagine库可显着缩短开发时间。你应该从你的php.ini启用GD或Imagick。您可以在foreach中使用以下课程。

composer.json

{
    "require": {
        "imagine/imagine": "^0.6.3"
    }
}

ImageProcessing.php

<?php
class ImageProcessing
{
    private $resizeRatio = array(
        400 => 0.5, // 50%
        200 => 0.25 // 25%
    );

    private $filePath;

    private $originalSize;

    public function __construct($filePath)
    {
        $this->filePath = $filePath;
        list($this->originalSize['width'], $this->originalSize['height']) = getimagesize($this->filePath);
    }

    public function getOriginalSize()
    {
        return $this->originalSize;
    }

    public function getFileSizeInKb()
    {
        $imgSizeByte = filesize($this->filePath);
        return round($imgSizeByte / 1024);
    }

    public function getReducePercentageByFileSize()
    {
        $fileSize = $this->getFileSizeInKb();

        foreach ($this->resizeRatio as $size => $percentage) {
            if ($fileSize >= $size) {
                return $percentage;
            }
        }

        // return 100% if not found in size ratio
        return 1;
    }

    public function getReduceSize()
    {
        $reducePercentage = $this->getReducePercentageByFileSize();
        $reduceSize = array(
            'width' =>  $this->originalSize['width'] * $reducePercentage,
            'height'=>  $this->originalSize['height'] * $reducePercentage
        );
        return $reduceSize;
    }

    public function resize()
    {
        $reduceSize = $this->getReduceSize();
        $imagine = new Imagine\Gd\Imagine();
        $size = new Imagine\Image\Box($reduceSize['width'], $reduceSize['height']);
        $mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET;
        $imagine
            ->open($this->filePath)
            ->thumbnail($size, $mode)
            ->save($this->filePath);
    }


}
?>

用法

$filePath = 'image.png';
$imgProcessing = new ImageProcessing($filePath);
echo 'File size: ' . $imgProcessing->getFileSizeInKb() . " KB \n";
echo 'Reduce Percentage : ' . $imgProcessing->getReducePercentageByFileSize() . "\n";
echo 'Original Size : ' . print_r($imgProcessing->getOriginalSize(), true) . "\n";
echo 'ReduceSize Size : ' . print_r($imgProcessing->getReduceSize(), true) . "\n";
$imgProcessing->resize();

输出

File size: 273 KB 
Reduce Percentage : 0.25
Original Size : Array
(
    [height] => 450
    [width] => 800
)

ReduceSize Size : Array
(
    [width] => 200
    [height] => 112.5
)

在你的班级

<?php
require __DIR__ . '/vendor/autoload.php';

foreach ($task->getDefects() as $defect)
{
    $photo = ($defect->getPhoto() != '') ? $path($defect->getPhoto()) : null;
    if (!empty($photo)) {
        $image = new ImageProcessing($photo);
        $image->resize();
    }

}
?>