PHP upload files and generate thumbnail code _php tutorial

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 permission to upload pictures! '} ';
}
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 supports images formatted as JPG, JPEG, GIF, PNG!"} ';
}
ElseIf ($_files[' upimg ' [' Size '] > 204800)
{
Echo ' {' Error ': ' The picture cannot exceed 200k! '} ';
}
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 ': ' Picture upload failed! '} ';
}
}
}
}

if ($loginArr [' state '] = = 0)
{
Echo ' {' ERROR ': ' You are not logged in! '} ';
}
Else
{
$avatarPath = "attachment/avatar/". ($LOGINARR [' uid ']%32). " /". ($LOGINARR [' uid ']%257). " /". $LOGINARR [' uid '];

if (Isset ($_files[' Upavatar '))
{
Mkdirs ($avatarPath);

$fileType = Strtolower (STRRCHR ($_files[' Upavatar ' [' Name '], "."));

if (!in_array ($fileType, Array (". jpg", ". jpeg", ". gif", ". png")))
{
Echo ' {"error": "Currently only supports images formatted as JPG, JPEG, GIF, PNG!"} ';
}
ElseIf ($_files[' Upavatar ' [' Size '] > 2097152)
{
Echo ' {' Error ': ' The picture cannot exceed 2mb! '} ';
}
Else
{
$imgInfo = @getimagesize ($_files[' Upavatar ' [' tmp_name ']);

if (! $imgInfo | |!in_array ($imgInfo [2], Array ()))
{
Echo ' {' Error ': ' The system does not recognize the file you uploaded! '} ';
}
Else
{
$TmpAvatar = $avatarPath. " /temp ". $fileType;

if (@move_uploaded_file ($_files[' Upavatar ' [' 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 ': ' Picture 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 '], max,, $_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 ();


http://www.bkjia.com/PHPjc/444998.html www.bkjia.com true http://www.bkjia.com/PHPjc/444998.html techarticle PHP uploads the file and generates the thumbnail code if (isset ($_files[' upimg ')) {if ($userGroup [$LOGINARR [' Group ']][' upload '] = = 0) {echo ' {error: You The user group is not authorized to upload pictures!...

  • 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.