Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Simple Particle Engine for a shooter game
Snake / Nibbles clone in C and Ncurses
Please improve
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Popular
Parsing of XML data has high CPU usage
Snake / Nibbles clone in C and Ncurses
Please improve
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Active Record getting unique records
Simple Particle Engine for a shooter game
Breadth first cartesian product iterator
php refactoring
first BST
Pastable version of
image upload script
<pre class='prettyprint' language='php'><?php // check to see if image has been uploaded if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) { die ('<strong>Invalid image uploaded. Please go back and try again.</strong>'); } else { $uploaded_image = $_FILES['image']['tmp_name']; } //function to view any type of image function open_image ($file) { // Get extension $extension = strrchr($file, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': $im = @imagecreatefromjpeg($file); $image_type = ".jpg"; break; case '.jpeg': $im = @imagecreatefromjpeg($file); $image_type = ".jpeg"; break; case '.gif': $im = @imagecreatefromgif($file); $image_type = ".gif"; break; case '.png': $im = @imagecreatefromgif($file); $image_type = ".png"; break; default: $im = false; break; } return $im; } // Load image $image = open_image($uploaded_image); if ($image === false) { die ('that file type is not allowed'); } // die if theres no image // get original image width and height $width = imagesx($image); $height = imagesy($image); // resized image width $resize_width = 1020; $resize_height = $height * ($resize_width/$width); // thumbnail image width $thumb_width = 150; $thumb_height = $height * ($thumb_width/$width); // Resample resized image $image_resized = imagecreatetruecolor($resize_width, $resize_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height); // Resample thumbnail image $image_resized = imagecreatetruecolor($thumbnail_width, $thumbnail_height); imagecopyresampled($image_thumbnail, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); ///////////////////////////////// check image name and rename it /////////////////////////////// $uploaded_img_name = $_FILES['image']['name']; $result_check = mysql_query("SELECT photo_file_name FROM album WHERE photo_file_name='$uploaded_img_name'") or die(mysql_error()); while($check_data = mysql_fetch_array( $result_check )) { $check_name= $check_data['user_id']; } if (empty($check_name)) { $uploaded_img_name = $uploaded_img_name; } else { $uploaded_img_name = str_replace($image_type, "", $uploaded_img_name); $uploaded_img_name = $uploaded_img_name ."1". $image_type; } /////////////////////////////////////// insert to directory /////////////////////////////// $filedir = '/upload/user_album/'; // the directory for the original image $thumbdir = '/upload/user_album/thumb/'; // the directory for the thumbnail image $prod_img = $filedir.$uploaded_img_name; $prod_img_thumb = $thumbdir.$uploaded_img_name; if( !empty($HTTP_POST_FILES['song_image']['tmp_name']) ) { $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var'; if ( @$ini_val('open_basedir') != '' ) { if ( @phpversion() < '4.0.3' ) { message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file<br /><br />Please contact your server admin', '', __LINE__, __FILE__); } $move_file = 'move_uploaded_file'; } else { $move_file = 'copy'; } } // moved resized image into directory $move_file($uploaded_image, $prod_img); @chmod($prod_img, 0777); // moved thumb image into directory $move_file($uploaded_image, $prod_img_thumb); @chmod($prod_img_thumb, 0777); } /////////////////////////////////////// insert to database/////////////////////////////// mysql_query("INSERT INTO album ('photo_file_name') VALUES('$uploaded_img_name') ") or die(mysql_error());</pre> <a href="http://www.refactormycode.com/codes/109-image-upload-script" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>