dfowler Posted June 15, 2008 Share Posted June 15, 2008 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 More sharing options...
corbin Posted June 15, 2008 Share Posted June 15, 2008 Read the following links... If you have any questions, I'll be happy to answer them ;p. http://php.net/gd http://php.net/imagecreatefromjpeg http://php.net/imagecreatefrompng http://php.net/imagecreatetruecolor http://php.net/imagecopyresampled http://php.net/imagesx http://php.net/imagesy Link to comment https://forums.phpfreaks.com/topic/110330-image-resize/#findComment-566082 Share on other sites More sharing options...
dfowler Posted June 15, 2008 Author Share Posted June 15, 2008 It looks like I will have to throw a couple of if statements to check and see what type of image file they upload, then depending on what it is use one of these functions. Am I correct? Link to comment https://forums.phpfreaks.com/topic/110330-image-resize/#findComment-566093 Share on other sites More sharing options...
corbin Posted June 15, 2008 Share Posted June 15, 2008 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 More sharing options...
dfowler Posted June 16, 2008 Author Share Posted June 16, 2008 Will I have to upload the original image first? Or can I use this to only place the thumbnail on the server? Link to comment https://forums.phpfreaks.com/topic/110330-image-resize/#findComment-566453 Share on other sites More sharing options...
craygo Posted June 16, 2008 Share Posted June 16, 2008 read up on this http://www.phpfreaks.com/forums/index.php/topic,191541.0.html No you won't have to save the original image. You can use the second parameter in imagejpeg, imagepng or the others to save the generated image. Ray Link to comment https://forums.phpfreaks.com/topic/110330-image-resize/#findComment-566466 Share on other sites More sharing options...
dfowler Posted June 16, 2008 Author Share Posted June 16, 2008 Looks like back to the drawing board, my upload script isn't even working. Can anybody see what is wrong with it? It is an older script that I've used before, but on a different server. Are there any settings that will affect this? Link to comment https://forums.phpfreaks.com/topic/110330-image-resize/#findComment-566504 Share on other sites More sharing options...
dfowler Posted June 16, 2008 Author Share Posted June 16, 2008 Ok, I fixed the uploading issue, just had to modify some of the server settings. Now back to trying to get the resize to work. Link to comment https://forums.phpfreaks.com/topic/110330-image-resize/#findComment-566568 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.