Посмотри этот класс
class UploadImages{
private $_file;
private $_adres;
private $_name_final = false;
private $_error = false;
private $_weight = 0;
private $_height = 0;
private $_weight_final = 0;
private $_height_final = 0;
private $_weight_max = 0;
private $_height_max = 0;
private $_permitted = array('image/gif',
'image/jpeg',
'image/pjpeg',
'image/png');
public function __construct($f){
$this->_file = current($f);
$this->_setErrorUpload($this->_file['error']);
}
public function setAdres($a){
$this->_adres = $a;
}
public function setMaxSize($w_max, $h_max){
$this->_weight_max = $w_max;
$this->_height_max = $h_max;
}
public function uploadImg(){
if(!empty($this->_error)){
return false;
}
$imageinfo = getimagesize($this->_file['tmp_name']);
$permitted = $this->_permittedValide($imageinfo['mime']);
$this->_weight = $imageinfo[0];
$this->_height = $imageinfo[1];
if($this->_weight_max > 0 or $this->_height_max > 0){
$this->_getSize($imageinfo[0], $imageinfo[1]);
}else{
$this->_weight_final = $imageinfo[0];
$this->_height_final = $imageinfo[1];
}
$name = $this->_getRandomName();
switch($permitted){
case 'jpg': $this->_jpgLoad($this->_file['tmp_name'], $name); break;
case 'jpeg': $this->_jpgLoad($this->_file['tmp_name'], $name); break;
case 'gif': $this->_gifLoad($this->_file['tmp_name'], $name); break;
case 'png': $this->_pngLoad($this->_file['tmp_name'], $name); break;
default: $this->_error = 'Неверный тип файла'; break;
}
return true;
}
private function _getSize($weight, $height){
$res;
if($weight > $this->_weight_max and $this->_weight_max > 0){
$ratio = $this->_weight_max/$weight;
$height = round($height*$ratio);
$weight = $this->_weight_max;
}
if($height > $this->_height_max and $this->_height_max > 0){
$ratio = $this->_height_max/$height;
$weight = round($weight*$ratio);
$height = $this->_height_max;
}
$this->_weight_final = $weight;
$this->_height_final = $height;
return true;
}
public function getImgSize(){
$res = array();
$res['w'] = $this->_weight_final;
$res['h'] = $this->_height_final;
return $res;
}
private function _permittedValide($p){
if(empty($this->_permitted) or empty($p)){return false;}
for($i = 0; count($this->_permitted) > $i; $i++){
if($this->_permitted[$i] === $p){
$perm = explode("/", $p);
return $perm[1];
}
}
return false;
}
private function _setErrorUpload($upload){
switch($upload){
case 1: $this->_error = 'Размер загружаемого файла превышает максимальный размер ini'; break;
case 2: $this->_error = 'Размер загружаемого файла превышает максимальный размер MAX_FILE_SIZE'; break;
case 3: $this->_error = 'Файл загружен частично'; break;
case 4: $this->_error = 'Данные формы загружены без указания файла'; break;
case 6: $this->_error = 'Временная папка отсутствует'; break;
case 7: $this->_error = 'Файл невозможно записать на диск'; break;
case 8: $this->_error = 'Процесс загрузки остановлен неопределенным PHP-расширением'; break;
}
}
public function getError(){
return $this->_error;
}
private function _getRandomName(){
return time().rand(1, 100);
}
public function getNameFinal(){
if(!empty($this->_name_final)){
return $this->_name_final;
}else{
return false;
}
}
private function _jpgLoad($tmp_name, $name){
$res = true;
$src_img = imagecreatefromjpeg($tmp_name);
$dst = imagecreatetruecolor($this->_weight_final, $this->_height_final);
imagecopyresampled($dst,
$src_img,
0,
0,
0,
0,
$this->_weight_final,
$this->_height_final,
$this->_weight,
$this->_height);
if(!imagejpeg($dst, $this->_adres.$name.'.jpg')){
$this->_error = 'Ошибка при загрузке файла.';
$res = false;
}
imagedestroy($src_img);
imagedestroy($dst);
$this->_name_final = $name.'.jpg';
return $res;
}
private function _pngLoad($tmp_name, $name){
$res = true;
$src_img = imagecreatefrompng($tmp_name);
$dst = imagecreatetruecolor($this->_weight_final, $this->_height_final);
imagecopyresampled($dst,
$src_img,
0,
0,
0,
0,
$this->_weight_final,
$this->_height_final,
$this->_weight,
$this->_height);
if(!imagepng($dst, $this->_adres.$name.'.png')){
$this->_error = 'Ошибка при загрузке файла.';
$res = false;
}
imagedestroy($src_img);
imagedestroy($dst);
$this->_name_final = $name.'.png';
return $res;
}
private function _gifLoad($tmp_name, $name){
$res = true;
$src_img = imagecreatefromgif($tmp_name);
$dst = imagecreatetruecolor($this->_weight_final, $this->_height_final);
imagecopyresampled($dst,
$src_img,
0,
0,
0,
0,
$this->_weight_final,
$this->_height_final,
$this->_weight,
$this->_height);
if(!imagegif($dst, $this->_adres.$name.'.gif')){
$this->_error = 'Ошибка при загрузке файла.';
$res = false;
}
imagedestroy($src_img);
imagedestroy($dst);
$this->_name_final = $name.'.gif';
return $res;
}
}
if(!empty($_FILES)){
$a = new UploadImages($_FILES);
$a->setMaxSize(0, 300);
$a->setAdres('C:\images\\');
$a->uploadImg();
$size = $a->getImgSize();
echo 'w = '.$size['w'].' | h = '.$size['h'].'<br/>';
echo $a->getError();
}