Hello,

I am currently able to upload an image and the info is being inserted into the db correctly.

I would like to add a gd function to add 3 image re-sizes into it.

locations and sizes
/prop_enlarged/ max 600px 400px
/prop_listing/ max 405px 270px
/prop_thumb/ max 135px 90px

I would love any help,
Matthew

<?php include('headera.php'); ?>
 
<?php
$propid = $_SESSION['propid'];
        
// define constant which contains the maximum file size in bytes
define('MAX_FILE_SIZE', 4000000);
 
if (array_key_exists('btn', $_POST)) {
 
// define new constant which contains the path the the upload folder
define('UPL_FLD','uploads/');
 
//Find the extension
$flext = pathinfo($_FILES['frmfile']['name']);
$ext = strtolower($flext['extension']);
 
 
// create new file name
$file = str_replace(' ', '_', $_POST['frmname'].'.'.$ext);
$file = strtolower($file);
 
// create variable and assign the formatted value of MAX_FILE_SIZE to it
$maxfs = number_format(MAX_FILE_SIZE/1024, 1).'KB';
$fsize = false;
 
// check the file size
if ($_FILES['frmfile']['size'] > 0 && $_FILES['frmfile']['size'] <= MAX_FILE_SIZE) {
 
        $fsize = true;
 
}
 
// allow MIME file types
$filetype = array('image/gif','image/jpeg','image/pjpeg','image/png');
$ftype = false;
 
// check if uploaded file type is allowed
foreach($filetype as $type) {
 
        if ($type == $_FILES['frmfile']['type']) {
        
                $ftype = true;
                break;
        
        }
 
}
 
if ($ftype && $fsize && $_POST['frmname'] != '') {
 
        switch($_FILES['frmfile']['error']) {
        
                case 0:
                // move file to the upload folder
                $upload = move_uploaded_file($_FILES['frmfile']['tmp_name'],UPL_FLD.$file);
 
 
                if ($upload) {
                
                        $msg = $_FILES['frmfile']['name'].' uploaded successfully';
                        
                        $img_name = $file;
                        
                        $query = "SELECT `propid` FROM `images` WHERE `propid` = $propid";
                                                $result = mysql_query($query);
                                                if (mysql_num_rows($result) > 0) {
 
                                                $sql = "INSERT into images (propid,userid,img_name,mainImage) VALUES ('$propid','$userid','$img_name','N')";
                        $result = mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);  //do the query
                        
                                                         } else {
     
                                                $sql = "INSERT into images (propid,userid,img_name,mainImage) VALUES ('$propid','$userid','$img_name','Y')";
                        $result = mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);  //do the query
     
     
                                                        }
                                        
                                                
                     
 
                                        
                } else {
                
                        $msg = 'Error.<br />Please try again.';
                }
                break;
                
                case 3:
                $msg = 'Error.<br />Please try again.';
                break;
                
                default:
                $msg = 'Error - please contact administrator';
                
        }
                
                
} elseif ($_FILES['frmfile']['error'] == 4) {
 
        $msg = 'Please select file';
 
} elseif ($_POST['frmname'] == '') {
 
        $msg = 'Please provide your full name';
        
} else {
 
        $msg = $_FILES['frmfile']['name'].' cannot be uploaded.<br />';
                        if(!$ftype) {
                                $msg .= 'Acceptable file formats are: .gif, .jpg, .png<br />';
                        }
                        if(!$fsize) {
                                $msg .= 'Maximum file size is '.$maxfs;
                        }
 
}
 
}
?>
 
 
 
<div id="memberblock">
<div id="memberleft">
<?php include('control_panel_tools.php'); ?>
</div>
<div id="memberright">
<h1>Upload Images</h1>
 
 
 
<?php if(isset($msg)) { echo '<p class="warning">'.$msg.'</p>'; } ?>
 
<form action="" method="post" enctype="multipart/form-data" name="frm_upload" id="frm_upload">
<input type="hidden" name="frmname" value="<?php echo date("YmdHis") ?>" id="frmname"  />
<input type="hidden" name="propid" value="<?=$propid?>" />
<label >File:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input name="frmfile" type="file" id="frmfile" size="30" />
<input type="submit" name="btn" id="btn" value="Upload" />
</form>
 
<?php echo $userid; ?>
 
        </div>
<?php include('footera.php'); ?>

Recommended Answers

All 2 Replies

Although I don't know about putting the picture into the database, I have made a function that will resize the picture for you. The script is:

<?php
function resize_image($filename,$newwidth,$newheight) {
    
    if (!file_exists($filename) || !in_array(preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename)),array('png','jpg','jpeg','gif','xbm','xpm','wbmp'))) {
        //note .wbmp is 'wireless bitmap' and not 'windows bitmap'.
        return false;
        } else {
        $ext=preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename));
        if ($ext=='jpg') { $ext='jpeg'; }
        $ext='imagecreatefrom'.$ext;
        list($width, $height) = getimagesize($filename);
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = $ext($filename);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        return $thumb;
        }
    }
$img = resize_image('file_from.jpg','320','280');
//save image to file
imagegif ($img,'file.gif');
?>

And the above function supports all the gd supported file formats which include png, jpg, jpeg, gif, xbm, xpm, and wbmp. Also note that wbmp is not windows bitmap but instead wireless bitmap. So many people including myself have found the name misleading when first discovering the function. Hope that helps.

Changing imagecopyresized (line 14) to imagecopyresampled (leave the parameters the same) in the resize_image function should give you somewhat better quality.

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.