Add white space to image using Laravel 5 intervention image to make square image
Asked Answered
D

2

8

Assume I have a favourite square size, and in this case it has 2236 px width and height.

I need to save my images in this size on my server using php intervention package.

It's not important that what is user's image size, but the point is that the image have to save with new size but the user image have to be center and middle of the square and if the picture is smaller than my favourite dimensions, it have to be stretch and if the image is bigger, it have to be compress to my dimension.

Please take a look at this picture: my plan

And these are some real examples: example 1 example 2

Does anyone any experience in this situation and do you know how can I do that?

Thanks in Advance

Dezhnev answered 4/6, 2017 at 1:52 Comment(1)
image.intervention.io/api/resizeCanvasAshleighashlen
D
5

Well, thanks to @Anton for his hint, I did this to solve my problem:

The image is horizontal rectangle, vertical rectangle or square.

I wrote these lines of code for every situation and it works great for my case

$img    = Image::make($image->getRealPath());

$width  = $img->width();
$height = $img->height();


/*
*  canvas
*/
$dimension = 2362;

$vertical   = (($width < $height) ? true : false);
$horizontal = (($width > $height) ? true : false);
$square     = (($width = $height) ? true : false);

if ($vertical) {
    $top = $bottom = 245;
    $newHeight = ($dimension) - ($bottom + $top);
    $img->resize(null, $newHeight, function ($constraint) {
        $constraint->aspectRatio();
    });

} else if ($horizontal) {
    $right = $left = 245;
    $newWidth = ($dimension) - ($right + $left);
    $img->resize($newWidth, null, function ($constraint) {
        $constraint->aspectRatio();
    });

} else if ($square) {
    $right = $left = 245;
    $newWidth = ($dimension) - ($left + $right);
    $img->resize($newWidth, null, function ($constraint) {
        $constraint->aspectRatio();
    });

}

$img->resizeCanvas($dimension, $dimension, 'center', false, '#ffffff');
$img->save(public_path("storage/{$token}/{$origFilename}"));
/*
* canvas
*/
Dezhnev answered 17/6, 2017 at 13:6 Comment(1)
the horizontal and square cases are identical, you could just get rid of square and use >= instead of > for horizontal check.Estus
E
8
<?php
$width = 2236;
$height = 2236;

$img = Image::make('image.jpg');

// we need to resize image, otherwise it will be cropped 
if ($img->width() > $width) { 
    $img->resize($width, null, function ($constraint) {
        $constraint->aspectRatio();
    });
}

if ($img->height() > $height) {
    $img->resize(null, $height, function ($constraint) {
        $constraint->aspectRatio();
    }); 
}

$img->resizeCanvas($width, $height, 'center', false, '#ffffff');
$img->save('out.jpg');
Eboni answered 4/6, 2017 at 2:9 Comment(5)
Thanks for your reply, but this script do not change original image size, just will put the original image in the center of 2236px. And what if image width was smaller than $width?Dezhnev
@Dezhnev and what should happen with the size, if the picture is too small? Do you want to stretch it?Eboni
Would you please click on my sample images in the question?Dezhnev
this is your script result: dooor.ir/storage/nzvo6tZtbb2wTQvVq8XMoRfnP0IJGIbXqmNv4CDX/1.jpg. The link will be disable during my developementfor future visitorsDezhnev
@Dezhnev I see that the photos in your example fit into the center in the same way that this code does. Could you explain the differences? The picture by URL from the your comment is very small, do you want it to stretch to bigger width/height?Eboni
D
5

Well, thanks to @Anton for his hint, I did this to solve my problem:

The image is horizontal rectangle, vertical rectangle or square.

I wrote these lines of code for every situation and it works great for my case

$img    = Image::make($image->getRealPath());

$width  = $img->width();
$height = $img->height();


/*
*  canvas
*/
$dimension = 2362;

$vertical   = (($width < $height) ? true : false);
$horizontal = (($width > $height) ? true : false);
$square     = (($width = $height) ? true : false);

if ($vertical) {
    $top = $bottom = 245;
    $newHeight = ($dimension) - ($bottom + $top);
    $img->resize(null, $newHeight, function ($constraint) {
        $constraint->aspectRatio();
    });

} else if ($horizontal) {
    $right = $left = 245;
    $newWidth = ($dimension) - ($right + $left);
    $img->resize($newWidth, null, function ($constraint) {
        $constraint->aspectRatio();
    });

} else if ($square) {
    $right = $left = 245;
    $newWidth = ($dimension) - ($left + $right);
    $img->resize($newWidth, null, function ($constraint) {
        $constraint->aspectRatio();
    });

}

$img->resizeCanvas($dimension, $dimension, 'center', false, '#ffffff');
$img->save(public_path("storage/{$token}/{$origFilename}"));
/*
* canvas
*/
Dezhnev answered 17/6, 2017 at 13:6 Comment(1)
the horizontal and square cases are identical, you could just get rid of square and use >= instead of > for horizontal check.Estus

© 2022 - 2024 — McMap. All rights reserved.