Page 1 of 1

Image upload script issue!!!

Posted: Sun Jan 31, 2010 3:18 am
by cap2cap10
Hello again, members of the php Technorati. I bring another one of my problems to your door step and humbly ask for your guidance. I have a great image upload script but I need it to automatically resize the image to a width of 320 pixels. I am not able to do this so I need your help. here is the script:

Code: Select all

class imageupload
{
    //pblic variables
    public $path = '';
    public $errorStr = '';
    public $imgurl = '';
 
    //private variables
    private $_errors = array();
    private $_params = array();
    private $_lang = array();
    private $_maxsize = 1048576;
 
    public $_im_status = false;
 
    //public methods
    public function __construct ()
    {
        require 'config.php';
        $this->_types = $types;
        $this->_lang = $lang;
        $this->_upload_dir = $upload_dir;
        $this->_maxsize = $maxsize;
 
        $this->path = $PHP_SELF;
        
        if (is_array($_FILES['__upload']))
        {
            $this->_params = $_FILES['__upload'];
            if (function_exists('exif_imagetype'))
                $this->_doSafeUpload();
            else
                $this->_doUpload();
 
            if (count($this->_errors) > 0)
                $this->_errorMsg();
        }
    }
 
    public function allowTypes ()
    {
        $str = '';
        if (count($this->_types) > 0) {
            $str = 'Allowed types: (';
            $str .= implode(', ', $this->_types);
            $str .= ')';
        }
 
        return $str;
    }
 
    // private methods
    private function _doSafeUpload ()
    {
        preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
        if (exif_imagetype($this->_params['tmp_name']) && in_array(strtolower($matches[1]), $this->_types))
        {
            if ($this->_params['size'] > $this->_maxsize)
                $this->_errors[] = $this->_lang['E_SIZE'];
            else
                $this->_im_status = true;
 
            if ($this->_im_status == true)
            {
                $ext = substr($this->_params['name'], -4);
                $this->new_name = md5(time()).$ext;
                 
   $pic_stat= $_POST['pic_stat'];
             $userID= $_POST['userID'];
             $image_name = $this->new_name;
 
             mysql_connect(**********************************) or die(mysql_error());
             mysql_select_db('js_info') or die(mysql_error());
 
             $query = "UPDATE js_resume SET image_name = '$image_name', pic_stat = '$pic_stat' WHERE userID = '$userID'";
 
             $result = mysql_query($query) or die(mysql_query());
             mysql_close();
 
                move_uploaded_file($this->_params['tmp_name'], $this->_upload_dir.$this->new_name);
 
                $this->imgurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$this->_upload_dir.$this->new_name;
            }
        }
        else
            $this->_errors[] = $this->_lang['E_TYPE'];
    }
 
    private function _doUpload ()
    {
        preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
        if(in_array(strtolower($matches[1]), $this->_types))
        {
            if ($this->_params['size'] > $this->_maxsize)
                $this->_errors[] = $this->_lang['E_SIZE'];
            else
                $this->_im_status = true;
 
            if ($this->_im_status == true)
            {
                $ext = substr($this->_params['name'], -3);
                $this->new_name = md5(time()).$ext;
                $pic_stat= $_POST['pic_stat'];
             $userID= $_POST['userID'];
             $image_name = $this->new_name;
            
             mysql_connect(*******************************) or die(mysql_error());
             mysql_select_db('js_info') or die(mysql_error());
 
              $query = "UPDATE js_resume SET image_name = '$image_name', pic_stat = '$pic_stat' WHERE userID = '$userID'";
 
             $result = mysql_query($query) or die(mysql_query());
             mysql_close();
 
            move_uploaded_file($this->_params['tmp_name'], $this->_upload_dir.$this->new_name);
 
                $this->imgurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$this->_upload_dir.'/'.$this->new_name;
 
 
            }
        }
        else
            $this->_errors[] = $this->_lang['E_TYPE'];
    }
 
    function _errorMsg()
    {
        $this->errorStr = implode('<br />', $this->_errors);
    }
}
 
?>
This script is functional, but again I need it to automatically resize the uploaded image to a width of 320 pixels before it renames the image and saves it to a folder. Can anyone lend me their expertise in this matter?
Thanks in advance,

Batoe

There is no such thing as a stupid question, only idiots who refuse to ask.

Re: Image upload script issue!!!

Posted: Sun Jan 31, 2010 4:16 am
by stuartshields
cap2cap10 I believe what your looking for is GD or Image Magik, (Just google it and you'll find information on it). It will basically resize the images for you without making it look crap. Unfortunately I am not the greatest person in this department to be talking to but I do know what you do need.