Image resize (someone must know) !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Image resize (someone must know) !

Post by MiniMonty »

Hi al,

been looking, searching and reading for hours and cannot find what I need.

I need an addition to my upload script which will resize users images to a max of 800px wide
and 500px high while maintaining the aspect ratio. I need this to happen only if the original
image is higher or wider than these dimensions. I also need the script to recognise whether the
image is "portrait" or "landscape" and behave accordingly.

Anyone got such a thing - or a link to a tutorial - or anything that might lead to success with this ?!?!
My upload script posted below for reference.

Best wishes
Monty

Code: Select all

 
<?php
session_start(); 
     if(isset($_GET['id']) || isset($_POST['id']));
 
 else if (isset($_SESSION['id'])) {
 
    
     $id = $_SESSION['id'];
 
} else {
    
   include_once "register.php";
   
   exit();
}
 
 
 
$galleryOwner= $id;
 
    // Connect to database, needed every time the page runs or is refreshed in a browser
include_once "scripts/connect_to_mysql.php";
 
    // Get the user session id variable into a local php variable for easier use in scripting
$id = $_SESSION['id'];
 
    //Define an array of acceptable image extensions
$acceptableextensions = array('jpg','jpeg','png','gif');
 
    //Define an array of acceptable MIME types
$acceptablemimetypes = array('image/jpg','image/jpeg','image/png','image/gif');
 
    //Define maximum file size
$maxfilesize = 256000; // 256000 bytes equals 250kb
 
function getExtension($str) {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}
    // Now initialize vars to be printed to page in the HTML section so our script does not return errors
    // they must be initialized in some server environments
$error_msg = "";
$success_msg = "";
$firstname = "";
$lastname = "";
$user_pic = "";
 
    // Parsing section for the  picture... only runs if they attempt to upload a picture
if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 0)){
    $image = $_FILES['fileField']['name'];
    $filename = stripslashes($_FILES['fileField']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
   
        //Filetype check
    if (!in_array($extension,$acceptableextensions)){
        exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
        echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
        $errors=1;
       
        //Filesize check
    } else if ($_FILES['fileField']['size'] > $maxfilesize ) {
        $error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
        unlink($_FILES['fileField']['tmp_name']);
    } else {
            // rename the picture incrementally
        $extension = getExtension($filename);
        $extension = strtolower($extension);
        $groovy = sizeof(glob("members/$id/images/*"));
        $groovy = ++$groovy;
 
        $image_name=$groovy.'.'.$extension;
        $newname="".$image_name;
        $gowhere = mysql_real_escape_string($_POST['gowhere']);
 
 
            // INSERT image into the DB
        $sql = mysql_query("INSERT INTO pictures
        (UID,filename,dirpath,gallery,dtadded)
        VALUES ('$id','$newname','/members/$id/images/$newname','$gowhere',now())")
        or die (mysql_error());
                       
        $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
        chmod ("members/$id/images/$newname", 0666);
        }
       
        $success_msg = '<span class="Big_Orange_Times">Your image has been uploaded, it may take a few moments to appear in your gallery, please be patient.</span>';
 
        } else if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 4)){ 
        //Error code 4: No image uploaded
    $error_msg = 'Please browse for an image before you press Go.';
}
 
// close else that checks file exists
?>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Member Upload</title>
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<link rel="icon" href="http://www.domain.com/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="http://www.domain.com/favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 
</head>
 
<body>
<?php include_once "header_template.php"; ?>
<br>
<br>
<br>
<br>
<br>
<table width="975" height="366"  border="0" align="center">
 
    <td width="44%" height="328"><div align="center">
      <table width="100%" height="270"  border="0">
        <tr>
          <td height="322"><div align="center">
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="426" height="320">
              <param name="movie" value="images/required_site_pics/uploadBlurb.swf">
              <param name="quality" value="high">
              <embed src="images/required_site_pics/uploadBlurb.swf" FlashVars="user_num=<?php echo "$galleryOwner";?>"quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="426" height="320"></embed>
            </object>
          </div></td>
        </tr>
      </table>
    </div>    </td>
    <td width="56%" rowspan="2">
   
    <div align="center"><?php include_once  "showpics.php"; ?>
      <br>
      </div></td>
  </tr>  <tr>
      <td height="32"><form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
        <table width="70%"  border="0" align="center">
          <tr>
            <td><div align="center">
                <input name="fileField" type="file" id="fileField" size="0" maxlength="0">
            </div></td>
            <td><div align="center">Which Gallery?
                <select name="gowhere" id="gowhere">
                  <option value="portraits" selected>People</option>
                  <option value="landscapes">Landscapes</option>
                  <option value="urban">Urban</option>
                  <option value="travel">Travel</option>
                  <option value="action">Action</option>
                  <option value="reportage">Reportage</option>
                </select>
            </div></td>
            <td><div align="center">
                <input name="button" type="submit" id="button" value="Go">
                <input name="parse_var" type="hidden" value="pic" />
            </div></td>
          </tr>
        </table>
      </form></td>
    </tr>
</table>
<table width="977" height="191" border="0" align="center">
  <tr>
    <td><span class="Big_Blue_Times"><?php print "$error_msg"; ?> </span>      <div align="center"><br />
    <span class="Big_Orange_Times"><?php print "$success_msg"; ?></span></div></td>
  </tr>
</table>
<br>
<p>
  <?php include_once "footer_template.php"; ?>
</p>
<p>&nbsp;</p>
</body>
</html>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image resize (someone must know) !

Post by requinix »

Plenty of links here.

Searching for hours? Please.
Post Reply