2017-06-24 552 views
1

我在我的ftp上使用img上傳器。 如果圖像大到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)); 
       } 

代碼舞會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; 
} 

這是不可能減少的大小而不失品位:(

+0

第三個參數在'圖像{類型}()'是質量... http://php.net/manual/en/function.imagejpeg.php – Rasclatt

回答

0

由於默認API可能不是,當涉及到圖像處理真的有效,我建議你使用一個圖書館。看看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); 
} 

謝謝

相關問題