2013-09-22 43 views
-1

哪种方式是更好调整大小图片?哪种方式更好地调整图像大小?

现在我使用缩略图类,它工作正常,但它是减少图像方式质量太多。

我想在大拇指大小的形式轮廓影像 = 32×32

我使用的是现在的代码是:

$cimg = $_FILES["image_src"]["name"]; 

move_uploaded_file($_FILES["image_src"]["tmp_name"],"uploads/profile_images/".$cimg); 

$thumb=new thumbnail("uploads/profile_images/".$cimg); 

$thumb->size_width(32); 

$thumb->save("uploads/profile_images/".$cimg); 

$thumb=new thumbnail("uploads/profile_images/".$cimg); 

$thumb->size_width(32); 


$thumb->save("uploads/profile_images/thumbs/".$cimg); 

请给我建议一些更好的方法来做到这一点。我的图片过于紧张,质量过低。

我的缩略图类别:

class thumbnail 

{ 

    var $img; 



    function thumbnail($imgfile) 

    { 

     //detect image format 

     $this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile); 

     $this->img["format"]=strtoupper($this->img["format"]); 

     if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") { 

      //JPEG 

      $this->img["format"]="JPEG"; 

      $this->img["src"] = ImageCreateFromJPEG ($imgfile); 

     } elseif ($this->img["format"]=="PNG") { 

      //PNG 

      $this->img["format"]="PNG"; 

      $this->img["src"] = ImageCreateFromPNG ($imgfile); 

     } elseif ($this->img["format"]=="GIF") { 

      //GIF 

      $this->img["format"]="GIF"; 

      $this->img["src"] = ImageCreateFromGIF ($imgfile); 

     } elseif ($this->img["format"]=="WBMP") { 

      //WBMP 

      $this->img["format"]="WBMP"; 

      $this->img["src"] = ImageCreateFromWBMP ($imgfile); 

     } else { 

      //DEFAULT 

      echo "Not Supported File"; 

      exit(); 

     } 

     @$this->img["lebar"] = imagesx($this->img["src"]); 

     @$this->img["tinggi"] = imagesy($this->img["src"]); 

     //default quality jpeg 

     $this->img["quality"]=75; 

    } 



    function size_height($size=100) 

    { 

     //height 

     $this->img["tinggi_thumb"]=$size; 

     @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"]; 

    } 



    function size_width($size=100) 

    { 

     //width 

     $this->img["lebar_thumb"]=$size; 

     @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"]; 

    } 



    function size_auto($size=100) 

    { 

     //size 

     if ($this->img["lebar"]>=$this->img["tinggi"]) { 

      $this->img["lebar_thumb"]=$size; 

      @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"]; 

     } else { 

      $this->img["tinggi_thumb"]=$size; 

      @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"]; 

     } 

    } 



    function jpeg_quality($quality=75) 

    { 

     //jpeg quality 

     $this->img["quality"]=$quality; 

    } 



    function show() 

    { 

     //show thumb 

     @Header("Content-Type: image/".$this->img["format"]); 



     /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/ 

     $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]); 

      @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]); 



     if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") { 

      //JPEG 

      imageJPEG($this->img["des"],"",$this->img["quality"]); 

     } elseif ($this->img["format"]=="PNG") { 

      //PNG 

      imagePNG($this->img["des"]); 

     } elseif ($this->img["format"]=="GIF") { 

      //GIF 

      imageGIF($this->img["des"]); 

     } elseif ($this->img["format"]=="WBMP") { 

      //WBMP 

      imageWBMP($this->img["des"]); 

     } 

    } 



    function save($save="") 

    { 

     //save thumb 

     if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]); 

     /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/ 

     $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]); 

      @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]); 



     if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") { 

      //JPEG 

      imageJPEG($this->img["des"],"$save",$this->img["quality"]); 

     } elseif ($this->img["format"]=="PNG") { 

      //PNG 

      imagePNG($this->img["des"],"$save"); 

     } elseif ($this->img["format"]=="GIF") { 

      //GIF 

      imageGIF($this->img["des"],"$save"); 

     } elseif ($this->img["format"]=="WBMP") { 

      //WBMP 

      imageWBMP($this->img["des"],"$save"); 

     } 

    } 

} 

?> 
+0

这是什么缩略图班?图书馆的一部分? ImageMagick的? –

+0

已更新。请检查 –

+0

我猜两者都是一样的 –

回答

0

这工作我使用imagecopyresampled的(注意,图像将始终保存使用.png格式,大小保持宽高比):

// thumbnail size 
$thumb_width = 32; $thumb_height = 32; 

// get image from post from 
$img = $_FILES['image_src']['tmp_name'];   

// thumbnail filename 
$dst = 'uploads/profile_images/thumbs/image_name.png'; 

// check valid image 
if (($img_info = getimagesize($img)) === FALSE) { die("Image not found or not an image"); } 

// check source image format and check valid image. get image in $src variable if valid image 
switch ($img_info[2]) { 
    case IMAGETYPE_GIF : $src = imagecreatefromgif($img); break; 
    case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break; 
    case IMAGETYPE_PNG : $src = imagecreatefrompng($img); break; 
    default : $src = false; 
} 
if(!$src) { die("Image not found or not an image"); } 

// get metrics to resize image to fit to thumbnail size 
$width = $img_info[0]; $height = $img_info[1]; 
$original_aspect = $width/$height; 
$thumb_aspect = $thumb_width/$thumb_height; 

if ($original_aspect >= $thumb_aspect) { 
    $new_height = $thumb_height; 
    $new_width = $width/($height/$thumb_height); 
} 
else { 
    $new_width = $thumb_width; 
    $new_height = $height/($width/$thumb_width); 
} 

// create thumbnail 
$tmp = imagecreatetruecolor($thumb_width, $thumb_height); 
imagecopyresampled($tmp, $src, 0 - ($new_width - $thumb_width)/2, 0 - ($new_height - $thumb_height)/2, 0, 0, $new_width, $new_height, $width, $height); 
imagepng($tmp, $dst); 
+0

你可以解释一下这段代码 –

+0

它不难理解(我认为),但我在上面的代码中添加了一些注释。只需阅读并从后期图像,计算正确的大小,以适应缩略图定义的大小,使用'imagecopyresampled'调整大小,并保存为PNG图像'iamgepng'。你不明白? – megatxi