John_S Posted January 20, 2009 Share Posted January 20, 2009 Hello everybody, Recently I decided to add an image upload script to my website and since myself I am not experienced at all with the GD functions yet, I decided to get an already coded script. However it didn't really suit my needs, and I decided to modify it, I ended up being stuck. I tried all I could but I couldn't manage to code these features: Save all images (gif, png, jpg) as jpg images in the upload gallery and give them all a 6 number/letter name (unique ID). I tried to use uniqid("pt").".jpg" and move_uploaded_file($_FILES['image']['tmp_name'], "./uploads/$newname"); but it didn't work. So I was wondering if there somebody that might want to help me to customize it. Here is the script: <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php // upload the file if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) { // file needs to be jpg,gif,bmp,x-png and 4 MB max if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000)) { // some settings $max_upload_width = 2592; $max_upload_height = 1944; // if user chosed properly then scale down the image according to user preferances if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){ $max_upload_width = $_REQUEST['max_width_box']; } if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){ $max_upload_height = $_REQUEST['max_height_box']; } // if uploaded image was JPG/JPEG if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){ $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was GIF if($_FILES["image_upload_box"]["type"] == "image/gif"){ $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]); } // BMP doesn't seem to be supported so remove it form above image type test (reject bmps) // if uploaded image was BMP if($_FILES["image_upload_box"]["type"] == "image/bmp"){ $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was PNG if($_FILES["image_upload_box"]["type"] == "image/x-png"){ $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]); } $remote_file = "uploads/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file,100); chmod($remote_file,0644); // get width and height of original image list($image_width, $image_height) = getimagesize($remote_file); if($image_width>$max_upload_width || $image_height >$max_upload_height){ $proportions = $image_width/$image_height; if($image_width>$image_height){ $new_width = $max_upload_width; $new_height = round($max_upload_width/$proportions); } else{ $new_height = $max_upload_height; $new_width = round($max_upload_height*$proportions); } $new_image = imagecreatetruecolor($new_width , $new_height); $image_source = imagecreatefromjpeg($remote_file); imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); imagejpeg($new_image,$remote_file,100); imagedestroy($new_image); } imagedestroy($image_source); header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error"); exit; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Image Upload with resize</title> <style type="text/css"> <!-- body,td,th { font-family: Arial, Helvetica, sans-serif; color: #333333; font-size: 12px; } .upload_message_success { padding:4px; background-color:#009900; border:1px solid #006600; color:#FFFFFF; margin-top:10px; margin-bottom:10px; } .upload_message_error { padding:4px; background-color:#CE0000; border:1px solid #990000; color:#FFFFFF; margin-top:10px; margin-bottom:10px; } --> </style></head> <body> <h1 style="margin-bottom: 0px">Submit an image</h1> <?php if(isset($_REQUEST['upload_message'])){?> <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>"> <?php echo htmlentities($_REQUEST['upload_message']);?> </div> <?php }?> <form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;"> <label>Image file, maximum 4MB. it can be jpg, gif, png:</label><br /> <input name="image_upload_box" type="file" id="image_upload_box" size="40" /> <input type="submit" name="submit" value="Upload image" /> <br /> <br /> <label>Scale down image? (2592 x 1944 px max):</label> <br /> <input name="max_width_box" type="text" id="max_width_box" value="1024" size="4"> x <input name="max_height_box" type="text" id="max_height_box" value="768" size="4"> px. <br /> <br /> <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;"> <strong>Notes:</strong><br /> The image will not be resized to this exact size; it will be scalled down so that neider width or height is larger than specified.<br /> When uploading this script make sure you have a directory called "image_files" next to it and make that directory writable, permissions 777.<br /> After you uploaded images and made tests on our server please <a href="delete_all_images.php">delete all uploaded images </a> <br /> </p> <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" /> </form> <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?> <p> <img src="uploads/<?php echo $_REQUEST['show_image'];?>" /> </p> <?php }?> </body> </html> Thanks a lot in advance! John Link to comment https://forums.phpfreaks.com/topic/141656-solved-image-upload-script-adding-unique-id/ Share on other sites More sharing options...
tommyda Posted January 21, 2009 Share Posted January 21, 2009 this is the upload image page from an admin panel I just built. <code> <p align="center" class="style1">Administration Panel </p> <table width="750" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F0F8FB"> <tr> <td> <div align="center"> <p><strong><br /> Edit Logo </strong></p> <table width="438" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> <tr> <td><div align="center"> <? //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","250"); //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an errro occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="images/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; }}}} //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "<h1>Logo change successful! </h1></br>"; mysql_query("UPDATE edit SET logo='$newname' WHERE id='1'") or die(mysql_error()); } //$myFile = "logo.php"; //$fh = fopen($myFile, 'w') or die("can't open file"); //$txt = '<img src="'.$newname.'" alt="logo">' //fwrite($fh, $txt); //fclose($fh); echo 'Current Logo:</br> <img src="'; include'logo.php'; echo '" alt="logo"></br>' ?> </div></td> </tr> </table> <br /> </div> <form action="" method="post" enctype="multipart/form-data" name="newad" id="newad"> <table width="438" align="center" bgcolor="#FFFFFF"> <tr> <td width="398"><input name="image" type="file" size="35" /> Max size 250KB</td> </tr> <tr> <td><input name="Submit" type="submit" value="Edit Logo" /></td> </tr> </table> <p align="center"><a href ="admin.php">Click here to return to the administration panel</a></p> </form></td></tr></table> <!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" --></code> Link to comment https://forums.phpfreaks.com/topic/141656-solved-image-upload-script-adding-unique-id/#findComment-741671 Share on other sites More sharing options...
John_S Posted January 21, 2009 Author Share Posted January 21, 2009 Thanks, interesting code! Can I use it? Link to comment https://forums.phpfreaks.com/topic/141656-solved-image-upload-script-adding-unique-id/#findComment-741894 Share on other sites More sharing options...
jkewlo Posted January 21, 2009 Share Posted January 21, 2009 Please put code in the [ code ] tags. as the code looks horrible without it! <p align="center" class="style1">Administration Panel </p> <table width="750" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F0F8FB"> <tr> <td> <div align="center"> <p><strong><br /> Edit Logo </strong></p> <table width="438" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> <tr> <td><div align="center"> <? //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","250"); //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an errro occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="images/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; }}}} //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "<h1>Logo change successful! </h1></br>"; mysql_query("UPDATE edit SET logo='$newname' WHERE id='1'") or die(mysql_error()); } //$myFile = "logo.php"; //$fh = fopen($myFile, 'w') or die("can't open file"); //$txt = '<img src="'.$newname.'" alt="logo">' //fwrite($fh, $txt); //fclose($fh); echo 'Current Logo:</br> <img src="'; include'logo.php'; echo '" alt="logo"></br>' ?> </div></td> </tr> </table> <br /> </div> <form action="" method="post" enctype="multipart/form-data" name="newad" id="newad"> <table width="438" align="center" bgcolor="#FFFFFF"> <tr> <td width="398"><input name="image" type="file" size="35" /> Max size 250KB</td> </tr> <tr> <td><input name="Submit" type="submit" value="Edit Logo" /></td> </tr> </table> <p align="center"><a href ="admin.php">Click here to return to the administration panel</a></p> </form></td></tr></table> <!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" --> Link to comment https://forums.phpfreaks.com/topic/141656-solved-image-upload-script-adding-unique-id/#findComment-741900 Share on other sites More sharing options...
tommyda Posted January 23, 2009 Share Posted January 23, 2009 Yes you may use it ! Link to comment https://forums.phpfreaks.com/topic/141656-solved-image-upload-script-adding-unique-id/#findComment-744402 Share on other sites More sharing options...
blackcode Posted May 11, 2009 Share Posted May 11, 2009 but, where is the validation of the copy section? look... $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="images/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if you see the code, the image is copied even if $errors = 1 ... i mean image is uploaded however. i think it should be in the following part: if(isset($_POST['Submit']) && !$errors) { [b] $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="images/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname);[/b] echo "<h1>Logo change successful! </h1></br>"; mysql_query("UPDATE edit SET logo='$newname' WHERE id='1'") or die(mysql_error()); } In fact there is a one more error: if input file is empty, the process run how it isnt empty (Sorry, my english sucks xD) I rewrite that code without errors: <?php /*************************************************************/ /* Cambia el nombre del dir "images" por el nombre del dir */ /* donde se guardara lo que se suba. Es todo lo que se edita */ /*************************************************************/ $directorio="images"; ///////////////////////////////////////////////////////////////////////////// //////// NO EDITES NADA DE ABAJO ////////////////// ///////////////////////////////////////////////////////////////////////////// echo ' <head> <style> * { margin: 0; padding: 0; text-decoration: none; font-size: 1em; outline: none;} code, kbd, samp, pre, tt, var, textarea, input, select, isindex, listing, xmp, plaintext { font: inherit; font-size: 1em; white-space: normal; } dfn, i, cite, var, address, em { font-style: normal; } th, b, strong,h2, h3, h4, h5, h6 { font-weight: bold } a, img, a img, iframe, form, fieldset, abbr, acronym, object, applet, table { border: none; } table { border-collapse: collapse; border-spacing: 0; } caption, th, td, { text-align: left; vertical-align: top; } q { quotes: "" ""; } ul, ol, dir, menu { list-style: none; } sub, sup { vertical-align: baseline; } a { color: inherit; } hr { display: none; } font { color: inherit !important; font: inherit !important; color: inherit !important; } marquee { overflow: inherit !important; -moz-binding: none; } blink { text-decoration: none; } nobr { white-space: normal; } </style> </head> <body style="text-align:center;padding-left:100px;background:#EFEFEF"> '; define ("MAX_SIZE","100"); function sacaExtension($str) { $datos=explode(".",$str); $ult=count($datos)-1; $extension=$datos[$ult]; return $extension; } $error=0; if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if ($image) { $extension = sacaExtension($image); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1 style="font-family:Calibri;font-size:40px">¡¡¡EXTENSION NO PERMITIDA!!!</h1>'; echo '<h2 style="color:red;font-family:Calibri;font-size:35px">CUIDADO CON LO QUE INTENTAS HACER</h2>'; die() ; $error=1; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { echo '<h3 style="font-family:Calibri;font-size:40px">¡ERROR! La Imagen Es Demasiado Grande</h3>'; $error=1; } } } else { echo '<h3 style="font-family:Calibri;font-size:40px" ><b>¡ERROR!</b> No Hay Nada Por Hacer</h3>'; $error=1; } if(!$error && $image) { $dirx=$directorio."/".$image; copy($_FILES['image']['tmp_name'], $dirx); echo '<h3 style="font-family:Calibri;font-size:40px">¡EXITO! El Archivo Ha Subido Satisfactoriamente </h3>'; } } echo '<center> <form name="newad" method="post" enctype="multipart/form-data" action=""> <table> <tr><td><h2 style="font-family:Calibri">Selecciona Una Imagen</h2></td></tr> <tr><td><input type="file" name="image"></td></tr> <tr><td><input name="Submit" type="submit" value="Subir"></td></tr> </table> </form></center> </body>'; ?> Link to comment https://forums.phpfreaks.com/topic/141656-solved-image-upload-script-adding-unique-id/#findComment-831950 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.