ok i am uploading images and all is well, i make a thumbnail and life is sweet. but a lot of my images are going to be like ultra big like bigger than my resolution. what i want is if the image is bigger than a certain height/width then it will be scalled down but keep the same proportions. heres what i want kinda
<code stuff>
if (height or width are greater than set limits)
//have max height = 800 and width = 600
take largest one from picture //ie. if pic is wider than height then take pic
//we will go with width on this example
take width down to 600 and then scale height to whatever it would be to keep proportions
else
leave picture be
save the picture over the original in the folder where i uploaded the initial picture
</code stuff>
get what i mean? if anyone can help me write this function that would be fantastic. also i will be dealing with both gif and jpegs so yaaaaaaaa
scale image down
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
ok i got some stuff kicking. PROBLEMS! i have probably had 1 too many today so i am sucking hard at math but this is what i got right now
only works on jpegs obviously but right now thats all i care about. what i really need is the math stuff to get the new height and new width while keeping the same proportions. really the problem is at
i hope you see what i mean. thanks
Code: Select all
function createImage($image_path,$image_name,$max_width = 80,$max_height = 60)
{
$src_img = imagecreatefromjpeg($image_path.'/'.$image_name);
$height = imagesy($src_img);
$width = imagesx($src_img);
if ($height <= $max_height && $width <= $max_width)
return true;
if ($width > $height)
$top = 'width';
else
$top = 'height';
if ($top == 'height')
{
$new_h = $max_height;
$diff = $height-$new_h;
$new_w = $max_width-$diff;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, $image_path/$image_name, 70);
return true;
}
if ($top == 'width')
{
$new_w = $max_width;
$diff = $width-$new_w;
$new_h = $max_height-$diff;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, $image_path/$image_name, 70);
return true;
}
}Code: Select all
$new_h = $max_height;
$diff = $height-$new_h;
$new_w = $max_width-$diff;
//and
$new_w = $max_width;
$diff = $width-$new_w;
$new_h = $max_height-$diff;This should work:
This is untested and could probably be optimized a bit, but I've used this logic before and it's sound
Code: Select all
//first, we need to figure out which dimension is the largest
$width_change = $max_width/$img_width;
$height_change = $max_height/$img_height;
//if (the amount the image width is greater than the max width) is greater than
//(the amount the image height is greater than the max height), use
//the change in width to determine height
if($width_change < $height_change)
{
$new_width = $max_width;
$new_height = $img_height * $width_change;
}
//likewise with the height
else
{
$new_height = $max_height;
$new_width = $img_width * $height_change;
}Real programmers don't comment their code. If it was hard to write, it should be hard to understand.