Jump to content

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

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.