Question

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

Was it helpful?

Solution

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! :)

OTHER TIPS

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;
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top