Jump to content

could someone help please


justAnoob

Recommended Posts

this is driving me crazy. all i'm looking to do is if the image height is taller than its width, set the height to 380 and the width to the correct ratio

 

and if the width is wider than the height, set the width to 380 and the height to the correct ratio

Does this look like i'm getting close?

list($width_orig, $height_orig) = getimagesize($newname);

$ratio_orig = $width_orig/$height_orig;     

if ($width/$height > $ratio_orig) {  
   $width = $height*$ratio_orig;
} else {
  $height = $width/$ratio_orig;
}

Link to comment
https://forums.phpfreaks.com/topic/187339-could-someone-help-please/
Share on other sites

Sorry all I saw was unnecessary variables but I guess they aren't

 

list($width_orig,$height_orig) = getimagesize($newname);  
if($height_orig > $width_orig) {  
$ratio = $width/$height;
$height = 380;
$width = $height*$ratio;
}
else {
$ratio = $height/$width;   
$width = 380;
$height = $width*$ratio;
}

that works great, actually i changed the 380 to 90  for my thumbnails,  now the problem is resizing the full size image that also gets uploaded. i tried duplicating the actions for the full size image, but things go screwy. Here is what I'm trying  (making the thumbnails now bigger than 90 for width or height, and full size images no bigger than 380 for width and height.  Anyone know?

Here is the whole script, I'm stumped.

<?php
session_start();
include "connection.php";

$item_name = mysql_real_escape_string($_POST['item_name']);
$description = mysql_real_escape_string($_POST['description']);
$in_return = mysql_real_escape_string($_POST['in_return']);
$category = mysql_real_escape_string($_POST['listmenu']);
$created_on = mysql_real_escape_string($_POST['created_on']);

define ("MAX_SIZE","1500");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
	return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;
if(isset($_POST['submit']))
{
$image=$_FILES['image']['name'];
if($image) 
{

	$filename = stripslashes($_FILES['image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension != "jpg") && ($extension != "jpeg"))
	{
		// error

	}
	else
	{
		$size=filesize($_FILES['image']['tmp_name']);
		if ($size > MAX_SIZE*2048)
		{
			// error
		}

		$tname = time();
$image_name=$tname.'.'.$extension;
$newname="userimages/$category/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);

$image_thumb=$tname.'-thumb.'.$extension;
$newthumbname="userimages/$category/".$image_thumb;


// Get new dimensions
list($width_orig, $height_orig) = getimagesize($newname);
// Thumbnail
if($height_orig > $width_orig)
{
$ratio = $width_orig/$height_orig;
$height = 90;
$width = $height*$ratio;
}
else
{
$ratio = $height_orig/$width_orig;
$width = 90;
$height = $width*$ratio;
}
//Full size image
if($height_orig_2 > $width_orig_2)
{
$ratio_2 = $width_orig_2/$height_orig_2;
$height2 = 380;
$width2 = $height2*$ratio2;
}
else
{
$ratio2 = $height_orig2/$width_orig2;
$width2 = 380;
$height2 = $width2*$ratio2;
}

// Resample
// Thumbnail
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($newname);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Full size image
$image_p2 = imagecreatetruecolor($width2, $height2);
$image2 = imagecreatefromjpeg($newname); 
imagecopyresampled($image_p2, $image2, 0, 0, 0, 0, $width2, $height2, $width_orig2, $height_orig2);


// Output
// Thumbnail
imagejpeg($image_p, $newthumbname, 100);
imagedestroy($image);
imagedestroy($image_p);
// Full size image
imagejpeg($image_p2, $newname, 100);
imagedestroy($image2);
imagedestroy($image_p2);


		if (!$copied)
		{
			// error

		}

	}
}
}

// if everything is good, post new item for the user
$mysqlcategory = $category;
$imgpath = $newname;
$thumb_1 = $newthumbname;
$findit = $_SESSION['id'];
$result=mysql_query("SELECT id FROM my_table WHERE username = '$findit'");
$row=mysql_fetch_assoc($result);
$user_id = $row['id'];
$sql = "INSERT INTO my_table_2(item_name, description, in_return, imgpath, thumb_1, category, user_id, created_on)VALUES('$item_name','$description','$in_return', '$imgpath', '$thumb_1', '$mysqlcategory', '$user_id', '$created_on')";
mysql_query($sql) or die(mysql_error());
// go to confirmation page if upload is completed.
if(isset($_POST['submit']) && !$errors)
{
// success
}
else
{
// no good
}
mysql_close();
?>

Try this out

 

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($newname);
// Thumbnail
if($height_orig > $width_orig)
{
$ratio = $width_orig/$height_orig;
$height1 = 90;
$height2 = 380;
$width = $height*$ratio;
$width2 = $height2*$ratio;
}
else
{
$ratio = $height_orig/$width_orig;
$width = 90;
$width2 = 380;
$height = $width*$ratio;
$height2 = $width2*$ratio;
}

// Resample
// Thumbnail
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($newname);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Full size image
$image_p2 = imagecreatetruecolor($width2, $height2);
$image2 = imagecreatefromjpeg($newname); 
imagecopyresampled($image_p2, $image2, 0, 0, 0, 0, $width2, $height2, $width_orig, $height_orig);

well, I'll gave it a shot and the full size image is not reduced.  The script below does everything great for creating the thumbnail,, still looking for an answer to get the full size image scaled down to 380, meaning the height or width is a max of 380 and the the aspect ratio remains intact.

 

<?php
session_start();
include "connection.php";

$item_name = mysql_real_escape_string($_POST['item_name']);
$description = mysql_real_escape_string($_POST['description']);
$in_return = mysql_real_escape_string($_POST['in_return']);
$category = mysql_real_escape_string($_POST['listmenu']);
$created_on = mysql_real_escape_string($_POST['created_on']);

define ("MAX_SIZE","1500");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
	return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;
if(isset($_POST['submit']))
{
$image=$_FILES['image']['name'];
if($image) 
{

	$filename = stripslashes($_FILES['image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension != "jpg") && ($extension != "jpeg"))
	{
		// ERROR

	}
	else
	{
		$size=filesize($_FILES['image']['tmp_name']);
		if ($size > MAX_SIZE*2048)
		{
			// ERROR
		}

		$tname = time();
$image_name=$tname.'.'.$extension;
$newname="userimages/$category/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);

$image_thumb=$tname.'-thumb.'.$extension;
$newthumbname="userimages/$category/".$image_thumb;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($newname);
if($height_orig > $width_orig)
{
$ratio = $width_orig/$height_orig;
$height = 90;
$width = $height*$ratio;
}
else
{
$ratio = $height_orig/$width_orig;
$width = 90;
$height = $width*$ratio;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($newname);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, $newthumbname, 100);
imagedestroy($image);
imagedestroy($image_p);


		if (!$copied)
		{
			// ERROR

		}

	}
}
}

// if everything is good, post new item for the user
$mysqlcategory = $category;
$imgpath = $newname;
$thumb_1 = $newthumbname;
$findit = $_SESSION['id'];
$result=mysql_query("SELECT id FROM members WHERE username = '$findit'");
$row=mysql_fetch_assoc($result);
$user_id = $row['id'];
$sql = "INSERT INTO my_table(item_name, description, in_return, imgpath, thumb_1, category, user_id, created_on)VALUES('$item_name','$description','$in_return', '$imgpath', '$thumb_1', '$mysqlcategory', '$user_id', '$created_on')";
mysql_query($sql) or die(mysql_error());
// go to confirmation page if upload is completed.
if(isset($_POST['submit']) && !$errors)
{
unset($_SESSION['toobig']);
unset($_SESSION['badformat']);
unset($_SESSION['notcopy']);
$_SESSION['posted'] = $item_name;
$_SESSION['picposted'] = $thumb_1;
$_SESSION['path_log'] = $imgpath;
header("location: http://www.mysite.com/mypage.php");
exit();
}
else
{
echo 'What the crap';
}
mysql_close();
?>

Try this

 

// Resample
// Thumbnail
print 'Thumbnal<br />-----';
print 'New Width: ' . $width . "<br />";
print 'New Height: ' . $height . "<br />";
print 'Original Width: ' . $width_orig . "<br />";
print 'Original Width: ' . $height_orig . "<br />";
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($newname);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Full size image
print 'Full Size Image<br />-----';
print 'New Width: ' . $width2 . "<br />";
print 'New Height: ' . $height2 . "<br />";
print 'Original Width: ' . $width_orig . "<br />";
print 'Original Width: ' . $height_orig . "<br />";
$image_p2 = imagecreatetruecolor($width2, $height2);
$image2 = imagecreatefromjpeg($newname); 
imagecopyresampled($image_p2, $image2, 0, 0, 0, 0, $width2, $height2, $width_orig, $height_orig);

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.