'Max Image Size'

SpaceshipSpaceship
Watch

Fewski

VIP Member
Impact
64
Is there a way to make a max image size? I have a site where people to upload photos.... I don't want the photos larger than 400x400 - but if someone uploads a phot thats a 100x100, I don't want it to be enlarged to 400x400 (Like what height="" width="" does).

Is there a way to keep it where it resizes it if's it's larger than 400x400, but it doesn't if it's smaller?

Thanks! :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
not 100% sure on what you mean.

If you wish for the image to be displayed in it's actual size simply find the size and output that in the height="" width="" attributes using http://www.php.net/manual/en/function.getimagesize.php.

This is of course assuming you are using php as you have not actually stated the language in use.
 
0
•••
peter@flexiwebhost said:
not 100% sure on what you mean.

If you wish for the image to be displayed in it's actual size simply find the size and output that in the height="" width="" attributes using http://www.php.net/manual/en/function.getimagesize.php.

This is of course assuming you are using php as you have not actually stated the language in use.

I think he means something like vbulletin has, when you upload your avatar it must be smaller than X pixels by X pixels, if it's bigger, vbulletin won't upload it and it will let you know that it's too big.

I've never done something myself, but seeing it done in vbulletin there must be a way :)
 
0
•••
yes there is, i'm using this code, altough i write it a long time ago it does the work ( maybe i revised it someday )
Code:
if ($_REQUEST[id])  {

//sets the max width in pixels 
  $maxwidth = "700";
// gets the image size - $get_dir = files"."/"; - in this case 
  $imagehw = GetImageSize($get_dir.$_REQUEST[id]);
// gets the size of the image in Kbs
  $realsize = round(FileSize($get_dir.$_REQUEST[id])/1024);
// sets the real image width and height
  $imagewidth = $imagehw[0];
  $imageheight = $imagehw[1];
  $realwidth = $imagewidth;
  $realheight= $imageheight;
  $imgorig = $imagewidth;
// resizes the image
  if ($imagewidth > $maxwidth) {
    $imageprop=($maxwidth*100)/$imagewidth;
    $imagevsize= ($imageheight*$imageprop)/100 ;
    $imagewidth=$maxwidth;
    $imageheight=ceil($imagevsize);
  }
// prints the result with some stuff...
  $img .="<strong>This image is hosted for free.</strong></br>";
  $img .="<a href='".$get_url.$_REQUEST[id]."' target='_blank'><img alt='' src='files/".$_REQUEST[id]."'border ='0' [B]width='".$imagewidth."'[/B] [B]height='".$imageheight."'[/B] /></a>";
  $img .="</br>";
  $img .="<strong>Image Resolution (".$realwidth."x".$realheight.") - Image size ".$realsize."Kbs</strong></br>";
  $img .="- to save the image right click and 'Save as'</br>";
  $img .="- to view the image in full size click on it</br>";
  $subtitle="- viewing - ".$_REQUEST[id];
}

hope this helps you :)
you can see a live demo on http://imghosted.net/view.php?id=twcoNCwild-dogs-01301334b.jpg that way you get a ideia of what means what.
 
Last edited:
1
•••
Thanks for the posts all. Let me see if I can clear it up some:

400x400 is max size

If someone uploads a 800x800 image to my website, it will be resized to 400x400 automaticly. But if someone uploads a 300x350 image, the image will stay 300x350, there will be no resizing.

Hope that clears it up some, and thanks for the posts! :)
 
0
•••
Fewski said:
Thanks for the posts all. Let me see if I can clear it up some:

400x400 is max size

If someone uploads a 800x800 image to my website, it will be resized to 400x400 automaticly. But if someone uploads a 300x350 image, the image will stay 300x350, there will be no resizing.

Hope that clears it up some, and thanks for the posts! :)

That's Cropping
 
0
•••
59codes said:
That's Cropping
No.. he's asking to scale the image down if it's over his max size, 400x400.

Fewski, do you already have the uploader and just need this resizing part? Resizing isn't hard to code, but it won't help if you don't have the uploader. :]
 
0
•••
Dan said:
No.. he's asking to scale the image down if it's over his max size, 400x400.

Fewski, do you already have the uploader and just need this resizing part? Resizing isn't hard to code, but it won't help if you don't have the uploader. :]
Hi Dan,

I have the uploader. I think the resizer is just the height="" width="" in the img code - not with the uploader code. It resizes the images fine that are over 400x400 - but it also does the images under 400x400 which I don't want.

Thanks for the reply. :)
 
0
•••
Could you post the code? It only needs an if statement to check the size first.
 
0
•••
This should be it:

PHP:
//Create a new ad in the MySQL table, and upload the given image. $image is a reference to the image in $_FILES
	function NewAd($owner_id, $title, $image, $category, $price, $info, $location)
	{
		$this->sql->query("SELECT * FROM ads WHERE title = '%s'", $title);
		
		//Die if there's another ad with the same name
		if($this->sql->num_rows() != 0)
		{
			echo "<script>alert('Sorry. Another ad exists with this title. Please choose another title.');</script>";
			return FALSE;
		}
		//If $image is an array (i.e. not a link to an offsite hosted image)
		if(is_array($image))
		{
			$filext = explode(".",$image['name']);
			$filext = $filext[(count($filext)-1)];
			
			echo $image['name'] . '<br>';
			
			move_uploaded_file($image['tmp_name'], AD_IMAGES.addslashes($title).'.'.$filext);
			$image_url = SERVER_ROOT.AD_IMAGES.addslashes($title).'.'.$filext;
		}
		elseif(empty($image))
			$image_url = 'http://www.racingrides.com/images/noimage.gif';
			
		//If $image is the URL to an image hosted elsewhere.
		else
			$image_url = $image;
		//Query to add this information into the db
		$this->sql->query("INSERT INTO ads SET owner = '%s', title = '%s', image = '%s', category = '%s', price = '%s', info = '%s', date = '%s', location = '%s'", $owner_id, $title, $image_url, $category, $price, $info, time(), $location);
		
		return TRUE;
	}

Thanks again Dan! :)
 
0
•••
Fewski said:
I think the resizer is just the height="" width="" in the img code - not with the uploader code.

That's the problem right there. If you have an img tag like

PHP:
<img src="/path/to/image" width="400" height="400" />

All images will be sized to 400x400, small and large alike.

In your PHP you need to determine the new scale size for the image (if it's too large), and then programmatically insert the height and width values into the <img> tag:

PHP:
<?php

// get the size of the image
list($width, $height) = getimagesize(AD_IMAGES.addslashes($title).'.'.$filext);

// if width or height are greater than 400, scale down
if ($width > 400 || $height > 400)
{
	// init the defaults
	$x = $y = 400;

	// scale the width and/or height
	if ($width > $height)
		$y = $height * (400 / $width);
	else if ($width < $height)
		$x  = $width * (400 / $height);

	// now set the actual variables
	$width = intval(floor($x));
	$height = intval(floor($y));
}
?>

<!-- somewhere in your html -->
<img src="/path/to/image" width="<?=$width?>" height="<?=$height?>" />

Note that the above code does not actually scale the image file itself on disk; rather, it prints out the sized scale values in the img tag and lets the browser do the work.

If you actually want to scale the image file, it'll take a few extra lines of code.
 
0
•••
Appraise.net

We're social

Spaceship
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back