Php uploads files and generates thumbnail code
If (isset ($ _ FILES ['upimg '])
{
If ($ userGroup [$ loginArr ['group'] ['upload'] = 0)
{
Echo '{"error": "Your User Group does not have the permission to upload images! "}';
}
Else
{
$ SavePath = "attachment/img/". date ('y/m/d/H ');
MkDirs ($ savePath );
$ FileType = strtolower (strrchr ($ _ FILES ['upimg '] ['name'], ".");
If (! In_array ($ fileType, array (". jpg", ". jpeg", ". gif", ". png ")))
{
Echo '{"error": "currently, only jpg, jpeg, gif, and png images are supported! "}';
}
Elseif ($ _ FILES ['upimg '] ['SIZE']> 204800)
{
Echo '{"error": "The image size cannot exceed 200 kb! "}';
}
Else
{
$ SaveImg = $ savePath. "/". $ loginArr ['uid']. "_". time (). rand (). $ fileType;
If (@ move_uploaded_file ($ _ FILES ['upimg '] ['tmp _ name'], $ saveImg ))
{
Echo '{"error": "", "msg": "http: //'. $ site_domain. $ site_catalog. $ saveImg .'"}';
}
Else
{
Echo '{"error": "Image Upload Failed! "}';
}
}
}
}
If ($ loginArr ['state'] = 0)
{
Echo '{"error": "You have not logged on! "}';
}
Else
{
$ AvatarPath = "attachment/avatar /". ($ loginArr ['uid'] % 32 ). "/". ($ loginArr ['uid'] % 257 ). "/". $ loginArr ['uid'];
If (isset ($ _ FILES ['upload'])
{
MkDirs ($ avatarPath );
$ FileType = strtolower (strrchr ($ _ FILES ['upload'] ['name'], ".");
If (! In_array ($ fileType, array (". jpg", ". jpeg", ". gif", ". png ")))
{
Echo '{"error": "currently, only jpg, jpeg, gif, and png images are supported! "}';
}
Elseif ($ _ FILES ['upload'] ['SIZE']> 2097152)
{
Echo '{"error": "The image cannot exceed 2 MB! "}';
}
Else
{
$ ImgInfo = @ getimagesize ($ _ FILES ['upload'] ['tmp _ name']);
If (! $ ImgInfo |! In_array ($ imgInfo [2], array (1, 2, 3 )))
{
Echo '{"error": "The system cannot identify the file you uploaded! "}';
}
Else
{
$ TmpAvatar = $ avatarPath. "/temp". $ fileType;
If (@ move_uploaded_file ($ _ FILES ['upload'] ['tmp _ name'], $ TmpAvatar ))
{
$ MaxWidth = 520;
$ MaxHeight = 520;
If ($ maxWidth> $ imgInfo [0] | $ maxHeight> $ imgInfo [1])
{
$ MaxWidth = $ imgInfo [0];
$ MaxHeight = $ imgInfo [1];
}
Else
{
If ($ imgInfo [0] <$ imgInfo [1])
$ MaxWidth = ($ maxHeight/$ imgInfo [1]) * $ imgInfo [0];
Else
$ MaxHeight = ($ maxWidth/$ imgInfo [0]) * $ imgInfo [1];
}
If ($ maxWidth <40)
{
$ MaxWidth = 40;
}
If ($ maxHeight <40)
{
$ MaxHeight = 40;
}
$ Image_p = imagecreatetruecolor ($ maxWidth, $ maxHeight );
Switch ($ imgInfo [2])
{
Case 1:
$ Image = imagecreatefromgif ($ TmpAvatar );
Break;
Case 2:
$ Image = imagecreatefromjpeg ($ TmpAvatar );
Break;
Case 3:
$ Image = imagecreatefrompng ($ TmpAvatar );
Break;
}
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ maxWidth, $ maxHeight, $ imgInfo [0], $ imgInfo [1]);
Imagejpeg ($ image_p, $ avatarPath. "/temp.jpg", 100 );
Imagedestroy ($ image_p );
Imagedestroy ($ image );
If ($ fileType! = ". Jpg" & file_exists ($ TmpAvatar ))
{
Unlink ($ TmpAvatar );
}
Echo '{"error": "", "url": "'. $ avatarPath. '/temp.jpg? '. Rand ().' "," width ":" '. $ maxWidth.' "," height ":" '. $ maxHeight .'"}';
}
Else
{
Echo '{"error": "Image Upload Failed! "}';
}
}
}
}
If (isset ($ _ POST ['X'], $ _ POST ['y'], $ _ POST ['W'], $ _ POST ['H'])
{
If (is_numeric ($ _ POST ['X']) & is_numeric ($ _ POST ['y']) & $ _ POST ['W']> 0 & $ _ POST ['H']> 0)
{
$ Image_p = imagecreatetruecolor (40, 40 );
$ Image = imagecreatefromjpeg ($ avatarPath. "/temp.jpg ");
Imagecopyresampled ($ image_p, $ image, 0, 0, $ _ POST ['X'], $ _ POST ['y'], 40, 40, $ _ POST ['W'], $ _ POST ['H']);
Imagejpeg ($ image_p, $ avatarPath. "/40_40.jpg", 100 );
Imagedestroy ($ image_p );
Imagedestroy ($ image );
Unlink ($ avatarPath. "/temp.jpg ");
Echo "1 ";
}
}
If (isset ($ _ POST ['Avatar ']) & $ _ POST ['Avatar'] = "delete ")
{
If (file_exists ($ avatarPath. "/temp.jpg "))
{
Unlink ($ avatarPath. "/temp.jpg ");
}
}
}
Ob_end_flush ();