views:

67

answers:

3

hy,

Let's say I want to convert an image to an exact size, eg: 400x300. The trick is, if the image, due to its aspect ratio, does not fit in 400x300, then place it in there with black borders.

A 900x1200 image would be converted down to 225x300 to retain its aspect ratio, and then given black borders left and right to make it 400x300.

original photo:

|||||||||||||||||||||||  
|||||||||||||||||||||||  
|||||||||||||||||||||||  
|||||||||||||||||||||||  
|||||||||||||||||||||||  
|||||||||||||||||||||||  
|||||||||||||||||||||||  
|||||||||||||||||||||||  

after resize i want to look something like this:

_______________________
|+++++++++++++++++++++|
|+++++++++++++++++++++|
|+++++++++++++++++++++|
|+++++++||||||||++++++|
|+++++++||||||||++++++|
|+++++++++++++++++++++|
|+++++++++++++++++++++|
|_____________________|

the: "+++++++" i want to be some color, and the "|||||||" are the image, in the middle!

unfortunately i don't have any code yet!

i want something like this: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x22 thanks

A: 

I'm a big fan of editing and resizing the pictures in advance to be the size I want rather than setting the width and height when it is rendered. The performance is better and you don't have to worry about how different browsers might handle it differently.

So if you don’t have an image editing program get Paint.net. Free and works well.

JBrooks
i want this to be done in php!
robertdd
A: 

Create a div styled to have a black background, and sized at 400x300. Then show your resized image inside that div. You can even include a check to make sure that the image is not the desired aspect before showing the div.

Something like this:

<? 
$height-ratio = $height / 300;
$width-ratio = $width / 400;

if ($height-ratio == $width-ratio) {
 $ratio = $height-ratio;
} elseif ($height-ratio > $width-ratio) {
 $ratio = $height-ratio;
 echo "<div class='blackbox'>";
} else {
 $ratio = $width-ratio;
 echo "<div class='blackbox'>";
}

$newHeight = $height / $ratio;
$newWidth = $width / $ratio;
echo "<img src='".$imgSrc."' height='".$newHeight."' width='".$newWidth."' />";

if ($height-ratio != $width-ratio)
 echo "</div>";
?>

With supporting css of:

.blackbox { 
 text-align: center;
 background-color: black;
 height: 300px;
 width: 400px;
}
JGB146
i need to save the photo, not only to display it
robertdd
Good thinking. No need to waste bandwidth on a solid bg--especially if the image is going to be output in compressed JPEG format, which makes solid colors look terrible. If you need to save it, then I would stick with PNG or SVG (SVG is less supported, but will render the background most efficiently).
Lèse majesté
You may not be wasting bandwidth on a solid background, but you're still downloading a potentially massive image, which just gets scaled down to 400x300 by the browser. Which is likely wasting a lot more bandwidth than a bit of black border.
deceze
+2  A: 

Get familiar with the gd functions, which allow you to manipulate images.

You'll need to read in your image using one of the imagecreatefrom... functions. Then you'll need to create a second image using, for example, imagecreatetruecolor, which you fill with your color of choice. Then you copy the original image into the new image using imagecopyresampled, which allows you to resize it in the process. You'll need to calculate the new size with some simple math beforehand, which functions like getimagesize can help you with.

Alternatively, play around with the ImageMagick class, which is an alternative to gd you may or may not find easier to work with.

Best of luck! :)

deceze
@robertdd There's lots of sample code for each function, the example for [`imagecopyresampled`](http://php.net/manual/en/function.imagecopyresampled.php) is pretty much exactly what you're looking for.
deceze