Solved

make sure the image max is 600x600

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

19 Experts available now in Live!

Get 1:1 Help Now