将图像复制到许多文件夹

时间:2013-04-14 01:04:59

标签: php

我写了一个经过测试的工作代码,用于缩小图像大小,从图像上传到另一个文件夹。它将图像大小减小到300和300的宽度。

我还希望将相同的图像尺寸缩小到100和100的宽度,这样我就可以将最小的图像用作个人资料图片和电子邮件收件箱图像。另一个用户专辑中300和300的图像。

我希望上传的图像复制到2个不同大小的宽度300和高度300以及100和高度100。

请帮帮我。

            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;                     
                }                   

            }

1 个答案:

答案 0 :(得分:0)

嗯,这很简单:

  1. 首先调整大小,将其复制到第一个位置。不要删除上传的文件。
  2. 第二次调整大小,复制到其他位置。
  3. 完成清洁(如有必要)
相关问题