Solved

make sure the image max is 600x600

Posted on 2010-08-14
4
421 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:rgb192
4 Comments
 
LVL 1

Expert Comment

by:scarybot
ID: 33436644
0
 
LVL 4

Accepted Solution

by:
philkeene earned 500 total points
ID: 33436660
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...
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33436814
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?
0
 

Author Closing Comment

by:rgb192
ID: 33436840
thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now