move_uploaded_file php

时间:2016-02-20 09:50:52

标签: php image resize gd

我尝试在使用GD进行处理之前弄清楚如何调整图像大小(使用resize类)。我在这里发布了一小段代码。这个脚本背后的想法是具有大尺寸和/或小型服务器内存的GD图像不会处理。所以我想过滤大于2500px宽度的图片,只在上传之前调整这些图片的大小。但我无法弄清楚如何以及正确的顺序。

    require('resize_image_class.php');

    $file = $_FILES['bestand1']['tmp_name'];
    $bestanden = 'beeld_bestanden/nieuws-item-' . $item_id . '-foto1.' . $encode1 . '.' . $ext;
    $resized = 'beeld_bestanden/nieuws-item-' . $item_id . '-foto1.' . $encode1 . '.' . $ext;// overwrite somehow..
    move_uploaded_file($file,$bestanden);   

    list($width, $height) = getimagesize($file);
    if($width > 2000){

    $resize = new ResizeImage($bestanden);
    $resize->resizeTo(2500, 2500, 'maxWidth');
    $resize->saveImage($resized);
    } 

$ res大小的输出是我用于进一步处理的新基本图像,就像GD一样。调整大小与否的上传文件必须以:$ bestanden结尾。 有人可以用代码示例帮助我。

这是调整大小的类:

class ResizeImage
{
private $ex;
private $image;
private $newImage;
private $origWidth;
private $origHeight;
private $resizeWidth;
private $resizeHeight;

/**
 * Class constructor requires to send through the image filename
 *
 * @param string $filename - Filename of the image you want to resize
 */
public function __construct( $filename )
{
    if(file_exists($filename))
    {
        $this->setImage( $filename );
    } else {
        throw new Exception('Image ' . $filename . ' can not be found, try another image.');
    }
}

/**
 * Set the image variable by using image create
 *
 * @param string $filename - The image filename
 */
private function setImage( $filename )
{
    $size = getimagesize($filename);
    $this->ex = $size['mime'];

    switch($this->ex)
    {
        // Image is a JPG
        case 'image/jpg':
        case 'image/jpeg':
            // create a jpeg extension
            $this->image = imagecreatefromjpeg($filename);
            break;

        // Image is a GIF
        case 'image/gif':
            $this->image = @imagecreatefromgif($filename);
            break;

        // Image is a PNG
        case 'image/png':
            $this->image = @imagecreatefrompng($filename);
            break;

        // Mime type not found
        default:
            throw new Exception("File is not an image, please use another file type.", 1);
    }

    $this->origWidth = imagesx($this->image);
    $this->origHeight = imagesy($this->image);
}

/**
 * Save the image as the image type the original image was
 *
 * @param  String[type] $savePath     - The path to store the new image
 * @param  string $imageQuality       - The qulaity level of image to create
 *
 * @return Saves the image
 */
public function saveImage($savePath, $imageQuality="100", $download = false)
{
    switch($this->ex)
    {
        case 'image/jpg':
        case 'image/jpeg':
            // Check PHP supports this file type
            if (imagetypes() & IMG_JPG) {
                imagejpeg($this->newImage, $savePath, $imageQuality);
            }
            break;

        case 'image/gif':
            // Check PHP supports this file type
            if (imagetypes() & IMG_GIF) {
                imagegif($this->newImage, $savePath);
            }
            break;

        case 'image/png':
            $invertScaleQuality = 9 - round(($imageQuality/100) * 9);

            // Check PHP supports this file type
            if (imagetypes() & IMG_PNG) {
                imagepng($this->newImage, $savePath, $invertScaleQuality);
            }
            break;
    }

    if($download)
    {
        header('Content-Description: File Transfer');
        header("Content-type: application/octet-stream");
        header("Content-disposition: attachment; filename= ".$savePath."");
        readfile($savePath);
    }

    imagedestroy($this->newImage);
}

/**
 * Resize the image to these set dimensions
 *
 * @param  int $width           - Max width of the image
 * @param  int $height          - Max height of the image
 * @param  string $resizeOption - Scale option for the image
 *
 * @return Save new image
 */
public function resizeTo( $width, $height, $resizeOption = 'default' )
{
    switch(strtolower($resizeOption))
    {
        case 'exact':
            $this->resizeWidth = $width;
            $this->resizeHeight = $height;
        break;

        case 'maxwidth':
            $this->resizeWidth  = $width;
            $this->resizeHeight = $this->resizeHeightByWidth($width);
        break;

        case 'maxheight':
            $this->resizeWidth  = $this->resizeWidthByHeight($height);
            $this->resizeHeight = $height;
        break;

        default:
            if($this->origWidth > $width || $this->origHeight > $height)
            {
                if ( $this->origWidth > $this->origHeight ) {
                     $this->resizeHeight = $this->resizeHeightByWidth($width);
                     $this->resizeWidth  = $width;
                } else if( $this->origWidth < $this->origHeight ) {
                    $this->resizeWidth  = $this->resizeWidthByHeight($height);
                    $this->resizeHeight = $height;
                }  else {
                    $this->resizeWidth = $width;
                    $this->resizeHeight = $height;  
                }
            } else {
                $this->resizeWidth = $width;
                $this->resizeHeight = $height;
            }
        break;
    }

    $this->newImage = imagecreatetruecolor($this->resizeWidth, $this->resizeHeight);
    imagecopyresampled($this->newImage, $this->image, 0, 0, 0, 0, $this->resizeWidth, $this->resizeHeight, $this->origWidth, $this->origHeight);
}

/**
 * Get the resized height from the width keeping the aspect ratio
 *
 * @param  int $width - Max image width
 *
 * @return Height keeping aspect ratio
 */
private function resizeHeightByWidth($width)
{
    return floor(($this->origHeight/$this->origWidth)*$width);
}

/**
 * Get the resized width from the height keeping the aspect ratio
 *
 * @param  int $height - Max image height
 *
 * @return Width keeping aspect ratio
 */
private function resizeWidthByHeight($height)
{
    return floor(($this->origWidth/$this->origHeight)*$height);
}

}

0 个答案:

没有答案