如何为上传的图像PHP设置宽度+高度


How to set width+height to uploaded image PHP

我的目标是保存上传的图像并为其设置宽度+高度。使用下面的代码,我可以保存图像,但无法设置宽度+高度...请查看//和//之间的代码以发现错误。多谢。

$format = $_FILES["picture"]["type"];
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" .time(). $_FILES["picture"]["name"]; 
 ////////////////////////////////////////////
$_destination = width [400];
$_destination = height [460]; // this doesn't work... please help:) 
  //////////////////////////////////////////
  move_uploaded_file($filename, $destination); //moves image to "upload" folder
 $_SESSION['picture'] = $destination; //saves image path... 
 $_SESSION['format'] = $format; //saves image format

如果在服务器上只安装了GD,则可以使用以下功能:

//helper function to resize an image based on input, output and size
function ResizeImage($input, $output, $mode, $w, $h = 0)
{
    switch(GetMimeType($input))
    {
        case "image/png":
            $img = ImageCreateFromPng($input);
            break;
        case "image/gif":
            $img = ImageCreateFromGif($input);
            break;
        case "image/jpeg":
        default:
            $img = ImageCreateFromJPEG ($input);
            break;
    }
    $image['sizeX'] = imagesx($img);
    $image['sizeY'] = imagesy($img);
    switch ($mode){
    case 1: //Quadratic Image
        $thumb = imagecreatetruecolor($w,$w);
        if($image['sizeX'] > $image['sizeY'])
        {
            imagecopyresampled($thumb, $img, 0,0, ($w / $image['sizeY'] * $image['sizeX'] / 2 - $w / 2),0, $w,$w, $image['sizeY'],$image['sizeY']);
        }
        else
        {
            imagecopyresampled($thumb, $img, 0,0, 0,($w / $image['sizeX'] * $image['sizeY'] / 2 - $w / 2), $w,$w, $image['sizeX'],$image['sizeX']);
        }
        break;
    case 2: //Biggest side given
        if($image['sizeX'] > $image['sizeY'])
        {
            $thumb = imagecreatetruecolor($w, $w / $image['sizeX'] * $image['sizeY']);
            imagecopyresampled($thumb, $img, 0,0, 0,0, imagesx($thumb),imagesy($thumb), $image['sizeX'],$image['sizeY']);
        }
        else
        {
            $thumb = imagecreatetruecolor($w / $image['sizeY'] * $image['sizeX'],$w);
            imagecopyresampled($thumb, $img, 0,0, 0,0, imagesx($thumb),imagesy($thumb), $image['sizeX'],$image['sizeY']);
        }
        break;
    case 3; //Both sides given (cropping)
        $thumb = imagecreatetruecolor($w,$h);
        if($h / $w > $image['sizeY'] / $image['sizeX'])
        {
            imagecopyresampled($thumb, $img, 0,0, ($image['sizeX']-$w / $h * $image['sizeY'])/2,0, $w,$h, $w / $h * $image['sizeY'],$image['sizeY']);
        }
        else
        {
            imagecopyresampled($thumb, $img, 0,0, 0,($image['sizeY']-$h / $w * $image['sizeX'])/2, $w,$h, $image['sizeX'],$h / $w * $image['sizeX']);
        }
        break;
    case 0:
        $thumb = imagecreatetruecolor($w,$w / $image['sizeX']*$image['sizeY']);
        imagecopyresampled($thumb, $img, 0,0, 0,0, $w,$w / $image['sizeX']*$image['sizeY'], $image['sizeX'],$image['sizeY']);
        break;
}        
    if(!file_exists($output)) imagejpeg($thumb, $output, 90);
}

//helper function to get the mime type of a file
function GetMimeType($file)
{
    $forbiddenChars = array('?', '*', ':', '|', ';', '<', '>');
    if(strlen(str_replace($forbiddenChars, '', $file)) < strlen($file))
        throw new 'ArgumentException("Forbidden characters!");
    $file = escapeshellarg($file);
    ob_start();
    $type = system("file --mime-type -b ".$file);
    ob_clean();
    return $type;
}

然后在代码中使用它:

//$filename: input filename
//$destination: output filename
//$mode: 1, quadratic / 2, proportional / 3, cropped
//$width: output image width
//$height: optional output image height, only needed for mode 3
ResizeImage($filename, $destination, $mode, $width, $height);

编辑:关于函数的一些解释。

<?php
function DisplayText($text)
{
   echo $text;
}
function MultiplyNumbers($a, $b)
{
   return $a*$b;
}
$test = MultiplyNumbers(3, 6);
DisplayText($test);

->输出:18

Imagick 将完全满足您的需求:http://php.net/manual/en/imagick.setsize.php

在读取图像文件之前使用 setSize 告诉 ImageMagick 在加载时立即调整图像大小 - 这可以大大增加性能时间并节省大图像的内存和磁盘资源:

<?php
$image = new Imagick();
$image->setSize(800,600);
$image->readImage($file);
?>