runnerjp Posted November 29, 2011 Share Posted November 29, 2011 I get the following errors Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 6 Warning: imagesx(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 49 Warning: Division by zero in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 63 Warning: imagesy(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 53 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 75 Warning: imagesx(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 49 Warning: imagesy(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 53 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 76 Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 22 but as far as i am awear the code should work :S <?php class imageupload { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?> with the following in my upload page if( isset($_POST['submit']) ) { include('imageupload.php'); $image = new imageupload(); $image->load($_FILES['uploaded_image']['tmp_name']); $image->resizeToWidth(250); $image->save('picture2.jpg'); } Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/ Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2011 Share Posted November 29, 2011 Did you remember to include enctype="multipart/form-data" in your <form> tag? Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292186 Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2011 Share Posted November 29, 2011 Or are you checking if the upload worked without any errors before you attempt to use the uploaded file information in the rest of your code? Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292187 Share on other sites More sharing options...
runnerjp Posted November 29, 2011 Author Share Posted November 29, 2011 Hello, As you can see from the below code i use enctype="multipart/form-data" also i took your advice PFMaBiSmAd and added if( isset($_POST['submit']) ) { if( $_FILES['uploaded_image']['error'] === UPLOAD_ERR_OK ) { include('imageupload.php'); $image = new imageupload(); $image->load($_FILES['uploaded_image']['tmp_name']); $image->resizeToWidth(250); $image->save('picture2.jpg'); } else { echo 'Error uploading file.'; } } it seems i get an error... how can i go about finding this error? <?php require_once('../../Connections/dbadmin.php'); ?> <?php //initialize the session if (!isset($_SESSION)) { session_start(); } // ** Logout the current user. ** $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true"; if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){ $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){ //to fully log out a visitor we need to clear the session varialbles $_SESSION['MM_Username'] = NULL; $_SESSION['MM_UserGroup'] = NULL; $_SESSION['PrevUrl'] = NULL; unset($_SESSION['MM_Username']); unset($_SESSION['MM_UserGroup']); unset($_SESSION['PrevUrl']); $logoutGoTo = "login.php"; if ($logoutGoTo) { header("Location: $logoutGoTo"); exit; } } ?> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = ""; $MM_donotCheckaccess = "true"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && true) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "login.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = $_SERVER['PHP_SELF']; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) $MM_referrer .= "?" . $_SERVER['QUERY_STRING']; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } // Validation $error = array(); $MM_flag="MM_insert"; if (isset($_POST[$MM_flag])) { // Check headline if (empty($_POST['topical_headline'])) { array_push($error, 'topical_headline');} // Check topical content if (empty($_POST['topical_detail'])) { array_push($error, 'topical_detail');} // Check display order if (empty($_POST['topical_order'])) { array_push($error, 'topical_order');} //add Image if( isset($_POST['submit']) ) { if( $_FILES['uploaded_image']['error'] === UPLOAD_ERR_OK ) { include('imageupload.php'); $image = new imageupload(); $image->load($_FILES['uploaded_image']['tmp_name']); $image->resizeToWidth(250); $image->save('picture2.jpg'); } else { echo 'Error uploading file. Please try again later.'; } } } if (!$error) { if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "addtopicalform")) { $insertSQL = sprintf("INSERT INTO topical (topical_headline, topical_detail, topical_readmore, topical_img, topical_order, topical_published) VALUES (%s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['topical_headline'], "text"), GetSQLValueString($_POST['topical_detail'], "text"), GetSQLValueString($_POST['topical_readmore'], "text"), GetSQLValueString($_POST['topical_img'], "text"), GetSQLValueString($_POST['topical_order'], "int"), GetSQLValueString($_POST['topical_published'], "text")); mysql_select_db($database_dbadmin, $dbadmin); $Result1 = mysql_query($insertSQL, $dbadmin) or die(mysql_error()); $insertGoTo = "topical_list.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } } ?> <!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"><!-- InstanceBegin template="/Templates/admin.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- InstanceBeginEditable name="doctitle" --> <title>Dearne Valley Group Practice | Admin Home</title> <script type="text/javascript" src="../tinymce/jscripts/tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ theme : "advanced", mode: "textareas", plugins : "", theme_advanced_buttons1 : "", theme_advanced_buttons2 : "", theme_advanced_buttons3 : "", theme_advanced_toolbar_location : "top", theme_advanced_buttons1 : "forecolor, fontsizeselect,bold,italic,underline,separator,bullist,numlist,separator,link,unlink", height:"200px", width:"620px" }); </script> <!-- InstanceEndEditable --> <link href="../css/admin.css" rel="stylesheet" type="text/css" /> <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <div id="container"> <div id="header"> <div id="dvgplogo"> <h1 id="DVGP"><span>Dearne Valley Group Practice</span></h1> </div> <div id="hdrtext"><h2>ADMINISTRATION AREA</h2></div> <div class="clear"></div> </div> <div id="navigation"> <ul> <li><a href="adminhome.php">Home</a></li> <li><a href="news_list.php">News Items</a></li> <li><a href="topical_list.php">Topical Items</a></li> <li><a href="practice_team_update.php">Practice Team</a></li> <li><a href="changepwd.php">Change Password</a></li> <li><a href="<?php echo $logoutAction ?>">Log Out</a></li> </ul> </div> <div id="contentwrapper"><!-- InstanceBeginEditable name="admincontent" --> <h1>Add Topical Item</h1> <p>Please complete the following fields to add a topical item.</p> <form action="<?php echo $editFormAction; ?>" name="addtopicalform" id="addtopicalform" enctype="multipart/form-data" method="POST"> <fieldset> <div id="rightform"> <div class="formfield"> <?php if (isset($error) && in_array('topical_order', $error)) { ?><div class="warning">Please enter a display order</div><?php } ?> <label for="topical_order" class="formlabel2">Display Order:</label><br /> <input name="topical_order" type="text" maxlength="2" class="textfieldvshort" <?php if (isset($error)) {echo 'value="'.htmlentities($_POST['topical_order']).'"' ;} ?>/> </div> <div class="formfield"> <label for="topical_headline" class="formlabel2">Publication Status:</label> <div id="whenpublish"> <label><input name="topical_published" type="radio" id="topical_published_0" value="y" checked="checked" <?php if ($_POST['topical_published'] == 'y') {echo 'checked="checked"';} ?>/>Published </label> <br /> <label><input type="radio" name="topical_published" value="n" id="topical_published_1" <?php if ($_POST['topical_published'] == 'n') {echo 'checked="checked"';} ?>/>Pending / Archived</label> </div> </div> <div class="formfield"> <label for="topical_img" class="formlabel">Select associated image (optional):</label><br /> <input name="topical_img" type="file" id="topical_img" /> </div> <div id="submitform"> <input name="submit" type="submit" value="Save" class="submitbutton" /> </div> </div> <div id="leftform"> <div class="formfield"> <?php if (isset($error) && in_array('topical_headline', $error)) { ?><div class="warning">Please enter a headline</div><?php } ?> <label for="topical_headline" class="formlabel1">Headline: </label><br /> <input name="topical_headline" type="text" maxlength="250" class="textfieldlong" <?php if (isset($error)) {echo 'value="'.htmlentities($_POST['topical_headline']).'"' ;} ?>/> </div> <div class="formfield"> <?php if (isset($error) && in_array('topical_detail', $error)) { ?><div class="warning">Please enter the details</div><?php } ?> <label for="topical_detail" class="formlabel1">Details to show on the home page: </label><br /> <textarea name="topical_detail" id="topical_detail"><?php if (isset($error)) {echo htmlentities($_POST['topical_detail']);} ?></textarea> </div> <div class="formfield"> <label for="topical_readmore" class="formlabel1">Addtional details to show on an extra page (optional): </label><br /> <textarea name="topical_readmore" id="topical_readmore"><?php if (isset($error)) {echo htmlentities($_POST['topical_readmore']);} ?></textarea> </div> </div> <div class="clear"></div> </fieldset> <input type="hidden" name="MM_insert" value="addtopicalform" /> </form> <!-- InstanceEndEditable --> </div> </div> </body> <!-- InstanceEnd --></html> Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292190 Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2011 Share Posted November 29, 2011 The name of your form field isn't uploaded_image Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292196 Share on other sites More sharing options...
runnerjp Posted November 29, 2011 Author Share Posted November 29, 2011 i chnage it to its correct one if( isset($_POST['submit']) ) { if( $_FILES['topical_img']['error'] === UPLOAD_ERR_OK ) { include('imageupload.php'); $image = new imageupload(); $image->load($_FILES['topical_img']['tmp_name']); $image->resizeToWidth(250); $image->save('picture2.jpg'); } else { echo 'Error uploading file. Please try again later.'; } } and i get the errors again Warning: imagesx(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 49 Warning: Division by zero in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 63 Warning: imagesy(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 53 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 75 Warning: imagesx(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 49 Warning: imagesy(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 53 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 76 Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/lgray3/public_html/DVGP/admin/imageupload.php on line 22 Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292199 Share on other sites More sharing options...
runnerjp Posted November 29, 2011 Author Share Posted November 29, 2011 how do i set the destination folder for the image? Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292285 Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2011 Share Posted November 29, 2011 You supply the path as part of the filename parameter when you call the ->save() method. Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292287 Share on other sites More sharing options...
runnerjp Posted November 29, 2011 Author Share Posted November 29, 2011 i tired the blow with function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { $filename = "images/". $_FILES['file']['name']; imagejpeg($tmp,$filename,100); chmod($filename,$permissions); } but it does not work :S <?php class imageupload { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { $filename = "images/". $_FILES['file']['name']; imagejpeg($tmp,$filename,100); chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292325 Share on other sites More sharing options...
runnerjp Posted November 29, 2011 Author Share Posted November 29, 2011 got it in the end Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292334 Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2011 Share Posted November 29, 2011 You should not be editing the class code. That makes it 'hard coded' to do only one thing, instead of being general purpose so that it can be reused without needing to edit/test it just because the location needs to change for a different application. Quote Link to comment https://forums.phpfreaks.com/topic/252039-getting-errors-when-trying-to-upload-an-image/#findComment-1292372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.