make sure the image max is 600x600

this code sets the $newwidth=600

$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);


next I want to set the $newheight=600

$newheight=600;
$newwidth=($width/$height)*600;




If the image is 1200 height and 2 width then this image will be larger than 600 height


I want this image to fit in a 600 by 600 foot box

<?php

// This is the temporary file created by PHP
//$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$uploadedfile="uploaded/7dkitt2.jpg";

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
//$filename = "images/". $_FILES['uploadfile']['name'];
$filename = "uploaded/newfile.jpg";
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.
?>

Open in new window

LVL 1
rgb192Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

scarybotCommented:
philkeeneCommented:
If I understand this right, you want the maximum height and maximum width to be set, and the image must maintain its original aspect ratio. If that is so, then simply replace the lines...

$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);

... with the following...

$maxwidth = 600;
$maxheight = 600;

if($height > $maxheight)
{
    $newheight = $maxheight;
    $newwidth = ($width / $height) * $newheight;
}
elseif($width > $maxwidth)
{
    $newwidth = $maxwidth;
    $newheight = ($height / $width) * $newwidth;
}
else
{
    $newwidth = $width;
    $newheight = $height;
}

$tmp=imagecreatetruecolor($newwidth,$newheight);

... hope this helps...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Give us another example or two about the aspect ratio, please.  

If I have an image that is 1000 wide and 500 high, and I resize it to be 600 wide and 600 high, there are a few choices.

I can stretch and squash the image, distorting it.

I can make a square crop 600x600 image from some part of the original image (perhaps the center) with some kind of borders above and below the 500 pixels of image information.

I can make the width 6/10 of the original and the height 6/10 * 500, preserving the aspect ratio and the original image information, again with some kind of filler or borders.

Or I can use the original image "as is" and simply use CSS or explicit attributes in my HTML to effect the resizing at the client browser.

What do you want to do?
rgb192Author Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.