Jump to content

image resize problem


garydt

Recommended Posts

I'm trying to resize an image before uploading it but it isn't being resized, it just uploads at its original size.

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start();
// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = 'uploads/'; 
$submit=$_POST['submit'];

if(isset($submit)){
// fieldname used within the file <input> of the HTML form 
$fieldname = 'file'; 

//Resize image
    $imgsize = GetImageSize($_FILE[$fieldname]['tmp_name']);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize[0] > 250) || ($imgsize[1] > 200)) 
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the 
            script at once they won't ruin each other's temp file ==*/
        $tmpimg = tempnam("/tmp", "MKUP");

        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type) 
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/
        
        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $fieldname >$tmpimg");
        
        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

        /*== remove temp image ==*/
        unlink($tmpimg);
    }

//Upload resized image
$uploadFilename = $uploadsDirectory.$_FILES[$fieldname]['name'];


// now let's move the file to its final location and allocate the new filename to it 
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);



$user = $_SESSION['MM_Username'];


mysql_select_db($database_elvisdb, $elvisdb);
$insertSQL = sprintf("INSERT INTO images (imageName, usnm) VALUES ('$uploadFilename', '$user')");

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());
}
echo $uploadfilename
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
     
        <h1> 
            Upload form 
        </h1> 
         
        <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p> 
         
        <p> 
            <label for="file">File to upload:</label> 
            <input id="file" type="file" name="file"> 
        </p>
        <p>
          <label>
          <input type="text" name="textfield" />
          </label>
        </p>
        <p> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form> 
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/
Share on other sites

Try to use that function: http://us2.php.net/image

 

Long time ago I made a function for myself:

<?php
function resizeimage($file) {
	if(file_exists("images/".$file)) {
		$image		= imagecreatefromjpeg("images/".$file); //unresized image
		$im_info	= getimagesize("images/".$file); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 100;
		}else {
			$im_divice	= $im_height / 100;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$file, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}
?>

I hope it helped you

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-213003
Share on other sites

I did

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start();
// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = 'uploads/'; 
$submit=$_POST['submit'];

if(isset($submit)){
// fieldname used within the file <input> of the HTML form 
$fieldname = 'file'; 
function resizeimage($_FILES[$fieldname]['name']) {
	if(file_exists($_FILES[$fieldname]['name'])) {
		$image		= imagecreatefromjpeg($_FILES[$fieldname]['name']); //unresized image
		$im_info	= getimagesize($_FILES[$fieldname]['name']); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 100;
		}else {
			$im_divice	= $im_height / 100;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$fieldname]['name'], 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

//Upload resized image
$uploadFilename = $uploadsDirectory.$fieldname['name'];


// now let's move the file to its final location and allocate the new filename to it 
@move_uploaded_file($fieldname['tmp_name'], $uploadFilename);


$user = $_SESSION['MM_Username'];


mysql_select_db($database_elvisdb, $elvisdb);
$insertSQL = sprintf("INSERT INTO images (imageName, usnm) VALUES ('$uploadFilename', '$user')");

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());
  header("Location: userpage.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
     
        <h1> 
            Upload form 
        </h1> 
         
        <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p> 
         
        <p> 
            <label for="file">File to upload:</label> 
            <input id="file" type="file" name="file"> 
        </p>
        <p>
          <label>
          <input type="text" name="textfield" />
          </label>
        </p>
        <p> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form> 
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-213099
Share on other sites

Hi!

You define a function like that:

<?php
function functionname($atr) {
   echo $atr;
} ?>

You call that function like

functionname('my atr');

Now the variable $atr has the value 'my atr'

 

You wrote, that your value is called $_FILES[$fieldname]['name'] in your function.

that doesn't work.

In your case it's better to use no function-vars.

Just make this:

function resizeimage($_FILES[$fieldname]['name']) {

to:

function resizeimage() {

 

another question: What's about that line:

if(imagejpeg($thumb, "thumbnails/".$fieldname]['name'], 80))

There is something wrong because there is no array open.

 

It seems you have to start with something easier as an uploadscript with an imageresizer because you have to move the uploaded file, befor you can resize it.

I give you some links you should read:

http://us2.php.net/manual/en/introduction.php - Here you can learn how to use PHP right.

http://us2.php.net/manual/en/function.move-uploaded-file.php - This function moves the uploaded file to a directory.

http://us2.php.net/image - This gives you a description, how to use those imagefunctions.

 

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-213349
Share on other sites

Can you tell me what I've got wrong because no image file goes into the uploads directory now and in the database the url is uploads/f  no matter what file i upload. If i don't call the function the same thing happen.

Do i need to have the GD library/program for this to work?

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start();


$submit=$_POST['submit'];
if(isset($submit)){
$fieldname = 'file';
function resizeimage(){
	if(file_exists($_FILES[$fieldname]['name'])) {
		$image		= imagecreatefromjpeg($_FILES[$fieldname]['name']); //unresized image
		$im_info	= getimagesize($image); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 100;
		}else {
			$im_divice	= $im_height / 100;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$newimage, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = 'uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
@move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

resizeimage();

$user = $_SESSION['MM_Username'];


mysql_select_db($database_elvisdb, $elvisdb);
$insertSQL = sprintf("INSERT INTO images (imageName, usnm) VALUES ('$uploadFilename', '$user')");

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());
  header("Location: userpage.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
     
        <h1> 
            Upload form 
        </h1> 
         
        <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p> 
         
        <p> 
            <label for="file">File to upload:</label> 
            <input id="file" type="file" name="file"> 
        </p>
        <p>
          <label>
          <input type="text" name="textfield" />
          </label>
        </p>
        <p> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form> 
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-213447
Share on other sites

You have to allow php that it can write in your directory. So you have to set them to 777... take a look on this picture:

file_Picture+1e822e.png

That's how the upload-directory should look like.

 

When you remove you @ before move_uploaded_file, you'll see the error.

 

For future: When you think you php-script doesn't work, remove all @ to display errors. Error-messages are a real help for developers to see, where they made a mistake.

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-213529
Share on other sites

For sure... if $_POST['submit'] was not given, it's an error.

Something's wrong with your form...

 

Try this:

Make another hidden input-tag, name it "sent" and set the value to "1".

Then replace

$submit=$_POST['submit'];

to

unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');

 

I'm sorry... now I see the mistake:

<?php
   $image = imagecreatefromjpeg($_FILES[$fieldname]['name']); //unresized image
   $im_info = getimagesize($image); //unresized image
?>

That's wrong because getimagesize needs a filename and not an image-resource. And also you need the  full path to your moved file. So:

<?php
	$filename = './my_uploaded_pictures/'.$_FILES[$fieldname]['name'];
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($_FILES[$fieldname]['name']); //unresized image
		$im_info	= getimagesize($image); //unresized image
?>

And also it has to be an JPEG-picture.

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-213794
Share on other sites

I did what you said and I get -

Access forbidden!

You don't have permission to access the requested object. It is either read-protected or not readable by the server.

 

If you think this is a server error, please contact the webmaster.

 

Error 403

localhost

03/24/07 12:15:37

Apache/2.2.2 (Win32) DAV/2 mod_ssl/2.2.2 OpenSSL/0.9.8b mod_autoindex_color PHP/5.1.4


Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-214177
Share on other sites

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start();
error_reporting(E_ALL);
if(isset($submit)){
unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');

$fieldname = 'file';
function resizeimage(){
$filename = './uploads/'.$_FILES[$fieldname]['name'];
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($image); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 100;
		}else {
			$im_divice	= $im_height / 100;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$newimage, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = 'uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

resizeimage();

$user = $_SESSION['MM_Username'];


mysql_select_db($database_elvisdb, $elvisdb);
$insertSQL = sprintf("INSERT INTO images (imageName, usnm) VALUES ('$uploadFilename', '$user')");

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());
  header("Location: userpage.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
     
        <h1> 
            Upload form 
        </h1> 
         
        <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p> 
         
        <p> 
            <label for="file">File to upload:</label> 
            <input id="file" type="file" name="file"> 
        </p>
        <p>
          <label>
          <input name="sent" type="hidden" id="sent" value="1" />
          </label>
        </p>
        <p> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form> 
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-214179
Share on other sites

Your root directory shouldn't has the permission that everyone can write.

Only your upload-directory should has the permission like the picture I pasted into my post.

 

also you didn't change that lines:

			$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($image); //unresized image

to

			$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image

 

and please make

$uploadsDirectory = 'uploads/';

to

$uploadsDirectory = './uploads/';

I think that's better.

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-214254
Share on other sites

I changed those bits and its still coming up with  access forbidden  etc.

my uploads directory has read write execute permissions.

What shall i try now?

Thank you for being patient with me, I appreciate it.

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start();
error_reporting(E_ALL);
if(isset($submit)){
unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');

$fieldname = 'file';
function resizeimage(){
$filename = './uploads/'.$_FILES[$fieldname]['name'];
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($filename); //unresized image
		$im_info	= getimagesize($filename); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 100;
		}else {
			$im_divice	= $im_height / 100;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$newimage, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = './uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

resizeimage();

$user = $_SESSION['MM_Username'];


mysql_select_db($database_elvisdb, $elvisdb);
$insertSQL = sprintf("INSERT INTO images (imageName, usnm) VALUES ('$uploadFilename', '$user')");

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());
  header("Location: userpage.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
     
        <h1> 
            Upload form 
        </h1> 
         
        <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p> 
         
        <p> 
            <label for="file">File to upload:</label> 
            <input id="file" type="file" name="file"> 
        </p>
        <p>
          <label>
          <input name="sent" type="hidden" id="sent" value="1" />
          </label>
        </p>
        <p> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form> 
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-214265
Share on other sites

omg!

http://us2.php.net/manual/en/features.file-upload.php

 

$fieldname = 'file';

Okay! Here you say, that the variable $fieldname has the value 'file'.

That's ok.

 

$uploadFilename = $uploadsDirectory.$fieldname['name'];

Here you use $fieldname as the $_FILES-Array. There is something wrong.

I changed $fieldname and gave it the value of $_FILES['file']

 

When you change that, it also doesn't work. Why?

Because of that:

if(isset($submit)){

$submit is never defined, so it's false everytime.

Change it to:

if(isset($_POST['sent'])){

That's because we defined the hidden input-object named sent.

 

Now it works...

But there are so many undefined variables and errors.

I made

function resizeimage() {

to

function resizeimage($name){

because it told me $fieldname was not set. But that's not your fault. (I think it's mine ^^)

 

$filename = './uploads/'.$_FILES[$fieldname]['name'];

That's also wrong. A function is only executing by calling. So when you load your script it executes everthing that stands not in a function. When a line is calling a function (or more) it's executing.

So $fieldname ist not 'file' in resizeimage()

Change

$filename = './uploads/'.$_FILES[$fieldname]['name'];

into

$filename = './uploads/'.$name['name'];

and on the bottom of the php-block change

resizeimage();

to

resizeimage($fieldname['name']);

 

$editFormAction and $max_file_size are not defined.

Because of the undefined $editFormAction you get to a wrong page.

I defined them under error_reporting(E_ALL);

$editFormAction = '';
$max_file_size = 1024*1024*1024; //1MB

 

Now it gets into you upload-folder

 

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start();
error_reporting(E_ALL);

$editFormAction = '';
$max_file_size = 1024*1024*1024; //1MB

if(isset($_POST['sent'])){
unset($submit);
if (isset($_POST['sent'])) $submit = true;
else die('Error: No file got sent');

$fieldname = $_FILES['file'];
function resizeimage($name){
$filename = './uploads/'.$name['name'];
	if(file_exists($filename)) {
		$image		= imagecreatefromjpeg($filename);
		$im_info	= getimagesize($filename);
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 100;
		}else {
			$im_divice	= $im_height / 100;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$newimage, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}

// make a note of the directory that will recieve the uploaded file 
// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'; 
$uploadsDirectory = './uploads/'; 

//Upload image
$uploadFilename = $uploadsDirectory.$fieldname['name'];

// now let's move the file to its final location and allocate the new filename to it 
move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

resizeimage($fieldname['name']);

$user = $_SESSION['MM_Username'];


mysql_select_db($database_elvisdb, $elvisdb);
$insertSQL = sprintf("INSERT INTO images (imageName, usnm) VALUES ('$uploadFilename', '$user')");

$Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());
header("Location: userpage.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST"> 
     
        <h1> 
            Upload form 
        </h1> 
         
        <p> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p> 
         
        <p> 
            <label for="file">File to upload:</label> 
            <input id="file" type="file" name="file"> 
        </p>
        <p>
          <label>
          <input name="sent" type="hidden" id="sent" value="1" />
          </label>
        </p>
        <p> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p> 
     
        <input type="hidden" name="MM_insert" value="form1">
</form> 
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/43808-image-resize-problem/#findComment-214289
Share on other sites

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.