适当的图像比例缩放

时间:2013-02-24 23:16:23

标签: php image image-processing

我有一个脚本,我用它来缩放成员个人资料图像,我想知道什么是正确的方式来执行缩放图片的图片,而不会过多地扭曲图像原生比率。上传时保持图像宽高比的原理是什么?目前我使用这段代码来执行缩放

//SCALING IMAGES 
    $newwidth = 301;
    $newheight = ceil(($height/$width)*$newwidth);
    $tmp = imagecreatetruecolor($newwidth,$newheight);


    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您应该设置示例来设置图像重新采样大小:

class imagemanipulate
{
public function set_size($max_x = 100,$max_y = 100)
{

    // Resize
    if($this->x_input > $max_x || $this->y_input > $max_y)
    {

    $a= $max_x / $max_y;
    $b= $this->x_input / $this->y_input;

    if ($a<$b)
    {

        $this->x_output = $max_x;
        $this->y_output = ($max_x / $this->x_input) * $this->y_input;

    }
    else
    {

        $this->y_output = $max_y;
        $this->x_output = ($max_y / $this->y_input) * $this->x_input;

    }
    // Ready

    $this->resize = TRUE;

    }

    // Don't resize       
    else { $this->resize = FALSE; }

}
 }


 $inst = new imagemanipulate;
 $inst->set_size('301', '301');

 $tmp = imagecreatetruecolor($inst->x_output,$inst->y_output);


 imagecopyresampled($tmp,$src,0,0,0,0,$inst->x_output,$inst->y_output,$width,$height);

?>