Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php调整宽度固定_Php_Image_Function_Resize - Fatal编程技术网

Php调整宽度固定

Php调整宽度固定,php,image,function,resize,Php,Image,Function,Resize,我在PHP中有这个函数 <?php function zmensi_obrazok($max_dimension, $image_max_width, $image_max_height, $dir, $obrazok, $obrazok_tmp, $obrazok_size, $filename){ $postvars = array( "image" => $obrazok, "image_tmp" => $obra

我在PHP中有这个函数

<?php
function zmensi_obrazok($max_dimension, $image_max_width, $image_max_height, $dir, $obrazok, $obrazok_tmp, $obrazok_size, $filename){


$postvars          = array(
"image"            => $obrazok,
"image_tmp"        => $obrazok_tmp,
"image_size"       => $obrazok_size,
"image_max_width"  => $image_max_width,
"image_max_height" => $image_max_height
);
// Array of valid extensions.
$valid_exts = array("jpg","jpeg","gif","png");
// Select the extension from the file.
$ext = end(explode(".",strtolower($obrazok)));
// Check not larger than 175kb.
if($postvars["image_size"] <= 256000){
// Check is valid extension.
if(in_array($ext,$valid_exts)){
if($ext == "jpg" || $ext == "jpeg"){
$image = imagecreatefromjpeg($postvars["image_tmp"]);
}
else if($ext == "gif"){
$image = imagecreatefromgif($postvars["image_tmp"]);
}
else if($ext == "png"){
$image = imagecreatefrompng($postvars["image_tmp"]);
}
list($width,$height) = getimagesize($postvars["image_tmp"]);

if($postvars["image_max_width"] > $postvars["image_max_height"]){
    if($postvars["image_max_width"] > $max_dimension){
            $newwidth = $max_dimension;
        } 
        else 
        {
            $newwidth = $postvars["image_max_width"];
        }

}

else
{
    if($postvars["image_max_height"] > $max_dimension)
    {
        $newheight = $max_dimension;
    }
        else
        {
            $newheight = $postvars["image_max_height"];
        }


}

$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);

imagejpeg($tmp,$filename,100);
return "fix";
imagedestroy($image);
imagedestroy($tmp);
}

}

}

?>


现在,如果我想使用它,我上传图像,例如500x300px,我已经将最大大小设置为205x205px,它不想调整图片的大小比例。它的形状类似于375x205(高度仍然可以)。谁能帮我修一下吗

只需缩放图像两次,一次与宽度匹配,一次与高度匹配。要节省处理时间,请先进行缩放,然后进行大小调整:

$max_w = 205;
$max_h = 205;

$img_w = ...;
$img_h = ...;

if ($img_w > $max_w) {
    $img_h = $img_h * $max_w / $img_w;
    $img_w = $max_w;
}

if ($img_h > $max_h) {
    $img_w = $img_w * $max_h / $img_h;
    $img_h = $max_h;
}

// $img_w and $img_h should now have your scaled down image complying with both restrictions.

我现在有了这样的版本,如果($postvars[“image\u max\u width”]>$max\u dimension){$newheight=$postvars[“image\u max\u height”]*$max\u dimension/$postvars[“image\u max\u width”];$newwidth=$max\u dimension;}如果($postvars[“image\u max\u height”]>$max\u dimension){$newwidth=$postvars[“image\u width”]*$max\u dimension/$postvars[“image\u max\u height”];$newheight=$max\u dimension;}