onthespot Posted August 5, 2009 Share Posted August 5, 2009 What I am trying to achieve: Images being uploaded by user The image must be jpeg Where the image goes, must be a certain height and width If a user uploads their image, it must overwrite the previous one I'm not really sure how to go about doing this. Could anyone suggest anything? Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/ Share on other sites More sharing options...
MatthewJ Posted August 5, 2009 Share Posted August 5, 2009 http://www.webdeveloper.com/forum/showthread.php?t=101466 First link on a Google search for "php image upload script" Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891352 Share on other sites More sharing options...
onthespot Posted August 5, 2009 Author Share Posted August 5, 2009 Ive looked into some of these, but they arent very useful. I was hoping for more of direction to more detailed help pages on each point. I shal take a look though Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891357 Share on other sites More sharing options...
alexdemers Posted August 5, 2009 Share Posted August 5, 2009 I can't provide you the code to do all of that but here are some of the basics: This is a good read to handle file uploads: PHP: POST method uploads - Manual While handling your file, you can check it it's a JPEG with: <?php $file = explode('.', $_FILES['user_picture']); $ext = end($file); if ($ext === 'jpg' || $ext === 'jpeg') { echo 'Your file is a JPEG format'; } else { // Do what you wanna do here } ?> Getting the image size once it's in a folder/uploaded: PHP: getimagesize - Manual And for checking the file if it exists, I would recommend to check it before invoking the move_uploaded_file() function when handling file uploads. Use PHP: file_exists - Manual to check if your file already exists, if it does, delete the old one (PHP: unlink - Manual) and then invoke move_uploaded_file() to that directory. A resumé: [*]Create the HTML for file upload [*]Retrieve files in PHP [*]Error handling (not covered in my post) [*]Check if it's a JPEG [*]Check if the file exists; if not, move_uploaded_file; if it exists, delete old one, then move_uploaded_file [*]Get the image size Hope that clarifies your mind. Of course, I HIGHLY recommend error handling and other things to consider like thumbnails creation and others. Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891360 Share on other sites More sharing options...
MatthewJ Posted August 5, 2009 Share Posted August 5, 2009 The link I posted is a step by step from upload to storage to display... it sounds like you want someone to write it to your specs. Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891374 Share on other sites More sharing options...
Adam Posted August 5, 2009 Share Posted August 5, 2009 You should be able to follow tutorials and pick up how to complete most of the steps. Also the move_uploaded_file function will overwrite the destination file if it already exists, which should solve your last point if you always name the image the same thing, perhaps md5 hash of their username or something? Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891379 Share on other sites More sharing options...
onthespot Posted August 5, 2009 Author Share Posted August 5, 2009 Thats awesome, was after links like that. Wasn't sure the php names of what i was after. Really grateful, and I should be able to make something from this. Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891386 Share on other sites More sharing options...
onthespot Posted August 5, 2009 Author Share Posted August 5, 2009 Oh one final point, is there a way I can let a user upload any jpeg but it will be resized? Or is it best to just state the height and width the image should be in the html, and will just fit it in somehow? Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891392 Share on other sites More sharing options...
alexdemers Posted August 5, 2009 Share Posted August 5, 2009 Oh one final point, is there a way I can let a user upload any jpeg but it will be resized? Or is it best to just state the height and width the image should be in the html, and will just fit it in somehow? Having the full size image resize to a much smaller size is HIGHLY not recommended. This really adds up to the page loading. In that case, you should create thumbnails of images uploaded and store them in a separate folder (eg. /thumbs/). Creating thumbnails is a bit complex (not too much) because it's different function calls depending on the type of images. Luckily in your case it's only JPEG. So, take a look at: How to Create Thumbnail Images using PHP which covers what I'm too lazy to type in this post. Also note, in that link the author's script is to convert every image in one folder at once which is not what you want, but it's the same function calls. Only the logic changes. Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891400 Share on other sites More sharing options...
Adam Posted August 5, 2009 Share Posted August 5, 2009 which should solve your second to last point * .. I meant. Yeah as alexdemers said. Not only that though, images re-sized by the browser look awful anyway. Definitely worth investing a bit of time into learning how to re-size them with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891404 Share on other sites More sharing options...
onthespot Posted August 5, 2009 Author Share Posted August 5, 2009 Yeah they looked absolutely shocking, I just have never done anything with images before, but I'm at the point now where I need to, the whole website is just looking a bit bland and needs some life put into it. Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/168944-images/#findComment-891409 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.