Jump to content

reduce image size/resolution on upload


robcrozier

Recommended Posts

Hi, does anyone know if its possible to reduce the resolution of an image as it is being uploaded by php. For example this website: www.netmechanic.com allows you to view all of the images on a specified website and also shows you what they would look like in a slightly lower resolution. How do they do it?

 

Basically i want to make sure that images being uploaded to my website are not too large and i want to avoid having to tell people to reduce the image size before uploading.

 

Can anyone help???

 

Cheers!

Link to comment
Share on other sites

I use this for multiple file uploads, But you can change the code for single if you like. Also this will put a full size picture and a thumbnail. I have commented out the thumbnail part.

<?php
$absolute_path = "/path/to/image/folder"; //Absolute path to where files are uploaded
$thumb_path = "/path/to/thumbnail/folder";  //Absolute path to where thumbs are to be stored if you want this
$size_limit = "yes"; //do you want a size limit yes or no.
$limit_size = "600000"; //How big do you want size limit to be in bytes
$limit_ext = "yes"; //do you want to limit the extensions of files uploaded
$ext_count = "3"; //total number of extensions in array below
$extensions = array(".jpg", ".png", ".gif"); //List extensions you want files uploaded to be

function resampimage($maxwidth, $maxheight, $sourcefile, $imgcomp=0)
   {
   $g_imgcomp=100-$imgcomp;
   if(file_exists($sourcefile))
       {
       $g_is=getimagesize($sourcefile);
       $w_adjust = ($maxwidth / $g_is[0]);
       $h_adjust = ($maxheight / $g_is[1]);
       if($w_adjust <= $h_adjust)
           {
           $new_width=($g_is[0]*$w_adjust);
           $new_height=($g_is[1]*$w_adjust);
           }
           else
           {
           $new_width=($g_is[0]*$h_adjust);
           $new_height=($g_is[1]*$h_adjust);
           }
       	//SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
    $image_type = strrchr($sourcefile, ".");

    //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
	switch($image_type) {
		case '.jpg':
			$img_src = imagecreatefromjpeg($sourcefile);
			break;
		case '.png':
			$img_src = imagecreatefrompng($sourcefile);
			break;
		case '.gif':
			$img_src = imagecreatefromgif($sourcefile);
			break;
		default:
			echo("Error Invalid Image Type");
			die;
			break;
		}
       $img_dst=imagecreatetruecolor($new_width,$new_height);
       imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
       imagejpeg($img_dst);
       imagedestroy($img_dst);
       imagedestroy($img_src);
       return true;
       }
       else
       return false;
   }

if(!isset($_POST['submit'])){
$extens = '';

        if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) {
           $extens = "any extension";
        } else {
        $ext_count2 = $ext_count+1;
        for($counter=0; $counter<$ext_count; $counter++) {
            $extens .= "  $extensions[$counter]";
        }
        }
        if (($limit_size == "") or ($size_limit != "yes")) {
            $limit_size = "any size";
        } else {
            $limit_size .= " bytes";
            $mb_size = ($limit_size/1000000);
        }
        $pichead = "<li><font size=\"2\" color=660000>File extension must be $extens<b>";
        $pichead .="</b></font>
        <li><font size=\"2\" color=660000>Maximum file size is $limit_size ($mb_size MB)</font></li>
        <li><font size=\"2\" color=660000>No spaces in the filename</font></li>";
?>
<html>
<head>
<title>HTML Form for uploading image to server</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<html>
<title>Add Vehicle Form</title>
<body>
<p><? echo $pichead; ?></p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Pictures:<br />
1 <input type="file" name="pictures[]" /><br />
2 <input type="file" name="pictures[]" /><br />
3 <input type="file" name="pictures[]" /><br />
4 <input type="file" name="pictures[]" /><br />
5 <input type="file" name="pictures[]" /><br />
6 <input type="file" name="pictures[]" /><br />
<input type="submit" name=submit value="Send" />
</p>
</form>
<?php
} else {
$i=0;
$photoarray = array();
  foreach ($_FILES["pictures"]["error"] as $key => $error) {
  $file_name =  $_FILES["pictures"]['name'][$i]; // can call this anything you like this will take the original name
  $file =  $_FILES["pictures"]['tmp_name'][$i];
  $photoarray[$i+1]= $file_name;
  $endresult = "<font size=\"4\" color=990000>$file_name uploaded successfully</font>";
    if ($file_name == "") {
    $pic = $i+1;
    $endresult = "<font size=\"4\" color=990000>Pic#$pic Not selected</font>";
    }else{
      if(file_exists("$absolute_path/$file_name")) {
      $endresult = "<font size=\"4\" color=990000>File Already Existed</font>";
      } else {
        if (($size_limit == "yes") && ($limit_size < $file_size)) {
        $endresult = "<font size=\"4\" color=990000>File was to big</font>";
        } else {
        $ext = strrchr($file_name,'.');
          if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
          $endresult = "<font size=\"4\" color=990000>File is wrong type</font>";
          }else{
          // Save full size image with max width/height
          resampimage(1000,1000,$file,"$absolute_path/$file_name",0);
          // Save thumb image with max width/height of 200
          // resampimage(200,200,$file,"$thumb_path/$file_name",0);
          }
        }
      }
    }
  $i++;
  echo $endresult."<br>";
  }
}
?>
</body>
</html>

 

Ray

Link to comment
Share on other sites

  • 5 months later...
  • 2 years later...
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.