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
https://forums.phpfreaks.com/topic/257562-re-image-resize-on-upload/
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.

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.