views:

69

answers:

2

I am uploading logos to my system, and they need to fix in a 60x60 pixel box. I have all the code to resize it proportionately, and that's not a problem.

My 454x292px image becomes 60x38. The thing is, I need the picture to be 60x60, meaning I want to pad the top and bottom with white each (I can fill the rectangle with the color).

The theory is I create a white rectangle, 60x60, then I copy the image and resize it to 60x38 and put it in my white rectangle, starting 11px from the top (which adds up to the 22px of total padding that I need.

I would post my code but it's decently long, though I can if requested.

Does anyone know how to do this or can you point me to code/tutorial that does this?

+3  A: 

With GD:

$newWidth = 60;
$newHeight = 60;
$img = getimagesize($filename);
$width = $width = $img[0];
$height = $img[1];
$old = imagecreatefromjpeg($filename); // change according to your source type
$new = imagecreatetruecolor($newWidth, $newHeight)
$white = imagecolorallocate($new, 255, 255, 255);
imagefill($new, 0, 0, $white);

if (($width / $height) >= ($newWidth / $newHeight)) {
    // by width
    $nw = $newWidth;
    $nh = $height * ($newWidth / $width);
    $nx = 0;
    $ny = round(fabs($newHeight - $nh) / 2);
} else {
    // by height
    $nw = $width * ($newHeight / $height);
    $nh = $max_height;
    $nx = round(fabs($newWidth - $nw) / 2);
    $ny = 0;
}

imagecopyresized($new, $old, $nx, $ny, 0, 0, $nw, $nh, $width, $height);
// do something with new: like imagepng($new, ...);
imagedestroy($new);
imagedestroy($old);
RC
That's what he said.
amphetamachine
@amphetamachine, yup, misread the question, so edit
RC
Perfect -- thank you :). The only problem I'm running into now, which I'm sure I can figure out is that the color isn't always turning out white.
Kerry
A: 

http://php.net/manual/en/function.imagecopyresampled.php

That's basically the function you want to copy and resize it smoothly.

http://www.php.net/manual/en/function.imagecreatetruecolor.php

With that one you create a new black image.

http://www.php.net/manual/en/function.imagefill.php

That part explains how to fill it white.

The rest follows.

Lajla