0

次の方法を使用して画像のサイズを変更します。

 private function newDim($maxHeight, $maxWidth, $height, $width) {

        if(($width / $height) >= ($maxWidth / $maxHeight)){
                $nw = $maxWidth;
                $nh = $height * ($maxWidth / $width);
                $nx = round(abs($maxHeight - $nh) / 2);
                $ny = 0 ;
        }else {
            // by height
                $nw = $width*($maxHeight/$height);
                $nh = $maxHeight;
                $nx = 0;
                $ny = round(abs($maxWidth - $nw) / 2); ;
        }

        return array($nw,$nh,$nx,$ny);
    }

その後、この画像を保存したいと思います。この場合、私はこの方法を使用します:

 public function upload($file_param_name) {
        $file_name = $_FILES[$file_param_name]['name'];
        $source_file_path = $_FILES[$file_param_name]['tmp_name'];
        $$this->ext = pathinfo($file_name, PATHINFO_EXTENSION);
        $ext = 'jpg';
        $this->ext = $ext;
        $ext =strtolower($ext);
        $new_file_name = $file_name."-".md5(uniqid(rand(), true));
        $this->image_hashname = $new_file_name;
        $target_path = $this->store_folder.basename($new_file_name);

        list($width,$height,$type)=getimagesize($source_file_path);

        switch ($type) {
            case 1:   //   gif -> jpg
                $src = imagecreatefromgif($source_file_path);
                break;
            case 2:   //   jpeg -> jpg
                $src = imagecreatefromjpeg($source_file_path);
                break;
            case 3:  //   png -> jpg
                $src = imagecreatefrompng($source_file_path);
                break;
        }

        $th = 240;
        $tw = 240;

        list($nh,$nw,$nx,$ny) = $this->newDim($th, $tw, $height, $width);

        $tmp=imagecreatetruecolor($tw,$th);

        $white = imagecolorallocate($tmp, 255,255,255);
        imagefill($tmp, 0, 0, $white);

        imagecopyresized($tmp,$src,$ny,$nx,0,0,$nh,$nw,$width,$height);
        $thmbfile = $this->store_folder.basename($new_file_name."-thumb.".$ext);
        imagejpeg($tmp,$thmbfile,100);


        imagedestroy($src);
        imagedestroy($tmp);
    }

300x300から240x240のサイズで画像を保存すると、新しい画像の品質が非常に悪くなります。どこが間違っているの?

画像 :

旧:http ://cbn-design.eu/imgs/original.jpg

NEW:http ://cbn-design.eu/imgs/new.jpg

4

1 に答える 1

5

この回答に従って、imagecopyresampled()の代わりにを使用してみてくださいimagecopyresized()

于 2012-09-03T13:11:50.307 に答える