codice:
class uploadPhoto{
private $tempimg,
$max_size,
$path,
$prename,
$crop;
public function imgMaker($tempimg,$max_size,$path,$prename,$crop){
//check if is empty
if($_FILES[$tempimg]['name']!=""){
ob_start();
//find name and type
$uploadedfile = $_FILES[$tempimg]['tmp_name'];
list($width,$height,$image_type)=getimagesize($uploadedfile);
switch($image_type){
case 1:
$src = imagecreatefromgif($uploadedfile);
break;
case 2:
$src = imagecreatefromjpeg($uploadedfile);
break;
case 3:
$src = imagecreatefrompng($uploadedfile);
break;
default:
return '';
break;
}
//for png and gif image that maintein the alpha
if(($image_type == 1) || ($image_type==3)){
imagealphablending($tmp, false);
imagesavealpha($tmp,true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
}
// resize and crop for litle image
if($crop==1){
if($width>$height){
$y_pos = ($width-$height ) / 2;
$y_pos = ceil($y_pos);
$newheight=$max_size;
$newwidth=($width/$height)*$newheight;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp=imagecreatetruecolor($max_size,$newheight);
imagecopyresampled($tmp,$src,0,0,$y_pos,0,$newwidth,$newheight,$width,$height);
} else {
$x_pos = ($height-$width) / 2;
$x_pos = ceil($x_pos);
$newwidth=$max_size;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp=imagecreatetruecolor($newwidth,$max_size);
imagecopyresampled($tmp,$src,0,0,0,$x_pos,$newwidth,$newheight,$width,$height);
}
}else{
$newwidth=$max_size;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
}
$nome_file=$_FILES[$tempimg]['name'];
$tipo = strtolower(substr($nome_file, strrpos($nome_file, "."), strlen($nome_file)-strrpos($nome_file, ".")));
$nameImg=md5($nome_file.rand()).$tipo;
//$nameImg=uniqid(23,false).$tipo;
if($prename){
$nameImg=$prename.$nameImg;
}
$filename = $path.$nameImg;
switch ($image_type){
case 1:
imagegif($tmp,$filename);
break;
case 2:
imagejpeg($tmp,$filename,100);
break; // best quality
case 3:
imagepng($tmp,$filename,0);
break; // no compression
default:
return '';
break;
}
imagedestroy($src);
imagedestroy($tmp);
//ob_clean();
return $nameImg;
}
}
}