Jump to content

How do I resize the uploaded image?


Skipjackrick

Recommended Posts

So I've got this image script working well for me.  But, I'd like to resize the image.

 

What do I use?

 

Here is the bit of code I think needs to be elaborated on but I've included the whole file if needed.

 

<?php
// make a unique filename for the uploaded file
$uploadFilename = $uploadsDirectory.$now.'_angler'.$anglerId.'_team'.$team.'.jpg';


// now let's move the file to its final and allocate it with the new filename
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);

$image_name = ''.$now.'_angler'.$anglerId.'_team'.$team.'.jpg';
?>

 

 

The rest of the file for reference.

 

<?php  // filename: upload.processor.php


// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the directory that will recieve the uploaded files
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'submitted_pics/';

// make a note of the location of the upload form in case we need it
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'photo.php';

// name of the fieldname used for the file in the HTML form
$fieldname = 'file';


if (!($_FILES[$fieldname]['type'] =='image/pjpeg' OR $_FILES[$fieldname]['type'] =='image/jpeg'))
{

header("Refresh: 5; URL=addaprofile_photos.php");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
'<html lang="en">'."\n".
'	<head>'."\n".
'		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
'		<link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
'	<title>Upload error</title>'."\n\n".
'	</head>'."\n\n".
'	<body>'."\n\n".
'	<div id="Upload">'."\n\n".
'		<h1>Error: Only non-progressive JPG file types are accepted</h1>'."\n\n".
'		<p>Please Try Again: '."\n\n".
'		<span class="red">' . $error . '...</span>'."\n\n".
'	 	The upload form is reloading</p>'."\n\n".
'	 </div>'."\n\n".
'</html>';
exit;

} else {

// Now let's deal with the upload

// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded', 
                2 => 'html form max file size exceeded', 
                3 => 'file upload was only partial', 
                4 => 'no file was attached');

// check the upload form was actually submitted else print form
isset($_POST['submit'])
or error('the upload form is neaded', $uploadForm);

// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
or error('not an HTTP upload', $uploadForm);

  
// validation... since this is an image upload script we 
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'])
or error('only image uploads are allowed', $uploadForm);

$now = time();
$anglerId = $_GET['anglerId'];
$team = $_GET['team'];

// make a unique filename for the uploaded file
$uploadFilename = $uploadsDirectory.$now.'_angler'.$anglerId.'_team'.$team.'.jpg';


// now let's move the file to its final and allocate it with the new filename
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);


$image_resolution = 'submitted_pics/'.$now.'_angler'.$anglerId.'_team'.$team.'.jpg';

//Check to make sure the resolution is accurate
      list($width, $height) = getimagesize($image_resolution);

      $widthMin  = 479;
      $heightMin = 479;

      if( ($width < $widthMin) || ($height < $heightMin) ) {
        $errorMessage = 'The minimum resolution for photos must be 640 x 480 pixels';

      $_SESSION['errorCount'] = 1;
      $_SESSION['errorMessage'] = $errorMessage;
      header("Location: team_error.php");
      exit;
      }

    
//if an error
echo(mysql_error()); 


$region = $_GET['region'];

// make a note of the location of the success page and call the team_id and region_id variables to pass
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'exif_check.php?team_id=' . $team . '&region_id=' . $region . '';

// If you got this far, everything has worked and the file has been successfully saved.
// We are now going to redirect the client to the success page.
header('Location: ' . $uploadSuccess);


// make an error handler which will be used if the upload fails
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL=\"$location\"");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
'<html lang="en">'."\n".
'	<head>'."\n".
'		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
'		<link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
'	<title>Upload error</title>'."\n\n".
'	</head>'."\n\n".
'	<body>'."\n\n".
'	<div id="Upload">'."\n\n".
'		<h1>Upload failure</h1>'."\n\n".
'		<p>An error has occured: '."\n\n".
'		<span class="red">' . $error . '...</span>'."\n\n".
'	 	The upload form is reloading</p>'."\n\n".
'	 </div>'."\n\n".
'</html>';
exit;
} // end error handler
} // end if else statement concerning file type
?>

Link to comment
Share on other sites

<?php
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {
   
   var $image;
   var $image_type;

   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;   
   }      
}
?>

 

This is the code I used on my old image hosting site. I did not create it, found it online.

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.