Link to home
Start Free TrialLog in
Avatar of runnerjp
runnerjp

asked on

image upload and resize

i have a few questions...  below is my current code for resizing my images


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?

3rd;y is there any more enhnace ments any 1 can suggest?
<?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>";  
 
          }
        }
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mahdii7
Mahdii7
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Mahdii7,

Take a look at pathinfo() to extract the extension.



<?php 
 
$path_parts = pathinfo("/www/htdocs/index.html"); 
 
echo $path_parts["dirname"] . "\n"; 
echo $path_parts["basename"] . "\n"; 
echo $path_parts["extension"] . "\n"; 
 
?>

Open in new window

Avatar of runnerjp
runnerjp

ASKER

ermm for some reason with the below code it says Extension: jpg is not allowed! :S:S
      // Adding extension verification
                                $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|";
                                if (!strpos($allowed_ext, "|".$file_ext."|")) die("Extension: $file_ext is not allowed!");  

Open in new window

I bet it's a case issue


if (!strpos($allowed_ext, "|".strtolower($file_ext)."|"))


oh it's NOT a case issue

if (strpos($allowed_ext, "|".strtolower($file_ext)."|")!==false)

---
strpos was returning 0 (first position of the string), that's different than false (not found)
i chnaged it to if (strpos($allowed_ext, "|".strtolower($file_ext)."|")!==false) and i still get Extension: jpg is not allowed!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial