Jump to content

RE: Image resize on upload


cdoggg94

Recommended Posts

What I want to do is to upload an image, find out what the size is and then make the height=377, and keep with width at the same ratio.

 

this is my upload code:

 

<?php
error_reporting(0);

mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("myDB") or die(mysql_error());

$picture = $_FILES['image']['tmp_name'];
$brand = $det['pro_name'];
$year = $det['pro_vint'];
$story = $det['pro_story'];
$description = $det['pro_desc'];
$why = $det['pro_why'];
$intensity = $det['F12'];
$body = $det['F11'];
$sweet = $det['F8'];
$acid = $det['F9'];
$oak = $det['F10'];
$lcbo = $det['lcbo_num'];
$size = $det['pro_size'];
$btl = $det['pro_btl'];
$price = $det['pro_price'];
$lto = $det['F7'];
$audio = $det['wineAudio'];

if(!isset($picture))
echo "";
	else
	{
	$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));	
	$image_name = addslashes($_FILES['image']['name']);
	$image_size = getimagesize($_FILES['image']['tmp_name']);

list($width, $height, $type, $attr) = getimagesize($_FILES['image']['tmp_name']);

if($height != $width * 1.5 ){
  	echo "Image does not meet requirements";
}else{

	if($image_size==FALSE)
		echo "This is not an Image";
	else{
			if(!$insert = mysql_query ("UPDATE `phpmy1_vincastweb_com`.`Sheet1` SET `image_name` = '".$image_name."', image='".$image."' WHERE  `pro_id` ='".$id."'"))
				echo "Problem Uploading Image.";
			else{
				$lastid = mysql_insert_id();
				echo "Image Uploaded!<p />".$text."<p /><p /> Your Image:<p />";
				$lastid = $id;
				echo "<img src='get_2.php?Product=$lastid' width='250' height='320' /><br />Click <a href='Agency/product3.php?Product=".$id."' target='_blank'>HERE</a> to view with Product"


				;

				}
		}

	}

}
?>

 

right now I have it so that its at a ratio of height=3, and width=2...everything works fine but its not exactly what i want..

 

any ideas ?

Link to comment
Share on other sites

Hi cdoggg94

 

Im working on a similar issue.  Here is my working code to get current width height then return desired scaled height, maybe you can work with it.

 

// Constraints
$img_path = MY IMG PATH HERE; 
$max_width = 110;
$max_height = 400;
list($width, $height) = getimagesize($img_path);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$mywidth = intval($ratio*$width);
$myheight = intval($ratio*$height);

 

mywidth and myheight will return your scaled numbers then you can do what you want with them.

Link to comment
Share on other sites

I have adapted it to work with my code.

 

$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));	
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);

//$img_path = $image; 
$max_width = 250;
$max_height = 377;
list($width, $height) = $image_size;

//echo $width.",".$height;

$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$mywidth = intval($ratio*$width);
$myheight = intval($ratio*$height);

//echo $mywidth.",".$myheight;

 

Then I am going to insert $mywidth and $myheight into the table with the rest of the form information and use that as my image height and width when i display them on any given page.

 

Many thanks !

 

I was getting a little frustrated haha.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.