Php uploads files and generates thumbnail code

Source: Internet
Author: User
Tags imagejpeg

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 ();


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.