Solved

image upload and resize

Posted on 2009-04-10
7
279 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:runnerjp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
Mahdii7 earned 250 total points
ID: 24120295
Answers to 1 & 2
<?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>";  
 
          }
        }
?>

Open in new window

0
 
LVL 19

Expert Comment

by:Michael701
ID: 24120899
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

0
 

Author Comment

by:runnerjp
ID: 24121423
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Michael701
ID: 24122048
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)
0
 

Author Comment

by:runnerjp
ID: 24125723
i chnaged it to if (strpos($allowed_ext, "|".strtolower($file_ext)."|")!==false) and i still get Extension: jpg is not allowed!
0
 
LVL 19

Assisted Solution

by:Michael701
Michael701 earned 250 total points
ID: 24126166
this is one of those LOL moments

normally I think

if (good extension)
{
  do something
}

but you want your program to die

so let's reverse the logic
$allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|";
if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) 
  die("Extension: $file_ext is not allowed!");  

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
$_GET call between URL 3 41
Session time out quickly. 6 26
PHP Web Development 6 22
PHP Curl Problem 10 36
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question