繁体   English   中英

如何使用PHP上传不同文件夹中不同大小的同一文件?

[英]How to upload a same file with different size in different folder using php?

如何使用PHP在不同的文件夹中上传不同大小(拇指,原始大小)的相同文件?

我试过以下代码:

//无论原始尺寸如何,此功能都可以拍摄图像以创建精确的方形图像!

function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
        {    
            //Check Image size is not 0
            if($CurWidth <= 0 || $CurHeight <= 0) 
            {
                return false;
            }

            //abeautifulsite.net has excellent article about "Cropping an Image to Make Square bit.ly/1gTwXW9
            if($CurWidth>$CurHeight)
            {
                $y_offset = 0;
                $x_offset = ($CurWidth - $CurHeight) / 2;
                $square_size    = $CurWidth - ($x_offset * 2);
            }else{
                $x_offset = 0;
                $y_offset = ($CurHeight - $CurWidth) / 2;
                $square_size = $CurHeight - ($y_offset * 2);
            }

            $NewCanves  = imagecreatetruecolor($iSize, $iSize); 
            if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
            {
                switch(strtolower($ImageType))
                {
                    case 'image/png':
                        imagepng($NewCanves,$DestFolder);
                        break;
                    case 'image/gif':
                        imagegif($NewCanves,$DestFolder);
                        break;          
                    case 'image/jpeg':
                    case 'image/pjpeg':
                        imagejpeg($NewCanves,$DestFolder,$Quality);
                        break;
                    default:
                        return false;
                }
            //Destroy image, frees memory   
            if(is_resource($NewCanves)) {imagedestroy($NewCanves);} 
            return true;

            }

        } 




     move_uploaded_file($_FILES["st_photo"]["tmp_name"],"upload/original/" . $photo);

    $ThumbSquareSize        = 100; //Thumbnail will be 200x200
    $ThumbPrefix            = "thumb_"; //Normal thumb Prefix
    $DestinationDirectory   = 'upload/thumb/'; //specify upload directory ends with / (slash)
    $Quality                = 90; //jpeg quality
    // Random number will be added after image name
    $RandomNumber   = rand(0, 9999999999); 
    $ImageName      = str_replace(' ','-',strtolower($_FILES['st_photo']['name'])); //get image name
    $ImageSize      = $_FILES['st_photo']['size']; // get original image size
    $TempSrc        = $_FILES['st_photo']['tmp_name']; // Temp name of image file stored in PHP tmp folder
    $ImageType      = $_FILES['st_photo']['type']; //get file type, returns "image/png", image/jpeg, text/plain etc.

    //Let's check allowed $ImageType, we use PHP SWITCH statement here
    switch(strtolower($ImageType))
    {
        case 'image/png':
            //Create a new image from file 
            $CreatedImage =  imagecreatefrompng($_FILES['st_photo']['tmp_name']);
            break;
        case 'image/gif':
            $CreatedImage =  imagecreatefromgif($_FILES['st_photo']['tmp_name']);
            break;          
        case 'image/jpeg':
        case 'image/pjpeg':
            $CreatedImage = imagecreatefromjpeg($_FILES['st_photo']['tmp_name']);
            break;
        default:
            die('Unsupported File!'); //output error and exit
    }
    //PHP getimagesize() function returns height/width from image file stored in PHP tmp folder.
    //Get first two values from image, width and height. 
    //list assign svalues to $CurWidth,$CurHeight
    list($CurWidth,$CurHeight)=getimagesize($TempSrc);
    //Get file extension from Image name, this will be added after random name
    $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
    $ImageExt = str_replace('.','',$ImageExt);
    //remove extension from filename
    $ImageName      = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName); 
    //Construct a new name with random number and extension.
    $NewImageName = $ImageName.'-'.$RandomNumber.'.'.$ImageExt;

    //set the Destination Image
    $thumb_DestRandImageName    = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumbnail name with destination directory
    $DestRandImageName          = $DestinationDirectory.$NewImageName; // Image with destination directory

    //Create a square Thumbnail right after, this time we are using cropImage() function
    if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType))
    {
        echo 'Error Creating thumbnail';
    }

谢谢

我用这个功能

做这样的事情很棒

使用jQuery-File-Upload:

https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin

它非常强大且易于设置。

您可以选择在客户端进行图片大小调整:

https://github.com/blueimp/jQuery-File-Upload/wiki/Client-side-Image-Resizing

或让服务器端处理图片大小调整:

在服务器端php文件:jQuery-File-Upload / server / php / UploadHandler.php

你可以找到:

            'medium' => array(
                'max_width' => 800,
                'max_height' => 600
            ),
            */
            'thumbnail' => array(
                // Uncomment the following to use a defined directory for the thumbnails
                // instead of a subdirectory based on the version identifier.
                // Make sure that this directory doesn't allow execution of files if you
                // don't pose any restrictions on the type of uploaded files, e.g. by
                // copying the .htaccess file from the files directory for Apache:
                //'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/thumb/',
                //'upload_url' => $this->get_full_url().'/thumb/',
                // Uncomment the following to force the max
                // dimensions and e.g. create square thumbnails:
                //'crop' => true,
                'max_width' => 80,
                'max_height' => 80
            ) 

在这里,您可以更改高度和宽度。

它会为您生成不同大小的图片。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM