Solved

make sure the image max is 600x600

Posted on 2010-08-14
4
423 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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

785 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