runnerjp Posted April 10, 2009 Share Posted April 10, 2009 i have a few questions... below is my current code for resizing my images <?php error_reporting(E_ALL); session_start(); require_once '../settings.php'; include "../info.php"; // sets username/id ect ?><form action="<?php echo $_server['php-self']; ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm"> <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" /> <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button> </form> <?php if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $file_name = $_FILES['new_image']['name']; $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; $imagename = "$id.$file_ext"; $source = $_FILES['new_image']['tmp_name']; $target = "images/$id.$file_ext"; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "images/thumbs/" . $imagepath; //This is the new file you saving $file = "images/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $imagepath = $imagename; $save = "images/mini/" . $imagepath; //This is the new file you saving $file = "images/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 80; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $imagepath = $imagename; $save = "images/tiny/" . $imagepath; //This is the new file you saving $file = "images/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 40; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $sql = "UPDATE `users` SET image = '".$id.".".$file_ext."' WHERE ID=$id"; mysql_query($sql) or die(mysql_error()); echo "Large image: <img src='images/thumbs/".$imagepath."'><br>"; echo "MINI: <img src='images/mini/".$imagepath."'>"; echo "TINY: <img src='images/tiny/".$imagepath."'><br>"; } } ?> my first question would be what would be the best size to use for a profile picture/avator ?? at the moment is have $modwidth = 200; but if i have a long picture then it looks too big..if you upload a picure nearly in all perportions then it looks great... is there anyway to make it more perpotionate or anything? my second question would be how could i stop anything but images beeing uploaded? Link to comment https://forums.phpfreaks.com/topic/153564-image-upload-and-resize/ Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 you could set a width to height ratio the pic would have to conform to before allowing the upload. maybe no greater than 1:2 but you'd have to test it. to only allow images check the $_FILES['image']['type'] data for 'image/gif', 'image/jpeg' and 'image/pjpeg'. other file types: http://en.wikipedia.org/wiki/Internet_media_type Link to comment https://forums.phpfreaks.com/topic/153564-image-upload-and-resize/#findComment-806956 Share on other sites More sharing options...
runnerjp Posted April 11, 2009 Author Share Posted April 11, 2009 ok iv tried this <?php error_reporting(E_ALL); session_start(); require_once '../settings.php'; include "../info.php"; // sets username/id ect ?><form action="<?php echo $_server['php-self']; ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm"> <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" /> <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button> </form> <?php if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $file_name = $_FILES['new_image']['name']; $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; // Adding extension verification $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|"; if (!strpos($allowed_ext, "|".$file_ext."|")) die("Extension: $file_ext is not allowed!"); // End extension verification $imagename = "$id.$file_ext"; $source = $_FILES['new_image']['tmp_name']; $target = "images/$id.$file_ext"; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "images/thumbs/" . $imagepath; //This is the new file you saving $file = "images/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; // Adding proportionate image resizing, with max values /* $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; */ $max_w = 200; $max_h = 200; if($width <= $max_w && $height <= $max_h){ // if it fits $modheight = $height; $modwidth = $width; }else{ // Then resize $diff = ($width > $height) ? ($width/$max_w) : ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800) $modheight = $height / $diff; $modwidth = $width / $diff; } // End $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $imagepath = $imagename; $save = "images/mini/" . $imagepath; //This is the new file you saving $file = "images/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 80; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $imagepath = $imagename; $save = "images/tiny/" . $imagepath; //This is the new file you saving $file = "images/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 40; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $sql = "UPDATE `users` SET image = '".$id.".".$file_ext."' WHERE ID=$id"; mysql_query($sql) or die(mysql_error()); echo "Large image: <img src='images/thumbs/".$imagepath."'><br>"; echo "MINI: <img src='images/mini/".$imagepath."'>"; echo "TINY: <img src='images/tiny/".$imagepath."'><br>"; } } ?> Extension: jpg is not allowed! --- this is what i get though :S strange how is this wrong?>?? $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|"; if (!strpos($allowed_ext, "|".$file_ext."|")) die("Extension: $file_ext is not allowed!"); Link to comment https://forums.phpfreaks.com/topic/153564-image-upload-and-resize/#findComment-807163 Share on other sites More sharing options...
runnerjp Posted April 11, 2009 Author Share Posted April 11, 2009 BUMP Link to comment https://forums.phpfreaks.com/topic/153564-image-upload-and-resize/#findComment-807221 Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 you should really use the mime type data in $_FILES['image']['type'] as extensions can be spoofed very easily. Link to comment https://forums.phpfreaks.com/topic/153564-image-upload-and-resize/#findComment-807356 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.