PHP picture upload and generate thumbnail code

Source: Internet
Author: User
Tags file size imagejpeg

if ($_files[' image '] [' size ']) {
if ($_files[' image '] [' type '] = = "Image/pjpeg") {
$im = @imagecreatefromjpeg ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. jpg ';
}elseif ($_files[' image '] [' type '] = = "Image/x-png") {
$im = @imagecreatefrompng ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. png ';
}elseif ($_files[' image '] [' type '] = = "Image/gif") {
$im = @imagecreatefromgif ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. gif ';
}
Resizeimage ($im, 8888,8888, $n _bmp);
Imagedestroy ($im);
$n _tag = 1;

}

function UploadFile ($STR) {

$save _path = './product/';//File Save directory path

$ext _arr = Array (' rar ', ' zip ', ' jpg ', ' gif ', ' PNG ', ' BMP ');//define file extensions that allow uploads

$max _size = 1000000;//Maximum file size

$file _rand = ';

$file _ext = ';

@mkdir ($save _path, 0777); Change directory Permissions

if ($_files[$str] [' name ']) {//When uploading files

$file _name = $_files[$str] [' name '];//original filename

$tmp _name = $_files[$str] [' tmp_name '];//temporary file name on the server]

$file _size = $_files[$str] [' Size '];//file size

if (@is_dir ($save _path) = = False) {//Check directory
Alert ("The upload directory does not exist.) ",'');
}

if (@is_writable ($save _path) = = False) {//Check directory Write permissions
Alert ("Upload directory does not have write permission.) ",'');
}

if (@is_uploaded_file ($tmp _name) = = False) {//Check if uploaded
Alert ("Temporary file" may not be an upload file.) ",'');
}

if ($file _size > $max _size) {//Check file size
Alert ("Upload file size exceeds limit.") ",'');
}

$temp _arr = Explode (".", $file _name);//Get File name extension
$file _ext = Array_pop ($temp _arr);
$file _ext = Trim ($file _ext);
$file _ext = strtolower ($file _ext);
$file _rand= MD5 (date ("y-m-d"));
if (In_array ($file _ext, $ext _arr) = = False) {//Check extension
Alert ("The upload file name extension is an disallowed extension.") ",'');
}

if (Move_uploaded_file ($tmp _name, $save _path. $file _rand. '. ') $file _ext) = = False) {//Move file
Alert ("Upload file failed.") ",'');
}
}
return $save _path $file _rand. $file _ext;
}


function Resizeimage ($im, $maxwidth, $maxheight, $name) {
$width = Imagesx ($im);
$height = Imagesy ($im);
if ($maxwidth && $width > $maxwidth) | | ($maxheight && $height > $maxheight)) {
if ($maxwidth && $width > $maxwidth) {
$widthratio = $maxwidth/$width;
$RESIZEWIDTH =true;
}
if ($maxheight && $height > $maxheight) {
$heightratio = $maxheight/$height;
$RESIZEHEIGHT =true;
}
if ($RESIZEWIDTH && $RESIZEHEIGHT) {
if ($widthratio < $heightratio) {
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif ($RESIZEWIDTH) {
$ratio = $widthratio;
}elseif ($RESIZEHEIGHT) {
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if (function_exists ("imagecopyresampled")) {
$newim = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = Imagecreate ($newwidth, $newheight);
Imagecopyresized ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
Imagejpeg ($newim, './product_e/'. $name);
Imagedestroy ($newim);
}else{
Imagejpeg ($im, './product_e/'. $name);
}
}

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.