php 图片等比例处理图片的时候有个问题一直不明白 就是 原图片有个宽高比 目标图片也有一个宽高比 为什么 原图的宽高比 大于目标图片的宽高比 要高度优先
$ratio = ($this->width)/($this->height); //实际图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);//改变后的图象的比例
if($ratio>$resize_ratio){
高度优先 //就是不明白 这里为什么是高度优先
}else{
宽度优先 //不明白为什么是宽度优先
} private $width;//实际宽度
private $height;//实际高度
private $resize_width;//改变后的宽度
private $resize_height;//改变后的高度
$ratio = ($this->width)/($this->height); //实际图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);//改变后的图象的比例
if($ratio>$resize_ratio){
高度优先 //就是不明白 这里为什么是高度优先
}else{
宽度优先 //不明白为什么是宽度优先
} private $width;//实际宽度
private $height;//实际高度
private $resize_width;//改变后的宽度
private $resize_height;//改变后的高度
$ratio = ($this->width)/($this->height); // 值为1
$resize_ratio = ($this->resize_width)/($this->resize_height);
若果 高度拉伸 了。$resize_ratio<1 = $ratio 就是高度优先。反之则宽度优先。
为什么高度拉伸了 就要高度优先
/***************************************/
/*功 能:利用PHP的GD库生成高质量的缩略图*/
/*运行环境:PHP5.01/GD2*/
/*类说明:可以选择是/否裁图。
如果裁图则生成的图的尺寸与您输入的一样。
原则:尽可能多保持原图完整
如果不裁图,则按照原图比例生成新图
原则:根据比例以输入的长或者宽为基准*/
/*参 数:$img:源图片地址
wid:新图的宽度
$hei:新图的高度
$c:是否裁图,1为是,0为否*/
/***************************************/
class Image{
private $type;//图片类型
private $width;//实际宽度
private $height;//实际高度
private $resize_width;//改变后的宽度
private $resize_height;//改变后的高度
private $cut;//是否裁图
private $srcimg;//源图象
private $dstimg; //目标图象地址
private $im;//临时建的图象
private static $_handle ;
function resizeimage($img, $wid, $hei,$c){
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
$this->type = substr(strrchr($this->srcimg,"."),1);//图片的类型
$this->initi_img();//初始化图象
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
$this->newimg();//生成图象
ImageDestroy($this->im);
}
function newimg() {
$resize_ratio = ($this->resize_width)/($this->resize_height);//改变后的图象的比例
$ratio = ($this->width)/($this->height); //实际图象的比例
if(($this->cut)=="1"){ //裁图
if($ratio>=$resize_ratio){ //高度优先
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio) { //宽度优先
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
ImageJpeg ($newimg,$this->dstimg);
}
} else { //不裁图
if($ratio>=$resize_ratio) {
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio) {
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
}
}
//初始化图象
function initi_img() {
if($this->type=="jpg") {
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif") {
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png") {
$this->im = imagecreatefrompng($this->srcimg);
}
}
/**
* Enter description here...
*
* @return Image
*/
public static function getInstance(){
if(is_object(self::$_handle)==false){
self::$_handle = new self();
}
return self::$_handle;
}
}
?>
你看看这个测试例$url = 'http://avatar.profile.csdn.net/9/D/6/2_dream1206.jpg';
$fn = 'dream1206.jpg';
$s = file_get_contents($url);
file_put_contents($fn, $s);//缓存在本地,一是为了防止网络问题造成图片错误,二是程序也只接受本地文件list($w, $h) = getimagesize($fn);//获取原始图片的宽和高$code = file_get_contents('images.php'); //因为后面要修改程序代码,所以要把你的类定义读进来if(1) //改变一下条件看看 1 强制高度优先
$code = str_replace(array('$ratio>=$resize_ratio', '$ratio<$resize_ratio'), array(1, 0), $code);
else //0 强制宽度优先
$code = str_replace(array('$ratio>=$resize_ratio', '$ratio<$resize_ratio'), array(0, 1), $code);eval("?>$code"); //加载被修改后的类$p = new image;
$p->resizeimage($fn, $h, $w, 1); //注意是“裁图”方式分别用 1 和 0 运行一下,就能理解了