2017-06-24 2 views
1

J'utilise img uploader sur mon ftp. Si l'image est grande à 1024x768, j'utilise un script pour redimensionner. Mais après redimensionnement perdre la qualité.Redimensionner l'image sans perte de qualité lorsque l'image est plus grande

code de 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)); 
       } 

Code de bal 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; 
} 

Il est impossible de réduire la taille sans perte de qualité :(

+0

Le troisième paramètre dans 'image {type}()' est la qualité ... http: //php.net/manual/fr/function.imagejpeg.php – Rasclatt

Répondre

0

Depuis l'API par défaut peut ne pas être vraiment efficace quand il s'agit de la manipulation d'image, je vous suggère d'utiliser une bibliothèque.Voyez grafika

0

Vous pouvez également utiliser un service externe pour le redimensionnement des images tel que ce repo Flyimg. J'ai trouvé le script pour redimensionner, fonctionnant correctement.

1

Je pense que c'est ce que je dois :)

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

Mais, un problème et s'il vous plaît me corriger: Si besoin redimensionner PNG avec fond transparent ce script supprimer transparent et ajouter fond noir.

Dans commentaires ci-dessous script modifié, mais avec ce code ne fonctionne pas la fonction compress

J'ai besoin de modifier ce code pour redimensionner PNG sans supprimer fond transparent:

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

Merci