News:

Wondering if this will always be free?  See why free is better.

Main Menu

Using GD2 to resize thumbnails with SMF?

Started by NoRad, October 10, 2004, 06:32:32 AM

Previous topic - Next topic

NoRad

I'm trying to resize thumbnails in a specific way.

I'd like to border them in with blank space (black background) at 100x100. If a user uploads an image that is 100 x 80, it will add 10 pixels of black on the top and bottom of the picture when it stores it as a thumbnail.

I was looking up some stuff here: http://www.boutell.com/gd/manual2.0.11.html#gdImageGd2 and just got totally overwhelmed. I'm assuming I need to set the background using this: black = gdImageColorAllocate(im, 0, 0, 0);   

but... Where can I choose to set the canvas size after resizing it as the thumbnail?

Would this be like creating a 100 x 100 image with a black background, resizing the image uploaded by the user to the thumbnail size, and then copying the user image on top of the background (centered) and then saving? I think that's the logic.

NoRad

#1
Maybe I should just use imagemagick?? =)



Pad out the image to a specific size

The next most common request is to generate thumbnails that fill out the image with borders of a specific color (usually 'black', or 'transparent' but for these examples I will use 'skyblue') so the thumbnail is exact the size you wanted.

For example: An which is 400x300 pixels shrunk to fit a 100x100 pixel box will normal (with the above) have a size of 100x75 pixels. We want to add some black borders to the top and bottom of the image (and to the sides to be sure) to make the final thumbnail always 100x100.

There are a number of ways to do this.. one is to add a border and crop...
    convert faerie.jpg -thumbnail '100x100>' \
            -bordercolor skyblue  -border 50 \
            -gravity center  -crop 100x100+0+0 +repage crop.gif

NoRad

Scratch that idea. I can't get imagemagick to work on the server (windows 2000). I've tested it with coppermine and no luck. Anybody here have it working with SMF on a windows server?

kegobeer

GIF create support was only recently added back into GD v2.0.28.  The newest release of PHP has that version bundled with it.  Before using GD make sure your version of GD is current, and your version of PHP has the imagecreatefromgif command available.

It's really not too hard to do what you want.  Instead of going to boutell.com, go to php.net and view their online manual.  All the PHP commands you'll need to do the image resizing are there, and it's much easier to comprehend.

Getting ImageMagick working on Windows can be very tricky.  I suggest installing it in a different directory than the default one (I have all my server files on a separate drive - ImageMagick is in f:\ImageMagick\).  Make sure you have the latest version, ImageMagick-6.1.0-Q16-windows-dll.exe.  Before that version I always had trouble getting it to work with Coppermine, too.
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

[Unknown]

Look in Sources/Subs-Graphics.php.  It has gif and bmp support.

-[Unknown]

NoRad

Thank you both. I'm checking into the GD2 solution first.

NoRad

I'm looking at the resize function now:

function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $max_height)
{
global $gd2, $modSettings;

// Determine whether to resize to max width or to max height (depending on the limits.)
if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height))
{
$dst_width = $max_width;
$dst_height = floor($src_height * $max_width / $src_width);
}
else
{
$dst_width = floor($src_width * $max_height / $src_height);
$dst_height = $max_height;
}

// (make a true color image, because it just looks better for resizing)
if ($gd2)
$dst_img = imagecreatetruecolor($dst_width, $dst_height);
else
$dst_img = imagecreate($dst_width, $dst_height);

// Resize it
if ($gd2)
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
else
imageCopyResampleBicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);

// Save it
if (!empty($modSettings['avatar_download_png']))
imagepng($dst_img, $destName);
else
imagejpeg($dst_img, $destName);

// Free the memory
imagedestroy($src_img);
imagedestroy($dst_img);
}


After referencing http://us2.php.net/image for the commands to use it looks like I should try imagefill. However, I am not sure where to place this and whether or not I'll need to do a merge afterwards? I think I should define a background color... Something like

$black = imagecolorallocate($im, 0, 0, 0);

OR ... I just modify this line to

      imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, 100, 100, $src_width, $src_height);

but that didn't work as planned.  I'll have to think about this.

kegobeer

Bear in mind that imagecreatetruecolor doesn't work with GIF images.  You should check the image type before doing any manipulation to take that into account.
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

Advertisement: