2017-06-24 14 views
1

私は自分のftpでimg uploaderを使用します。 画像サイズが1024x768に大きい場合は、サイズ変更用のスクリプトを使用します。 サイズ変更後は品質が低下します。 uploader.phpから画像が大きくなると画像が失われることなくサイズ変更

コード:

   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], "$upload_folder/" . $newname)) 
       if($width>1024 || $height>768) { 
        require './image_resize.php'; 
        echo (image_resize("$upload_folder/" . $newname, "$upload_folder/" . $newname, 1024, 768)); 
       } 

コードPROMのimage_resize.php:

<?php ini_set('memory_limit','500M'); 
function image_resize($src, $dst, $width, $height, $crop=0){ 

    if(!($pic = @getimagesize($src))) 
     return false; 

    $w = $pic[0]; 
    $h = $pic[1]; 
    $type = substr($pic['mime'], 6); 

    $func = 'imagecreatefrom' . $type; 

    if(!function_exists($func)) 
     return false; 

    $img = $func($src); 

    if($crop){ 

      if($w < $width && $h < $height) 
       return false; 

      $ratio = max($width/$w, $height/$h); 
      $h = $height/$ratio; 
      $x = ($w - $width/$ratio)/2; 
      $w = $width/$ratio; 
    } 
    else{ 

      if($w < $width && $h < $height) 
       return false; 

      $ratio = min($width/$w, $height/$h); 
      $width = $w * $ratio; 
      $height = $h * $ratio; 
      $x = 0; 
    } 

    $new = imagecreatetruecolor($width, $height); 

    if($type == "gif" || $type == "png"){ 
     imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); 
     imagealphablending($new, false); 
     imagesavealpha($new, true); 
    } 
    imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h); 

    $save = 'image' . $type; 

    $save($new, $dst); 
    return true; 
} 

デフォルトのAPIので品質:(

+0

三番目のパラメータありがとう'image {type}()'の品質は... http://php.net/manual/en/function.imagejpeg.php – Rasclatt

答えて

0

を失うことなく、サイズを小さくすることは不可能です画像処理に関しては本当に効果的ではないかもしれませんが、ライブラリを使用することをお勧めします。grafika

0

また、このレポ Flyimgなどの画像のサイズ変更に外部サービスを使用することもできます。

1

サイズ変更用のスクリプトが見つかりました。私はこれは私が:)

http://sanchiz.net/blog/resizing-images-with-php

しかし、一つの問題に必要なものだと思うし、私のために修正してください:このスクリプトは透明を削除し、黒の背景を追加し、透明な背景をPNGのサイズを変更する必要がある場合。

変更したスクリプトを投稿されたコメントで

が、このコードは圧縮機能に働いていないと

私は透明な背景を削除することなく、PNGサイズ変更のために、このコードを変更する必要があります。

class SimpleImage { 
public $image; 
public $image_type; 
public function load($filename) { 
    $image_info  = getimagesize($filename); 
    $this->image_type = $image_info[2]; 
    if ($this->image_type == IMAGETYPE_JPEG) { 
     $this->image = imagecreatefromjpeg($filename); 
    } elseif ($this->image_type == IMAGETYPE_GIF) { 
     $this->image = imagecreatefromgif($filename); 
    } elseif ($this->image_type == IMAGETYPE_PNG) { 
     $this->image = imagecreatefrompng($filename); 
    } 
} 
public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 100, $permissions = NULL) { 
    if ($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image, $filename, $compression); 
    } elseif ($image_type == IMAGETYPE_GIF) { 
     imagegif($this->image, $filename); 
    } elseif ($image_type == IMAGETYPE_PNG) { 
     imagepng($this->image, $filename); 
    } 
    if ($permissions != NULL) { 
     chmod($filename, $permissions); 
    } 
} 
public function output($image_type = IMAGETYPE_JPEG) { 
    if ($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image); 
    } elseif ($image_type == IMAGETYPE_GIF) { 
     imagegif($this->image); 
    } elseif ($image_type == IMAGETYPE_PNG) { 
     imagepng($this->image); 
    } 
} 
public function resizeToHeight($height) { 
    $ratio = $height/$this->getHeight(); 
    $width = $this->getWidth() * $ratio; 
    $this->resize($width, $height); 
} 
public function getHeight() { 
    return imagesy($this->image); 
} 
public function getWidth() { 
    return imagesx($this->image); 
} 
public function resize($width, $height) { 
    $new_image = imagecreatetruecolor($width, $height); 
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
    $this->image = $new_image; 
} 
public function resizeToWidth($width) { 
    $ratio = $width/$this->getWidth(); 
    $height = $this->getheight() * $ratio; 
    $this->resize($width, $height); 
} 
public function scale($scale) { 
    $width = $this->getWidth() * $scale/100; 
    $height = $this->getheight() * $scale/100; 
    $this->resize($width, $height); 
} 

関連する問題