Jump to content

Question about upload


slpctrl

Recommended Posts

Alright, I have an upload script, and I haev another script to resize an image to specific dimensions, but how do I combine the two to check the uploaded image before upload, resize as needed, and then save? Here are my 2 scripts:

 

Upload:

<?php
echo('Upload images:
<FORM ENCTYPE="multipart/form-data" ACTION="" METHOD="POST">
The file: <INPUT TYPE="file" NAME="image"><br>
<INPUT TYPE="submit" VALUE="Upload">
</FORM>');
//start upload function
Function uploadimg($image = NULL)
{
   $dir = "images/";
   $size = 1572864;
   if (is_null($image))
   $image = $_FILES['image'];
   $imagetype = $_FILES['image']['type'];
   $imagesize = $_FILES['image']['size'];
   if (!isset($_FILES['image'])) exit;
   if (is_uploaded_file($_FILES['image']['tmp_name'])) {
   if ($imagesize>$size) { echo "The file is too big<br>"; exit; }
   if (($imagetype=="image/gif") || ($imagetype=="image/pjpeg") || ($imagetype=="image/jpeg") 
   || ($imagetype=="image/png") || ($imagetype=="image/bmp")) {
   if (file_exists($dir . $image)) { echo "The file already exists<br>"; exit; }
   $res = copy($_FILES['image']['tmp_name'], $dir . $_FILES['image']['name']);
   if (!$res) { echo "upload failed!<br>n"; exit; } else { echo "upload sucessful<br>"; }
   echo "<b>File Name:</b> ".$_FILES['image']['name']."<br>";
   echo "<b>File Size:</b> ".$_FILES['image']['size']." bytes<br>";
   echo "<b>File Type:</b> ".$_FILES['image']['type']."<br>"; }
else {
   echo "Wrong file type<br>"; exit; }}
   $my_file = $_FILES['image']['name'];}
//end upload function

$image = $_FILES['image'];
if(isset($image) && $_POST['group1'] == "Original")
uploadimg($image);
else
die();
?> 

 

And then the size script:

 

<?php
function size($image)
{
  header("Content-type: image/jpeg");
  $x = @getimagesize($image);
  $sw = $x[0];
  $sh = $x[1];
if($sw > 640 || $sh > 480)
  {
  $w = 640;
  $h = ($w/$x[0]) * $x[1];
  $new = @ImageCreateFromJPEG($image) or
  $new = @ImageCreateFromPNG($image) or
  $new = @ImageCreateFromGIF($image) or
  $new = false;
if(!$new)
  {
    readfile($new);
  }
else
  {
  $thumb = @ImageCreateTrueColor($w, $h);
  @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh);
  return $thumb;
  }
  }
else
   exit;
}
?>

 

Any help would be greatly appreciated, thanks.

Link to comment
https://forums.phpfreaks.com/topic/104640-question-about-upload/
Share on other sites

<?php
echo('Upload images:
<FORM ENCTYPE="multipart/form-data" ACTION="" METHOD="POST">
The file: <INPUT TYPE="file" NAME="image"><br>
<INPUT TYPE="submit" VALUE="Upload">
</FORM>');
//start upload function
Function uploadimg($image = NULL)
{
   $dir = "images/";
   $size = 1572864;
   if (is_null($image))
   $image = $_FILES['image'];
   $imagetype = $_FILES['image']['type'];
   $imagesize = $_FILES['image']['size'];
   if (!isset($_FILES['image'])) exit;
   if (is_uploaded_file($_FILES['image']['tmp_name'])) {
   if ($imagesize>$size) { echo "The file is too big<br>"; exit; }
   if (($imagetype=="image/gif") || ($imagetype=="image/pjpeg") || ($imagetype=="image/jpeg") 
   || ($imagetype=="image/png") || ($imagetype=="image/bmp")) {
   if (file_exists($dir . $image)) { echo "The file already exists<br>"; exit; }
   $res = copy($_FILES['image']['tmp_name'], $dir . $_FILES['image']['name']);
   if (!$res) { 
     echo "upload failed!<br>n"; exit; 
   } else { 
     $image = $dir . $_FILES['image']['name'];
     echo "upload sucessful<br>"; 
     size($image);
   }
   echo "<b>File Name:</b> ".$_FILES['image']['name']."<br>";
   echo "<b>File Size:</b> ".$_FILES['image']['size']." bytes<br>";
   echo "<b>File Type:</b> ".$_FILES['image']['type']."<br>"; }
else {
   echo "Wrong file type<br>"; exit; }}
   $my_file = $_FILES['image']['name'];}
//end upload function

$image = $_FILES['image'];
if(isset($image) && $_POST['group1'] == "Original")
uploadimg($image);
else
die();
?> 

 

 

 

<?php
function size($image)
{
  header("Content-type: image/jpeg");
  $x = @getimagesize($image);
  $sw = $x[0];
  $sh = $x[1];
if($sw > 640 || $sh > 480)
  {
  $w = 640;
  $h = ($w/$x[0]) * $x[1];
  $new = @ImageCreateFromJPEG($image) or
  $new = @ImageCreateFromPNG($image) or
  $new = @ImageCreateFromGIF($image) or
  $new = false;
if(!$new)
  {
    readfile($new);
  }
else
  {
  $thumb = @ImageCreateTrueColor($w, $h);
  @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh);
  return $thumb;
  }
  }
else
   exit;
}
?>

 

 

 

All I did was incorperate the size function if the image copy was successful.

Bleh, yeah I know and it doesn't work. I'm pretty sure I know why, I need the function to open the image, check the image to see if it's past it's maximum dimensions I would like to allow, and if it is set the width to say 640 and scale the other dimension based on that, and then resave the file. But I'm unsure as to how to go about this. Can anyone tell me the main functions I'll need to achieve this? Or what I should research? Thanks.

Archived

This topic is now archived and is closed to further replies.

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