If I use this:
Code: Select all
if(strlen($image)<2){
// Image Handling
// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=177;
$newheight=($height/$width)*177;
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// now write the resized image to disk. The upload path is the first part of $filename
$filename = "../images/uploaded/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
// kill the temp files
imagedestroy($src);
imagedestroy($tmp);
}else{
$filename = "../images/picture-na.jpg";
}
Then I can get the new file to display, but if I don't upload anything I just get errors, so it's not defaulting to the blank image.
These are the errors I get:
Code: Select all
Warning: Division by zero in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 47
Warning: imagecreatetruecolor(): Invalid image dimensions in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 48
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 52
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 56
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 59
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 60
Which are caused because I didn't upload an image, but it's still executing them instead of skipping them.
If I change
to
It will ONLY display the "no image found" image.
This is the full script:
Code: Select all
<?php
require_once('global.php');
$cookie = $_COOKIE['Cookie'];
if ($cookie = "true"){
//get ID from URL
$EDITid = $_POST['EDITid'];
// Data Handling
$date = str_replace("-", ".", $_POST['date']); //Date
$street = str_replace("-", ".", $_POST['street']); //Street
$city = str_replace("-", ".", $_POST['city']); //City
$bedrooms = str_replace("-", ".", $_POST['bedrooms']); //Bedrooms
$baths = str_replace("-", ".", $_POST['baths']); //Baths
$parking = str_replace("-", ".", $_POST['parking']); //Parking
$movein = str_replace("-", ".", $_POST['movein']); //Move In Date
$price = str_replace("-", ".", $_POST['price']); //Price
$security = str_replace("-", ".", $_POST['security']); //Security Deposit
$pets = str_replace("-", ".", $_POST['pets']); //Pets
$petfee = str_replace("-", ".", $_POST['petfee']); //Pet Fee
$sqfeet = str_replace("-", ".", $_POST['sqfeet']); //Square Feet
$year = str_replace("-", ".", $_POST['year']); //Year Constructed
$image = str_replace(" ", "", $_POST['uploadfile']); //Image
$status = str_replace("-", ".", $_POST['status']); //Status
$sign = str_replace("-", ".", $_POST['sign']); //Sign Displayed
$hometype = str_replace("-", ".", $_POST['hometype']); //Type of Home
if(strlen($image)<2){
// Image Handling
// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=177;
$newheight=($height/$width)*177;
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// now write the resized image to disk. The upload path is the first part of $filename
$filename = "../images/uploaded/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
// kill the temp files
imagedestroy($src);
imagedestroy($tmp);
}else{
$filename = "../images/picture-na.jpg";
}
// connect to the database
db_connect();
$query = "UPDATE units SET date='$date', street='$street', city='$city', bedrooms='$bedrooms', baths='$baths', parking='$parking', movein='$movein', price='$price', security='$security', pets='$pets', petfee='$petfee', sqfeet='$sqfeet', year='$year', image='$filename', status='$status', sign='$sign', type='$hometype' WHERE id='$EDITid'";
//check for errors
$results = mysql_query($query) or die ("Could not execute query : $query." . mysql_error());
if ($results){
//success! Echo the $filename variable to access the new image!
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head profile="http://www.w3.org/2000/08/w3c-synd/#"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Revive Media Services" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>D and D Admin</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css" />
<link rel="stylesheet" type="text/css" href="../css/style.css" />
</head>
<body>
<div id="containeradmin">
<div id="adminmenu">
<ul id="adminnav">
<li><a href="index-true.php">Admin Home</a></li>
<li><a href="add.php">Add New Location</a></li>
</ul><div class="clear"></div>
</div>
<div id="container">
<h1>Data Submitted.</h1>
</div>
</div>
</body>
</html>
<?
}
}else{echo "fail";}
?>