Jump to content

[SOLVED] Resizing Photos / Images once Uploaded


RhysAndrews

Recommended Posts

Hey everyone.

I've googled the hell out of this, and while I've found a lot of solutions, they were all either too in-depth or too hard to understand.

 

I use this script to upload photos - the form has 6 upload fields, so the user can upload up to 6 JPG photos at once. It checks each file and moves it to the directory. All I need added to the script is for it to resize the JPG first - I want the larger axis to be resized to 600, and the proportions to remain the same - I don't want it to be upsized, though. So, for instance:

 

- 600x1000 photo will be resized to 360x600

- 200x200 photo will not be resized

- 1000x600 photo will be resized to 600x360

 

Here is my code - could someone help me add this feature to the code without altering it too much? Thanks so much! The names of the upload fields in the form are imgfile1, imgfile2, and so on.

<?php
if ($_GET["action"]==1)
{
$album = $_POST["album"];
$album = stripcslashes($album);

$allowedExtensions = array("jpg", "jpeg");
for($i=1; $i<7; $i+=1)
{
	if (!empty($_FILES["imgfile$i"]["name"]))
	{
		$filename = basename($_FILES["imgfile$i"]["name"]);

		if (in_array(end(explode(".", $filename)), $allowedExtensions))
		{
			//Find destination directory
			$targetpath="../photos/$album/";
			$targetpath = $targetpath . $filename;

			if(move_uploaded_file($_FILES["imgfile$i"]["tmp_name"], $targetpath)) {
				echo "<SPAN style='color:lime;'>The file $filename has been uploaded successfully!</SPAN><BR>";
			} else{
				echo "<SPAN style='color:red;'>There was an error uploading $filename - please try again!</SPAN><BR>";
			}
		}
		else
		{
			echo "<SPAN style='color:red;'>$filename has an invalid extension - only JPG and PNG files are allowed!</SPAN><BR>";
		}
	}
}
}
?>

 

good example gd theo.

<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

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

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

// Output
imagejpeg($image_p, null, 100);
?>

Hi redarrow

The code looks like it should work, but if I use that header code it tells me the header has already been set. And if I take it out... I get a very long page full of symbols. This is my code:

 

<?php
if ($_GET["action"]==1)
{
$album = $_POST["album"];
$album = stripcslashes($album);

for($i=1; $i<7; $i+=1)
{
	if (!empty($_FILES["imgfile$i"]["name"]))
	{
		//header('Content-type: image/jpeg');
		list($width_orig,$height_orig) = getimagesize($_FILES["imgfile$i"]["tmp_name"]);
		$ratio_orig = $width_orig/$height_orig;

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

		$image_p = imagecreatetruecolor($width,$height);
		$image = imagecreatefromjpeg($_FILES["imgfile$i"]["tmp_name"]);
		imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
		imagejpeg($image_p,null,100);

		$filename = basename($_FILES["imgfile$i"]["name"]);

		if (end(explode(".", $filename))=="jpg")
		{
			//Find destination directory
			$targetpath="../photos/$album/";
			$targetpath = $targetpath . $filename;

			if(move_uploaded_file($image_p, $targetpath)) {
				echo "<SPAN style='color:lime;'>The file $filename has been uploaded successfully!</SPAN><BR>";
			} else{
				echo "<SPAN style='color:red;'>There was an error uploading $filename - please try again!</SPAN><BR>";
			}
		}
		else
		{
			echo "<SPAN style='color:red;'>$filename has an invalid extension - only JPG and PNG files are allowed!</SPAN><BR>";
		}
	}
}
}
?>

Could you help me out? Thanks!

-Rhys

put the header('Content-type: image/jpeg'); part outside your html tags at the top.

 

header('Content-type: image/jpeg');

<html>

<head>

</head>

<body>

</body>

</html>

 

if it's allowed. this should work, i think....

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.