创建显示缩略图或加载50%的图像并创建图像预览

时间:2014-11-19 18:08:04

标签: php image thumbnails

使用下面给出的代码从单个输入上传多个文件并进行处理 如何创建拇指表单图像或将图像加载50%并创建低质量图像

    <?php

$newname = md5(rand() * time());
$file1 = isset($_FILES['files']['name'][0]) ? $_FILES['files']['name'][0] : null;
$file2 = isset($_FILES['files']['name'][1]) ? $_FILES['files']['name'][1] : null;
$file3 = isset($_FILES['files']['name'][2]) ? $_FILES['files']['name'][2] : null;
$file4 = isset($_FILES['files']['name'][3]) ? $_FILES['files']['name'][3] : null;
$file5 = isset($_FILES['files']['name'][4]) ? $_FILES['files']['name'][4] : null;
if (isset($_FILES['files'])) {
    $errors = array();
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
        $file_name = $key . $_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];
        if ($file_size > 2097152) {
            $errors[] = 'File size must be less than 2 MB';
        }
        $desired_dir = "user_data";
        if (empty($errors) == true) {
            if (is_dir($desired_dir) == false) {
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if (is_dir("$desired_dir/" . $file_name) == false) {
                move_uploaded_file($file_tmp, "$desired_dir/" . $newname . $file_name);
            } else {                                  // rename the file if another one exist
                $new_dir = "$desired_dir/" . $newname . $file_name;
                rename($file_tmp, $new_dir);
            }
        } else {
            print_r($errors);
        }
    }
    if (empty($error)) {
        echo "FILE : $file1<br>";
        echo "FILE : $file2<br>";
        echo "FILE : $file3<br>";
        echo "FILE : $file4<br>";
        echo "FILE : $file5<br>";
    }
}
?>

如果使用512 kb上传图像,则预览图像应加载少于预设的原始文件大小

1 个答案:

答案 0 :(得分:0)

使用以下代码创建缩略图图像,您可以创建所有类型的图像文件jpeg, png, gif

    $orig_directory = "$desired_dir";    //Full image folder
    $thumb_directory = "thumb/";    //Thumbnail folder

    /* Opening the thumbnail directory and looping through all the thumbs: */
    $dir_handle = @opendir($orig_directory); //Open Full image dirrectory
    if ($dir_handle > 1) { //Check to make sure the folder opened
        $allowed_types = array('jpg', 'jpeg', 'gif', 'png');
        $file_type = array();
        $ext = '';
        $title = '';
        $i = 0;

        while ($file_name = @readdir($dir_handle)) {
            /* Skipping the system files: */
            if ($file_name == '.' || $file_name == '..')
                continue;

            $file_type = explode('.', $file_name);    //This gets the file name of the images
            $ext = strtolower(array_pop($file_type));

            /* Using the file name (withouth the extension) as a image title: */
            $title = implode('.', $file_type);
            $title = htmlspecialchars($title);

            /* If the file extension is allowed: */
            if (in_array($ext, $allowed_types)) {

                /* If you would like to inpute images into a database, do your mysql query here */

                /* The code past here is the code at the start of the tutorial */
                /* Outputting each image: */

                $nw = 100;
                $nh = 100;
                $source = "$desired_dir{$file_name}";
                $stype = explode(".", $source);
                $stype = $stype[count($stype) - 1];
                $dest = "thumb/{$file_name}";

                $size = getimagesize($source);
                $w = $size[0];
                $h = $size[1];

                switch ($stype) {
                    case 'gif':
                        $simg = imagecreatefromgif($source);
                        break;
                    case 'jpg':
                        $simg = imagecreatefromjpeg($source);
                        break;
                    case 'png':
                        $simg = imagecreatefrompng($source);
                        break;
                }

                $dimg = resizePreservingAspectRatio($simg, $nw, $nh);
                imagepng($dimg, $dest);
            }
        }

        /* Closing the directory */
        @closedir($dir_handle);
    }
}

function resizePreservingAspectRatio($img, $targetWidth, $targetHeight) {
    $srcWidth = imagesx($img);
    $srcHeight = imagesy($img);

    // Determine new width / height preserving aspect ratio
    $srcRatio = $srcWidth / $srcHeight;
    $targetRatio = $targetWidth / $targetHeight;
    if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight)) {
        $imgTargetWidth = $srcWidth;
        $imgTargetHeight = $srcHeight;
    } else if ($targetRatio > $srcRatio) {
        $imgTargetWidth = (int) ($targetHeight * $srcRatio);
        $imgTargetHeight = $targetHeight;
    } else {
        $imgTargetWidth = $targetWidth;
        $imgTargetHeight = (int) ($targetWidth / $srcRatio);
    }

    // Creating new image with desired size
    $targetImg = imagecreatetruecolor($targetWidth, $targetHeight);

    // Add transparency if your reduced image does not fit with the new size
    $targetTransparent = imagecolorallocate($targetImg, 255, 0, 255);
    imagefill($targetImg, 0, 0, $targetTransparent);
    imagecolortransparent($targetImg, $targetTransparent);

    // Copies image, centered to the new one (if it does not fit to it)
    imagecopyresampled($targetImg, $img, 0, 0, 0, 0, $targetWidth, $targetHeight, $srcWidth, $srcHeight);

    return $targetImg;
}

?>