Jump to content

resize image problem


garydt

Recommended Posts

I want to resize an image before i upload it but i'm getting errors-

Warning: getimagesize(file) [function.getimagesize]: failed to open stream: No such file or directory in C:\Program Files\xampp\htdocs\epeople\uploadforom.php on line 11

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\epeople\uploadforom.php:11) in C:\Program Files\xampp\htdocs\epeople\uploadforom.php on line 45

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

// fieldname used within the file <input> of the HTML form 
$fieldname = 'file'; 

//Resize image
    $imgsize = GetImageSize($fieldname);

    /*== 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);

session_start();

$user = $_SESSION['MM_Username'];

$submit=$_POST['submit'];

if(isset($submit)){
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/43693-resize-image-problem/
Share on other sites

For saftey reasons put session_start(); at the top of the page to avoid the session_Start error.

 

Can we see the code for the function GetImageSize($param); ??

 

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start(); // Session start should always be before any output.

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

// fieldname used within the file <input> of the HTML form 
$fieldname = 'file'; 

//Resize image
    $imgsize = GetImageSize($fieldname);

    /*== 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'];

$submit=$_POST['submit'];

if(isset($submit)){
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/43693-resize-image-problem/#findComment-212092
Share on other sites

So you are right, do this than:

 

<?php require_once('Connections/elvisdb.php'); ?>
<?php
session_start(); // Session start should always be before any output.

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

// 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'];

$submit=$_POST['submit'];

if(isset($submit)){
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>

 

Changed declaration of GetImageSize, see if that works.

 

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

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

Found this on the getimagesize php page in the user comments http://us2.php.net/manual/en/function.getimagesize.php

getimagesize

boshka at gmail dot com

01-Feb-2007 05:40

I was trying to workaround with the problem of getting the mime type of the image from the raw data (the images data is stored in a database and the mime type is not known in advance). Since getimagesize requires a file name, there are some ways to deal with it:

1. call getimagesize with a URL which points to the image - this is too slow.

2. use PHP file i/o wrapper class.

3. use temporary files. The code for #3 could be as follows:

 

   function getimagesize_raw($data){
       $cwd = getcwd(); #get current working directory
       $tempfile = tempnam("$cwd/tmp", "temp_image_");#create tempfile and return the path/name (make sure you have created tmp directory under $cwd
       $temphandle = fopen($tempfile, "w");#open for writing
       fwrite($temphandle, $data); #write image to tempfile
       fclose($temphandle);
       $imagesize = getimagesize($tempfile); #get image params from the tempfile
       unlink($tempfile); // this removes the tempfile
       return $imagesize;
}

 

Maybe that will help.

Link to comment
https://forums.phpfreaks.com/topic/43693-resize-image-problem/#findComment-212127
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.