well, you could use the age-old CSS and HTML trick and just tell it to be X size 😉 But if you're creating thumbnails, then no. You have to use imagecreatefromjpeg().
What you might think about doing is utilizing some scaling techniques. here's a quick overview:
Set width of your thumbnail in pixels: 300 (from your above code)
Calculate proprotionate dimension: Original Height / 300 * Original Width (remember math order of operations, left to right)
If the image is tall (portrait):
The set width (300) becomes the height, and the calculated proportion becomes the width.
So all in all, a very short script to calculate dimensions would be:
$o_dims = getimagesize('original_image.jpg');
$is_tall = $o_dims[1] >= $o_dims[0];
$t_width = 300;
$t_height = ceil($o_dims[1] / $t_width * $o_dims[0]); // We'll round up ;)
if($is_tall)
{
$t = $t_width;
$t_width = $t_height;
$t_height = $t;
}
That's very crude, but it works for creating a proportionate image. It won't, however, create an image that is limited in dimensions both width and height. But I'm sure you can figure that out 😉