Jump to content

image upload and resize


runnerjp

Recommended Posts

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
Share on other sites

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
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.