Jump to content

Recommended Posts

OK.  I have tried to make my own image upload script which checks if the image is  bigger the 500px in height.  If it is shrinks the image down then uploads it an saves it to a database.

 

I got the upload an save to database working fine.  Once I added the check size bits it just fails with no error.  Does anybody know of any simple scripts or tuts out there that do this as Im fed up of banging my head against the wall.  Ive tried to find one myself but they dont exist it would seem!!

 

heres my code anyway just incase its fixable..

I get no error an no redirect,  I dont think its even making it to the move upload file as its not appearing on my server

 

<?php 
if(isset($_POST['submit'])){

$fileName9 = trim($_FILES['imagefile']['name']);
$tmpName = $_FILES['imagefile']['tmp_name'];
$fileType = $_FILES['imagefile']['type'];

//re name the image



$randName = md5(rand() * time());
$fileName = $randName.$fileName9;


//where to save the image
$folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/{$members['county']}/";


//check types
$types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png');
if (in_array($fileType, $types)) {



//get original width and height 
$size = @GetImageSize($fileName); 
$width = $size[0]; 
$height = $size[1]; 

// auto adjust width of image according to predetermined height 
$new_height = 500; 
$new_width = $width * ($new_height/$height); 

// Resample image 
$image_resized = imagecreatetruecolor($new_width, $new_height); 
imagecopyresampled($image_resized, $fileName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// New image 
$image = $image_resized; 
}



// Your file handing script here
if(move_uploaded_file($tmpName , $folder.$image)) {


$qInsert = mysql_query("UPDATE `members` 

SET profilePic = '$image' WHERE userID = '".$members['userID']."'") or die (mysql_error());


} 





if ($qInsert){


$url = "/members/clubs/image.php?pic=$image";        


header("Location: $url");  


}







}




?>
        
        
   
      <form action="" method="post" enctype="multipart/form-data" name="photo" id="photo">
      

<input name="imagefile" type="file">
        <input name="submit" type="submit" id="submit">
      </form>



Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/
Share on other sites

ok here is the new script with my error from the error reporting.  I dont understand any of them

 

if(isset($_POST['submit'])){

$fileName = $_FILES['imagefile']['name'];
$tmpName = $_FILES['imagefile']['tmp_name'];
//where to save the image
$folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/";

//get original width and height 
$size = @GetImageSize($fileName); 
$width = $size[0]; 
$height = $size[1]; 

// auto adjust width of image according to predetermined height 
$new_height = 500; 
$new_width = $width * ($new_height/$height); 

// Resample image 
$image_resized = imagecreatetruecolor($new_width, $new_height); 
imagecopyresampled($image_resized, $fileName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// New image 
$image = $image_resized; 

 

my errors are

 

Division by zero in /home/sites/image.php on line 264

 

which is this line.........

$new_width = $width * ($new_height/$height);

 

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/sites/image.php on line 267

 

which is this line.......

$image_resized = imagecreatetruecolor($new_width, $new_height);

 

finally

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/sites/image.php on line 268

 

which is this line.......

imagecopyresampled($image_resized, $fileName, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300489
Share on other sites

Don't put @ on getimagesize(), and you would see another error. You are giving getimagesize() the image name, like, "image.jpg" but you aren't actually giving it the image - just a string containing its name.

 

So you need to give it the tmp_name instead.

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300497
Share on other sites

ok thanks guys made those changes and left with one error.

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/sites/image.php on line 268

 

which is now this line.....

 

imagecopyresampled($image_resized, $tmpName, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

 

 

 

Here is the ammended code.

 

<?php

error_reporting(-1);
ini_set('display_errors', 1);


if(isset($_POST['submit'])){

$fileName = $_FILES['imagefile']['name'];
$tmpName = $_FILES['imagefile']['tmp_name'];
//where to save the image
$folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/";

//get original width and height 
$size = GetImageSize($tmpName); 
$width = $size[0]; 
$height = $size[1]; 

// auto adjust width of image according to predetermined height 
$new_height = 500; 
$new_width = $width * ($new_height/$height); 

// Resample image 
$image_resized = imagecreatetruecolor($new_width, $new_height); 
imagecopyresampled($image_resized, $tmpName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// New image 
$tmpName= $image_resized; 


$move = move_uploaded_file($tmpName, $folder.$fileName);
if($move) {



$url = "/members/clubs/image.php?pic=$image";        
header("Location: $url");  	
}
}
?>

 

Ive tried changing

 

imagecopyresampled($image_resized, $tmpName, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

 

to $fileName but that didnt work either.

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300505
Share on other sites

ok added that line an all my errors have started coming back now.

 

error_reporting(-1);
ini_set('display_errors', 1);

if(isset($_POST['submit'])){
$fileName = $_FILES['imagefile']['name'];
$tmpName = $_FILES['imagefile']['tmp_name'];
//where to save the image
$folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/";

//get original width and height 
$size = GetImageSize($tmpName); 
$width = $size[0]; 
$height = $size[1]; 

// auto adjust width of image according to predetermined height 
$new_height = 500; 
$new_width = $width * ($new_height/$height); 

// Resample image 
$image_resized = imagecreatetruecolor($new_width, $new_height); 

$image = imagecreatefromjpeg($tmpName);

imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

//// New image 
$tmpName = $image_resized; 

$move = move_uploaded_file($tmpName , $folder.$fileName);
if($move) {

$url = "/members/clubs/image.php?pic=$image";        
header("Location: $url");  
}
}


 

my error list from entering that line.

 

 

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home/sites/image.php on line 255

 

Warning: Division by zero in /home/sites//image.php on line 261

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/sites/image.php on line 264

 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in /home/sites/image.php on line 266

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/sites/image.php on line 268

 

where as before there was only the other error.

 

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300526
Share on other sites

Your latest error(s) indicate that the upload failed (probably because the file was too large or you didn't select a file.) See this link for possible upload errors - http://us3.php.net/manual/en/features.file-upload.errors.php

 

At a minimum, your code needs to check that $_FILES isset (exceeding the post_max_size setting will cause the $_FILES array and the $_POST array to be empty and errors in your form or uploads not enabled on the server will also cause the $_FILES array to be empty) and that $_FILES['imagefile']['error'] is equal to zero before you attempt to use any of the the uploaded file information in your logic.

 

if(isset($_FILES) && $_FILES['imagefile']['error']==UPLOAD_ERR_OK){
    // the upload worked, use the uploaded file information here...
} else {
    // the upload failed due to an error, handle that condition here - output a user message indicating why the upload failed (some of them the user can correct by uploading a smaller file.)
}

 

Also, in the case of an upload form, since the $_POST array can be empty for one of the possible error conditions, you should test if the form has been submitted by using -

 

if($_SERVER['REQUEST_METHOD']=='POST'){

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300814
Share on other sites

ok thanks guys Ive done everything that has been said and addapted my code accordingly.  Thanks for the help.  Unfortunately however the upload is still not working. 

 

I have no errors with error reporting switched on, when I upload an image the page just refreshes  and no image is on the server. 

 

Am I even doing this correctly.  Im trying to save a resized image to the server (for obvious space saving reasons) I will then store its name in the database. Ive googled every possible solution but none seem to do what I need.

 

Here is the code as it stands.  I cant see any errors myself and none output but no file gets uploaded to the server or anything.

 

        <?php
error_reporting(-1);
ini_set('display_errors', 1);

if($_SERVER['REQUEST_METHOD']=='POST'){

if(isset($_FILES) && $_FILES['file']['error']==UPLOAD_ERR_OK){
    // the upload worked, use the uploaded file information here...
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
//where to save the image
$folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/";

//get original width and height 
$size = GetImageSize($tmpName); 
$width = $size[0]; 
$height = $size[1]; 

// auto adjust width of image according to predetermined height 
$new_height = 500; 
$new_width = $width * ($new_height/$height); 

// Resample image 
$image_resized = imagecreatetruecolor($new_width, $new_height); 

$image = imagecreatefromjpeg($tmpName);

imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

//// New image 
$tmpName = $image_resized; 

$move = move_uploaded_file($tmpName , $folder.$fileName);
if($move) {

$url = "/members/clubs/image.php?pic=$image";        
header("Location: $url");  
}

}
else {
   echo "Error";
}	





}
?>

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300846
Share on other sites

when I upload an image the page just refreshes

 

What page just refreshes? We need to know exactly what occurs in front of you in order to help. For all we know there's something you are doing in your form that is causing the problem or that your header() statement in the code you posted is redirecting to the page in question or that you have opened the form directly in your browser through the file system and not through a URL on your web server.

 

Edit: I just noticed that you changed the name of the uploaded file field from 'imagefile' to 'file'. Are you sure that is correct?

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1301178
Share on other sites

I tried your posted code using a valid and matching upload form and you will get an error at the move_uploaded_file statement because you have set the first parameter to be a GD image resource. move_uploaded_file expects the first parameter to be the original tmp uploaded file (kind of why they named it move_uploaded_file.)

 

If your intent is to save the resized image in $image_resized to the $folder.$fileName location, you would need to use an appropriate GD output function -

imagejpeg($image_resized, $folder.$fileName);

 

Link to comment
https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1301252
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.