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

Kiyarash
  • 2,437
  • 7
  • 32
  • 61

2 Answers2

7
<?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');
kopaty4
  • 2,236
  • 4
  • 26
  • 39
  • 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`? – Kiyarash Jun 04 '17 at 02:31
  • @Kiyarash and what should happen with the size, if the picture is too small? Do you want to stretch it? – kopaty4 Jun 04 '17 at 02:34
  • Would you please click on my sample images in the question? – Kiyarash Jun 04 '17 at 02:36
  • this is your script result: http://dooor.ir/storage/nzvo6tZtbb2wTQvVq8XMoRfnP0IJGIbXqmNv4CDX/1.jpg. The link will be disable during my developement`for future visitors` – Kiyarash Jun 04 '17 at 02:39
  • @Kiyarash 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? – kopaty4 Jun 04 '17 at 02:44
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
*/
masud_moni
  • 1,121
  • 16
  • 33
Kiyarash
  • 2,437
  • 7
  • 32
  • 61
  • 1
    the horizontal and square cases are identical, you could just get rid of square and use `>=` instead of `>` for horizontal check. – Linuslabo Jun 01 '20 at 13:50