PDA

View Full Version : how and where to insert image resize function



vinpkl
09-15-2016, 03:11 PM
hi

i am trying to add image resize function in simple image upload form script.

i created a simple upload script which works fine and uploads image.

i m not involving validation at present just for testing.


<?php
if($_FILES['photo']['name'])
{
echo $file_name = $_FILES['photo']['name'];
echo "<br>";
echo $file_tmp =$_FILES['photo']['tmp_name'];

move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Your Photo: <input type="file" name="photo" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>


now i have this code that resizes the image


// The file
$filename = 'images/s1.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);


so i tried to combine them as they both work fine independently


<?php
if($_FILES['photo']['name'])
{
echo $file_name = $_FILES['photo']['name'];
echo "<br>";
echo $file_tmp =$_FILES['photo']['tmp_name'];

//resize function starts
$filename = 'images/'.$file_name;

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

// 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);
// resize function ends

move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Your Photo: <input type="file" name="photo" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>


so i have two questions related to above combined code

1) The resample or resize function code will come before or after "move_uploaded_file" function

2) what will i write in the "move_uploaded_file" function so that resized thumbnail gets uploaded

vineet