b85411
b85411

Reputation: 10010

Simplest possible way to resize images in PHP

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

So at this point, I can assume unless $photo is null then it contains the full path to an image on my server.

What I want to do is:

What is the easiest way to do this in PHP?

If possible, I'd love to do this without having to enable any components or touch php.ini but I understand if this is unavoidable...

Thanks

Upvotes: 2

Views: 65

Answers (1)

Pradeep Sanjaya
Pradeep Sanjaya

Reputation: 1846

This is not the simplest solution but using Imagine library is reducing development time significantly. You should enable GD or Imagick from your php.ini. You can use following class inside your 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);
    }


}
?>

Usage

$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();

Output

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

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

In your class

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

}
?>

Upvotes: 2

Related Questions