Image Upload and Re-size PHP

Sometimes you need to upload and resize image files using PHP, and here you will find the simplest PHP snippet that takes care of your uploaded image files. This snippet should work as it is, but there are several other methods you can combine with it to create a proper functional image uploader, for example you can take a look at Ajax and PHP based image uploader here.

Fixing Image Orientation using its Exif data

Sometimes photos are wrongly taken, eg: upside down, sideways etc, and most newbie users don’t know how to rotate image, so they might just upload image “as it is”. This might cause the problem later, unless you have some kind of image rotate program to set it right.

Grab Picture From Remote URL

It’s a really easy function to that copies remote picture into local folder. You can just use PHP copy() directly, but this function does little more than that, enjoy. function grab_remote_pic($new_file_name, $local_dir_path, $remote_picture_url) { if(!is_dir($local_dir_path)){ //create new dir if doesn't exist mkdir($local_dir_path); } $local_file_path = $local_dir_path .'/'.$new_file_name; if(copy($remote_picture_url, $local_file_path)) { return true; } } Usage : //grab_remote_pic(NEW FILE NAME, LOCAL SAVE PATH, REMOTE IMAGE URL) grab_remote_pic('new_file_name.gif', 'home/path/to/local/images/', 'http://graph.facebook.com/1442161041/picture?width=120&height=120');

Redirect users to a new Page

There are various methods we can use to redirect users to different URL. Let’s have a look at codes below with just one goal. HTML Meta Refresh Tag Most common way to redirect users to another location is using HTML refresh meta tag, just have this code placed within the <head></head> section of your HTML page : Javascript Location And here is another method using Javascript window.location, only downside is if browser doesn’t support Javascript, this code will fail to redirect user to new location: //Will take you to sanwebe.com window.location.href = 'http://www.sanwebe.com'; PHP Header We can use PHP header to redirect user to new URL easily, but remember there must be NO other output before this code, such as echo code which will trigger “headers already sent” error. Perl Location Here’s perl code to redirect user to sanwebe.com. #!/usr/bin/perl $url = "https://www.sanwebe.com/"; print "Location: $url\n\n";