Jump to content

Image resize


dfowler

Recommended Posts

Hey guys, I've created the following upload script to allow an admin to upload images and associate with items in a database.  The problem is, I would like to be able to resize the images for a thumbnail size, but I'm not sure how to do this.  Eventually on the front-end the page will pull 'featured' items from the database and display them next to the thumbnail associated with them.  Here is what I have so far:

 

<?php
include 'system.php';

   $query2 = "select * from items where active='1' and featured='1'";
   $resource = mysql_query($query2);
   while (($row = mysql_fetch_assoc($resource)) !== false) {
        $items[] = $row;
   }
?>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
Featured Item: <select name="item_name">
<?php foreach($items as $i){
	echo "<option value='".$i['id']."'>".$i['name']."</option>";
} ?>
</select>
<input type="submit" value="Upload File" />
</form>

 

<?php
include 'system.php';

$path = "images/";

$target_path = $path . basename($_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded";
$sql = "INSERT INTO images (name, location, item_id) VALUES ('". $_FILES['uploadedfile']['name'] ."', '". $target_path ."', '". $_POST['item_name'] ."')";
mysql_query($sql) or die (mysql_error());
} else {
echo "There was an error uploading the file (". basename($_FILES['uploadedfile']['name']). "), please try again!";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/110330-image-resize/
Share on other sites

Exactly ;p.

 

What you'll want to do....

 

(Psuedo code....  Too lazy to actually type it all out.  That's what you get to do ;p.)

 

<?php

//pretend $file holds the filepath to the input file...
//pretend $output is the output path

//get the file extension from $file
if(extension is jpg) {
     $in_img = imagecreatefromjpeg($file);
}
else if(extension is png) {
     $in_img = imagecreatefrompng($file);
}
else if(.....)

$out_img = imagecreatetruecolor(new_width, new_height);

imagecopyresampled($out_img, $in_img, <other params>);

//then, what ever file type you want to save it as, you use the corrosponding image<type>() function

imagepng($out_img, $output);

?>

 

Edit: Oh, you'll probably wanna do some math to keep the porportions, although, if you want thumbs of the same size, you might have to distort it a little.

Link to comment
https://forums.phpfreaks.com/topic/110330-image-resize/#findComment-566103
Share on other sites

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.