1

I want to resize my images to a square. Say I want a squared image of 500x500 and I have an image of 300x600 I want to resize that image down to 200x500 and then add a white background to it to make it 500x500

I got something working good by doing this:

$TargetImage = imagecreatetruecolor(300, 600); 
imagecopyresampled(
  $TargetImage, $SourceImage, 
  0, 0, 
  0, 0, 
  300, 600, 
  500, 500
);
$final = imagecreatetruecolor(500, 500);
$bg_color = imagecolorallocate ($final, 255, 255, 255)
imagefill($final, 0, 0, $bg_color);
imagecopyresampled(
  $final, $TargetImage, 
  0, 0, 
  ($x_mid - (500/ 2)), ($y_mid - (500/ 2)), 
  500, 500, 
  500, 500
);

It's doing almost EVERYTHING right. The picture is centered and everything. Except the background is black and not white:/

Anyone know what I'm doing wrong?

picture

zipzit
  • 3,778
  • 4
  • 35
  • 63
odannyc
  • 717
  • 9
  • 25
  • As far as I'm aware, this cannot be done with PHP. – Sauced Apples Feb 09 '16 at 20:05
  • You may want to use an extension like [imagemagick](http://php.net/manual/en/intro.imagick.php). Particularly if other additional image manipulations are on the horizon. – Michael Plotke Feb 09 '16 at 20:17
  • Can you provide real world values for original image width/ height, `$Width`/`$Height` and `$FinalWidth`/`$FinalHeight`? – maxhb Feb 09 '16 at 20:45

1 Answers1

5

I think this is what you want:

<?php
   $square=500;

   // Load up the original image
   $src  = imagecreatefrompng('original.png');
   $w = imagesx($src); // image width
   $h = imagesy($src); // image height
   printf("Orig: %dx%d\n",$w,$h);

   // Create output canvas and fill with white
   $final = imagecreatetruecolor($square,$square);
   $bg_color = imagecolorallocate ($final, 255, 255, 255);
   imagefill($final, 0, 0, $bg_color);

   // Check if portrait or landscape
   if($h>=$w){
      // Portrait, i.e. tall image
      $newh=$square;
      $neww=intval($square*$w/$h);
      printf("New: %dx%d\n",$neww,$newh);
      // Resize and composite original image onto output canvas
      imagecopyresampled(
         $final, $src, 
         intval(($square-$neww)/2),0,
         0,0,
         $neww, $newh, 
         $w, $h);
   } else {
      // Landscape, i.e. wide image
      $neww=$square;
      $newh=intval($square*$h/$w);
      printf("New: %dx%d\n",$neww,$newh);
      imagecopyresampled(
         $final, $src, 
         0,intval(($square-$newh)/2),
         0,0,
         $neww, $newh, 
         $w, $h);
   }

   // Write result 
   imagepng($final,"result.png");
?>

Note also, that if you want to scale down 300x600 to fit in 500x500 whilst maintaining aspect ratio, you will get 250x500 not 200x500.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • That works for an image that's standing up vertically. But if I lay the image down horizontally the image gets "squished" vertically. – odannyc Feb 10 '16 at 18:25
  • Ok, I am not at my computer now, but you have a white background now of the right size so the code is correct up to the `imagecopyresampled()`, yes? So we need to get width and height of original image and find which is longer, which is easy, then we just change the 2nd-8th parameters of `imagecopyresampled()` accordingly. I'll do it tomorrow if you don't work it out. – Mark Setchell Feb 10 '16 at 19:31
  • Please have another try. – Mark Setchell Feb 11 '16 at 10:13
  • While it works, it's much more complicated than necessary. Instead of if-else, you can simply calculate the $width / $bitmap_width ratio, the $height / $bitmap_height ratio, get the minimum of the two and call imagecopyresampled with that scale. That will give you the "fit to" size immediately. And, actually, if you use the maximum instead, you get the "fill to" size with the same operation. – Gábor Sep 29 '19 at 19:24