我编写了一个经过测试的工作代码,用于减少图像大小,当图像上传时从一个文件夹复制到另一个文件夹。它将图像大小减小为宽度300,高度300。
我还想将相同的图像大小减少到宽度100和高度100,所以我可以使用最小的图像作为个人资料图片和电子邮件收件箱的图像。而另一个在用户相册上的图像为300,高度为300。
我想上传的图像复制到宽度300和高度300和100和高度100的2个不同的大小。
请帮帮我。
if(move_uploaded_file($this->tem_path, $terger_path)){
$exe = explode(".", $this->filename);
$ext = $exe[1];
$w = 300;
$h = 300;
$target = SITE_ROOT.DS.$this->img_path.DS.$this->filename;
$newcopy = SITE_ROOT.DS.$this->resize.DS.$this->filename;
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if(@imagejpeg($tci, $newcopy, 80)){
// return true;
$terger_path = SITE_ROOT.DS.$this->img_path.DS.$this->filename;
return unlink($terger_path) ? true : false;
}
}
这很简单:
- 第一次调整大小,复制到第一个位置。请勿删除上传的文件。第二次调整大小,复制到其他位置。
- 完成清洁(如有必要)