?
Solved

make sure the image max is 600x600

Posted on 2010-08-14
4
Medium Priority
?
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Expert Comment

by:scarybot
ID: 33436644
0
 
LVL 4

Accepted Solution

by:
philkeene earned 2000 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 111

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses

764 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