Jump to content

Images


onthespot

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.