Jump to content

Recommended Posts

i have a script that uploads an image and sets a random name to it

but how can i set it to resize to the size i want?

can anyone help me please?

<?php 
// start session
session_start();
// check to make sure the session is registered
if(session_is_registered('username')) {} else {}
?>
<?php include('includes/header.inc.php'); ?>
<?php
if($_POST['uploadimagefish'] == "Upload the Image"){
	function findexts ($filename) 
	{ 
	$filename = strtolower($filename) ; 
	$exts = split("[/\\.]", $filename) ; 
	$n = count($exts)-1; 
	$exts = $exts[$n]; 
	return $exts; 
	} 

	//This applies the function to our file 
	$ext = findexts ($_FILES['uploaded']['name']) ;

	//This line assigns a random number and a timestamp to a variable.
	$ranr = rand();
	$rant = time();
	$ran = $ranr.$rant;

	//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
	$ran2 = $ran.".";

	//This assigns the subdirectory you want to save into... make sure it exists!
	$target = "uploaded_pictures/";
	//This combines the directory, the random file name, and the extension
	$target = $target . $ran2.$ext;

	if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
	{
	echo "The file has been uploaded as ".$ran2.$ext;
	} 
	else
	{
	echo "Sorry, there was a problem uploading your file.";
	}
}
?>
			<h1>Add a <span class="green">Picture</span></h1><br>

<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" name="uploadimagefish" value="Upload the Image" />
</form> 

<?php include('includes/footer.inc.php'); ?>

Link to comment
https://forums.phpfreaks.com/topic/75407-im-trying-to-resize-an-image-on-upload/
Share on other sites

Have a look at this, i wrote it yesterday afternoon for some minor test of something... it's not at all 100% perfect but it'll get you started out:

 

<?php
//Filename - do what you did in your own script
$filename = "something";

//Path
$path = "where/to/save/the/image/";

//Max height and width
$max_height = 600;
$max_width = 600;

//JPEG quality
$quality = 75;

//Getting image size
$size=getimagesize($image);
$width=$size[0];
$height=$size[1];

//Calculating the new height and width
$x_ratio=$max_width/$width;
$y_ratio=$max_height/$height;


if(($width <= $max_width) && ( $height <= $max_height)){
$tn_width=$width;
$tn_height=$height;
}
else if(($x_ratio*$height) < $max_height){
$tn_height=ceil($x_ratio*$height);
$tn_width=$max_width;
}
else{
$tn_width=ceil($y_ratio*$width);
$tn_height=$max_height;
}

$src = imagecreatefromjpeg($image);
$dst=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($dst, $src,0,0,0,0,$tn_width,$tn_height,$width,$height);
$create_image = imagejpeg($dst, $path . $filename, $quality);

 

This was what i tested...

http://wuhtzu.dk/test/imagemanipulator/image.php?img=humle.jpg&max_w=3000&max_h=3000&quality=75

 

Just if you wanted to "test" the script. It uses the same resizing method as i posted above.

The webserver, running PHP, will of course have to load the whole image first (in this case a 2048px x 1536px picture of hop from my garden), then resize the image and finally respond with the resized image to a request. But the visitor/user who requests the link i gave you will only receive the small image.

 

But I don't know where you would actually use a script like the one i posted a link to. Most often you would use image resizing along side with uploading. So if you write an article / post in your cms you can attach an image and it gets resized upon upload and then stored in it's resized form.

 

If your server had unlimited resources it would be cool to store all images in full resolution and then scale them upon request, but you haven't ... :)

Have a look at this, i wrote it yesterday afternoon for some minor test of something... it's not at all 100% perfect but it'll get you started out:

 

<?php
//Filename - do what you did in your own script
$filename = "something";

//Path
$path = "where/to/save/the/image/";

//Max height and width
$max_height = 600;
$max_width = 600;

//JPEG quality
$quality = 75;

//Getting image size
$size=getimagesize($image);
$width=$size[0];
$height=$size[1];

//Calculating the new height and width
$x_ratio=$max_width/$width;
$y_ratio=$max_height/$height;


if(($width <= $max_width) && ( $height <= $max_height)){
$tn_width=$width;
$tn_height=$height;
}
else if(($x_ratio*$height) < $max_height){
$tn_height=ceil($x_ratio*$height);
$tn_width=$max_width;
}
else{
$tn_width=ceil($y_ratio*$width);
$tn_height=$max_height;
}

$src = imagecreatefromjpeg($image);
$dst=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($dst, $src,0,0,0,0,$tn_width,$tn_height,$width,$height);
$create_image = imagejpeg($dst, $path . $filename, $quality);

 

This was what i tested...

http://wuhtzu.dk/test/imagemanipulator/image.php?img=humle.jpg&max_w=3000&max_h=3000&quality=75

 

Just if you wanted to "test" the script. It uses the same resizing method as i posted above.

 

Looks like you modified a script I posted!

Trium918:

Be careful with such accusations. A lot of things are done in a standard way which many individual people sooner or later will "invent" if they aren't told by anyone else. This applies to almost everything and certainly also to programming / code making. The script posted in this thread deals with the resizing of a JPEG image in the most logic way which involves basic logical steps:

 

#1: Obtain the image height and width

#2: Calculate a new height and with based on a maximum width and height which is pure logic mathematics.

#3: Create the new resized image using standard php gdlib functions.

 

So the second person to write this script isn't necessarily copying the first persons work since it is so logic and basic. But I would very much like to see the script you posted (the post containing the script of course) because it is a funny coincidence. People often discuss this whole copyright-plagiat-copy-recreating thing but I have never seen a real example of this happening.

 

The hard facts:

I have personally obtained the script (or something very similar) from page 560-561 of the second edition of the book PHP and Mysql Web Development written by Luke Welling and Laura Thomson and published by Sams Publishing, first printed in February 2003. I did this a few years back as I was teaching my self PHP and the concept of the script, logic and useful as it is, have stayed with me and I rewrote it the other day to test some stuff regarding image size (in bytes) and JPEG compression quality.

 

Here are the pages from the book:

http://wuhtzu.dk/random/php_and_mysql_web_development_c26_p560.jpg

http://wuhtzu.dk/random/php_and_mysql_web_development_c26_p561.jpg

 

So have you been reading the book too?

 

Wuhtzu :)

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.