Jump to content

Tired of messing around


sumfight

Recommended Posts

I have been trying this since noon today, I wasted my whole day off of school and work

 

Can someone just tell me what the best image resizer would be that would save the original file in one folder, the resized picture in another folder, so then I can put a random image generator tag on the page and call the new pictures form the new folder?

Link to comment
https://forums.phpfreaks.com/topic/109069-tired-of-messing-around/
Share on other sites

<form name="uploadform" enctype="multipart/form-data" method="post" action=<?php $_SERVER[php_SELF] ?>>
Choose a file to upload:<input type="file" name="file01"/><br>
<input type="submit" value="submit" name="action"/>
</form>

<?php
if(isset($_FILES['file01'])) {

$fil = $_FILES['file01']['name'];

if($_POST["action"] == "submit"){

if(file_exists($_FILES['file01']['tmp_name'])) {

$upload = $_FILES['file01']['tmp_name'];
$filepath = $_SERVER['DOCUMENT_ROOT'] ."/images/" . $_FILES['file01']['name'];


try {

$err = move_uploaded_file($upload, $filepath);
}
catch(EXCEPTION	$e) {

echo $e;
}
}

}
}
else {
echo "upload failed";
}

}
$imgpath = "images/your_image_original.jpg";
$thumbpath = "thumbs/your_image_small.jpg";
$img = imagecreatefromjpeg($imgpath);

$percent = 0.20;
list($imgwidth,$imgheight) = getimagesize($imgpath);
$newimgwidth = $imgwidth * $percent;
$newimgheight = $imgheight * $percent;
$newimage = imagecreatetruecolor($newimgwidth, $newimgheight);

imagecopyresampled($newimage, $img,0,0,0,0,$newimgwidth, $newimgheight,
$imgwidth, $imgheight);

imagejpeg($newimage, $thumbpath,100);
?>

 

mod edit - code tags added

<form name="uploadform" enctype="multipart/form-data" method="post" action=<?php $_SERVER[php_SELF] ?>>
Choose a file to upload:<input type="file" name="file01"/><br>
<input type="submit" value="submit" name="action"/>
</form>

<?php
if(isset($_FILES['file01'])) {

$fil = $_FILES['file01']['name'];

if($_POST["action"] == "submit"){

if(file_exists($_FILES['file01']['tmp_name'])) {

$upload = $_FILES['file01']['tmp_name'];
$filepath = $_SERVER['DOCUMENT_ROOT'] ."/images/" . $_FILES['file01']['name'];


try {

$err = move_uploaded_file($upload, $filepath);
}
catch(EXCEPTION	$e) {

echo $e;
}
}

}
}
else {
echo "upload failed";
}

}
$imgpath = "images/your_image_original.jpg";
$thumbpath = "thumbs/your_image_small.jpg";
$img = imagecreatefromjpeg($imgpath);

$percent = 0.20;
list($imgwidth,$imgheight) = getimagesize($imgpath);
$newimgwidth = $imgwidth * $percent;
$newimgheight = $imgheight * $percent;
$newimage = imagecreatetruecolor($newimgwidth, $newimgheight);

imagecopyresampled($newimage, $img,0,0,0,0,$newimgwidth, $newimgheight,
$imgwidth, $imgheight);

imagejpeg($newimage, $thumbpath,100);
?>

 

mod edit - code tags added

 

Thanks for the help, I have this code which is great for a resize now, but I was wondering if you could tell me what to put into it to make the file names change, because if i have 5.jpg uploaded, and someone uploads another 5.jpg, the old one gets replaced

 

<form action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">

        <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />

        <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>

</form>

<?php

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

          if (isset ($_FILES['new_image'])){

              $imagename = $_FILES['new_image']['name'];

              $source = $_FILES['new_image']['tmp_name'];

              $target = "Lost/".$imagename;

              move_uploaded_file($source, $target);

 

              $imagepath = $imagename;

              $save = "lost/" . $imagepath; //This is the new file you saving

              $file = "Lost/" . $imagepath; //This is the original file

 

              list($width, $height) = getimagesize($file) ;

 

              $modwidth = 390;

 

              $diff = $width / $modwidth;

 

              $modheight = $height / $diff;

              $tn = imagecreatetruecolor($modwidth, $modheight) ;

              $image = imagecreatefromjpeg($file) ;

              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

 

              imagejpeg($tn, $save, 100) ;

 

             

            echo "<img src='Lost/".$imagepath."'><br>";

            echo "You are wonderful for the upload, thanks :D";

 

          }

        }

?>

 

Also how to add it to a database too(but this one is not necessary)

change

$imagename = $_FILES['new_image']['name'];

 

to whatever you want, you can even generate a random name using rand()

 

as for the db part

 

just add an INSERT query before

echo "<img src='Lost/".$imagepath."'>

 

might want to use sumfight's exception handling to see whether or not the image has been uploaded and resized before inserting in your db

change

$imagename = $_FILES['new_image']['name'];

 

to whatever you want, you can even generate a random name using rand()

 

as for the db part

 

just add an INSERT query before

echo "<img src='Lost/".$imagepath."'>

 

might want to use sumfight's exception handling to see whether or not the image has been uploaded and resized before inserting in your db

 

I change it to this

 

  $imagename = $_FILES[rand()];

 

but it didn't work

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.