前几天在找缩略图算法的时候发现网上的算法和我想的不太一样
这个是原帖
http://www.open-open.com/lib/view/open1337330620354.html这个是我写的
当然,某些部分是参考上面的一篇帖子的 public function createThumbImage($SourcePath, $SavePath)
{
if(!is_file($SourcePath))
{return false;}
$FilePath = pathinfo($SourcePath);
$FileExt = $FilePath['extension'];
if(!$this->checkFileExt($FileExt))
{return false;}
$createfun = 'imagecreatefrom' . ($FileExt == 'jpg' ? 'jpeg' : strtolower($FileExt));
$ImgInfo = getimagesize($SourcePath);
$SourceW = floatval($ImgInfo[0]);
$SourceH = floatval($ImgInfo[1]);
$NewW = $TargetW = floatval($this->DefaultWidth);
$NewH = $TargetH = floatval($this->DefaultHeight);
(($SourceW / $SourceH) <= ($TargetW / $TargetH)) ? $NewH = $SourceH * ($TargetW / $SourceW) : $NewW = $SourceW * ($TargetH / $SourceH);
$x = ($TargetW - $NewW) / 2;
$y = ($TargetH - $NewH) / 2;
$SourceImg = $createfun($SourcePath);
$TargetImg = imagecreatetruecolor($TargetW, $TargetH);
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($TargetImg, $SourceImg, $x, $y, 0, 0, $NewW, $NewH, $SourceW, $SourceH);
}
else
{
imagecopyresized($TargetImg, $SourceImg, $x, $y, 0, 0, $NewW, $NewH, $SourceW, $SourceH);
}
return $this->saveImageByExt($TargetImg, $FileExt, $SavePath);
}关键实现的是这几行 $NewW = $TargetW = floatval($this->DefaultWidth);
$NewH = $TargetH = floatval($this->DefaultHeight);
(($SourceW / $SourceH) <= ($TargetW / $TargetH)) ? $NewH = $SourceH * ($TargetW / $SourceW) : $NewW = $SourceW * ($TargetH / $SourceH);我想请教下这样实现有什么弊端么?
这个是原帖
http://www.open-open.com/lib/view/open1337330620354.html这个是我写的
当然,某些部分是参考上面的一篇帖子的 public function createThumbImage($SourcePath, $SavePath)
{
if(!is_file($SourcePath))
{return false;}
$FilePath = pathinfo($SourcePath);
$FileExt = $FilePath['extension'];
if(!$this->checkFileExt($FileExt))
{return false;}
$createfun = 'imagecreatefrom' . ($FileExt == 'jpg' ? 'jpeg' : strtolower($FileExt));
$ImgInfo = getimagesize($SourcePath);
$SourceW = floatval($ImgInfo[0]);
$SourceH = floatval($ImgInfo[1]);
$NewW = $TargetW = floatval($this->DefaultWidth);
$NewH = $TargetH = floatval($this->DefaultHeight);
(($SourceW / $SourceH) <= ($TargetW / $TargetH)) ? $NewH = $SourceH * ($TargetW / $SourceW) : $NewW = $SourceW * ($TargetH / $SourceH);
$x = ($TargetW - $NewW) / 2;
$y = ($TargetH - $NewH) / 2;
$SourceImg = $createfun($SourcePath);
$TargetImg = imagecreatetruecolor($TargetW, $TargetH);
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($TargetImg, $SourceImg, $x, $y, 0, 0, $NewW, $NewH, $SourceW, $SourceH);
}
else
{
imagecopyresized($TargetImg, $SourceImg, $x, $y, 0, 0, $NewW, $NewH, $SourceW, $SourceH);
}
return $this->saveImageByExt($TargetImg, $FileExt, $SavePath);
}关键实现的是这几行 $NewW = $TargetW = floatval($this->DefaultWidth);
$NewH = $TargetH = floatval($this->DefaultHeight);
(($SourceW / $SourceH) <= ($TargetW / $TargetH)) ? $NewH = $SourceH * ($TargetW / $SourceW) : $NewW = $SourceW * ($TargetH / $SourceH);我想请教下这样实现有什么弊端么?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货