Php调整宽度修复 [英] Php resize width fix

查看:127
本文介绍了Php调整宽度修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?php 
函数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
);
//有效扩展的数组。
$ valid_exts = array(jpg,jpeg,gif,png);
//从文件中选择扩展名。
$ ext = end(explode(。,strtolower($ obrazok)));
//检查不超过175kb。
if($ postvars [image_size]< = 256000){
// Check是有效的扩展名。 ($ ext ==jpg|| $ ext ==jpeg){
$ 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);
返回修复;
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和$ img_h现在应该已缩小的图像符合两个限制。


I have this function in 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);
}

}

}

?>

Now if I want to use it, and I upload image for example 500x300px and I have set max size to 205x205px it don't want to make resized picture proportion. It make something like 375x205 (height is still OK). Can somebody help how to fix it?

解决方案

Just scale your image twice, once to match the width, once to match the height. To save on processing, get your scaling first, then do the resizing:

$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.

这篇关于Php调整宽度修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆