PDA

View Full Version : i get the error 'Warning: imagecopy(): 4 is not a valid Image resource in...'


Adam_C
11-22-2009, 11:11 AM
in my code:

<?php
////////////////////////////////////// connection script - enter your details here
$database="portfolio";
$con = mysql_connect("localhost", "root", "password") or die(mysql_error()); mysql_select_db("portfolio") or die(mysql_error());?>
<script type="text/javascript">alert("Connected to MySQL and Database <?php echo ($image) ?> :)")</script>
<?php
////////////////////////////////error handling - if an error is detected in the url the appropriate error will be displayed
$error = $_GET['error'];
if($error == 1){
$error = "Unknown Image extension!";
}elseif($error == 2){
$error = "You have exceeded the size limit";
}else $error = '';

function getExtension($str) {// function to get file extension

$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// define max file size, change to suit your own maximum file size
define ("MAX_SIZE","1000");
$errors=0;
//get the original file from form
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image = $_FILES["file"]["name"];// from your upload form
$uploadedfile = $_FILES['file']['tmp_name'];// from your upload form
// check for correct file extension
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{

header("location:upload.php?error=1");// error handling here - if the file ext is wrong the script will exit and redirect with error in the url
exit;///////////////////////////////// note that I have used adam.php as the file name, change this if you save the file differently
}
// check for correct file size
else
{
$size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024)
{

header("location:upload.php?error=2");// error handling here - if the file size is wrong the script will exit and redirect with error in the url
exit;///////////////////////////////// note that I have used adam.php as the file name, change this if you save the file differently
}
// check which extension we have and create file
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
//declare destinations
$filename = "uploads/large/". $_FILES['file']['name'];
//write files to folders
imagejpeg($src,$filename,100);
//destroy temps to save memory
imagedestroy($src);
}
}
}
?>
<!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=utf-8" />
<title>Adam Carter | Portfolio</title>
<link href="main.css" type="text/css" media="screen" rel="stylesheet"/>
<link href="styles.css" type="text/css" media="screen" rel="stylesheet" />
<link href="upload.css" type="text/css" media="screen" rel="stylesheet" />
<script type="text/javascript" src="mootools/mootools.js"></script>
<script type="text/javascript" src="mootools/mooefx.js"></script>
<link href="mootools/menu.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header"></div>
<div id="content">
<div id="sidebar">
<ul>
<li class="home"><a href="index.php" >Home</a></li>
<li class="about"><a href="about.php" >About</a></li>
<li class="portfolio"><a href="portfolio.php" >Portfolio</a></li>
<li class="contact"><a href="contact.php" >Contact</a></li>
<li><a href="upload.php">Upload</a></li>
</ul>
</div>
<div id="maincontent">
<h3 class="mainheader">Upload</h3>
<br />
<br />
<br />
<div id="uploadholder"> <?php echo $success; ?><?php echo $error; ?>
<form name="upload" enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<!--10MB-->
<h3 class="uploadhead">Enter file information: </h3>
<div class="linebreak"></div>
<br />
<span class="space">Main File: &nbsp;
<input name="file" type="file" title="Upload Large File" value="Upload Large File"/></span>
<br />
<span class="space">File Name: &nbsp;
<input name="name" maxlength="20" style="width:250px;" type="text" alt="Name" value=" (20 Char Max)" onClick="if (this.value=' (20 Char Max)') { this.value=''}" /></span>
<br />
<span class="space">Short Info: &nbsp;
<input name="shortinfo" maxlength="30" style="width:250px;" type="text" alt="Short Info" value=" (30 Char Max)" onClick="if (this.value=' (30 Char Max)') { this.value=''}" /></span>
<br />
<span class="space">Long Info: &nbsp;
<input name="longinfo" maxlength="100" style="width:250px;" type="text" alt="Long Info" value=" (100 Char Max)" onClick="if (this.value=' (100 Char Max)') { this.value=''}" /></span>
<br />
<br />
<br />
<input type="submit" name="Submit" id="Submit" value="Upload File" />
</form>
<?php
//Get number of files in the db
$renamesql = mysql_query("SHOW TABLES")or die(mysql_error());

while($fetch = mysql_fetch_array($renamesql)){
$renamesql2 = mysql_query("SELECT * FROM `logos` ORDER BY `logos`.`id` DESC LIMIT 1")or die(mysql_error());

while($row = mysql_fetch_array($renamesql2)){
$newfilenamewithoutext=$row['id'];
}
}

//Rename old file to number
$filenameold= $_FILES['file']['name'];
$oldestfilename= $_FILES['file']['tmp_name'];
$oldfilename = $filename;
$newfilename="uploads/large/". $newfilenamewithoutext. ".". $extension;
rename($oldfilename, $newfilename) or die(mysql_error());

//create icon
$dest_im = imagecreatetruecolor(180,160);
$src_im = $src;
$icon=imagecopy($dest_im,$src, 0, 0, 0, 13, 180, 160);

//If no errors registered, insert file name into database and print the success message
$name= $_POST['name'];
$shortinfo= $_POST['shortinfo'];
$longinfo= $_POST['longinfo'];

if(isset($_POST['Submit']) && !$errors)
{
mysql_query("INSERT INTO `portfolio`.`logos` (`id`, `name`, `timestamp`, `shortinfo`, `longinfo`, `file`, `icon`) VALUES (NULL, '$name', CURRENT_TIMESTAMP, '$shortinfo', '$longinfo', '$image', '$icon')");

$success = "Image Uploaded Successfully!";
}
//////////////////////////////////////////////////select file names from database
$query = sprintf("SELECT * FROM logos ");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
?>
</div>
</div>
</div>
</div>
</body>
</html>

i get the error: Warning: imagecopy(): 4 is not a valid Image resource in C:\XAMPP\htdocs\Portfolio\upload.php on line 155

why is this and how do i resolve it? thanks, Adam. :)

edbr
11-23-2009, 01:12 AM
is image copy a function you have forgotten to include perhaps?

Adam_C
11-23-2009, 03:51 PM
is image copy a function you have forgotten to include perhaps?

where and how would i add it? :)

edbr
11-24-2009, 01:55 AM
put it a t the top of the page or in an included file.

Adam_C
11-24-2009, 02:14 PM
put it a t the top of the page or in an included file.

what would i write too?

gentleone
11-24-2009, 02:36 PM
Did you look at php.net?
http://php.net/manual/en/function.imagecopy.php

Adam_C
11-24-2009, 03:25 PM
Did you look at php.net?
http://php.net/manual/en/function.imagecopy.php

yeah, i did look at it but i dont understand what i have to put and where i have to put it :)

fyi i am quite new to php :P

domedia
11-24-2009, 03:30 PM
fyi i am quite new to php :P
Sounds like you're at a perfect spot to start learning then :)
Pick up a PHP book at your local book store, or search Amazon for one.

Adam_C
11-24-2009, 03:38 PM
Sounds like you're at a perfect spot to start learning then :)
Pick up a PHP book at your local book store, or search Amazon for one.

i am learning by making websites and such :)

all i need to know is where to put the function so i can keep learning ! :P

:)

domedia
11-24-2009, 08:09 PM
Doesn't make sense to me, but good luck either way!

edbr
11-25-2009, 12:33 AM
i hadnt come across that function before, it is part of php4 ad 5 soit would seem more likely you have perhaps made an error in the script. check the error line and it will help you learn then