2010-10-03 70 views
0

我正在創建一個動態類來生成各種圖像。使用PHP和GD2調整圖像大小。我得到的圖像和黑色背景

ImagesUtils.php

<?php 
class ImagesUtils 
{ 
    private $nombre_imagen = null; 
    private $imagen = null; 
    private $extension = null; 
    private $directorio = null; 

    private $width = null; 
    private $height = null; 
    private $tipo = null; 

    private $final_width = null; 
    private $final_height = null; 
    private $nuevo_nombre = null; 
    private $nuevo_directorio = null; 

    public function __construct($imagen, $directorio = '') 
    { 
     $this->directorio = realpath("..".DS."data".DS."storage".DS."files".DS.$directorio); 

     $this->imagen = $this->directorio.DS.$imagen; 
     $this->nombre_imagen = $imagen; 

     $this->extension = substr($imagen, strrpos($imagen, '.') + 1, strlen($imagen)); 

     $propiedades = getimagesize($this->imagen); 

     $this->width = $propiedades["0"]; 
     $this->height = $propiedades["1"]; 
     $this->tipo = $propiedades["2"]; 
    } 

    public function Resize($width = null, $height = null, $proporcion = true) 
    { 
     $this->final_width = $width; 
     $this->final_height = $height; 

     if(true == $proporcion) 
      self::proporcion($width, $height); 

     $imagen = imagecreatefromjpeg($this->imagen); 

     $nueva_imagen = imagecreatetruecolor($this->final_width, $this->final_height); 

     imagecopyresampled($nueva_imagen, $imagen, 0, 0, 0, 0, $this->final_width, $this->final_height, $this->width, $this->height); 

     return imagejpeg($image, $this->nueva_imagen); 
    } 
} 
?> 

我怎麼稱呼:

$procesar_imagen = new ImagesUtils($imagen["nombre"]); 
$procesar_imagen->Resize(640, 480); 

寬度此代碼工作正常...但如果我用這個:

$procesar_imagen->Resize(300, 300); 

我生成的最終圖像看起來像:http://i51.tinypic.com/htwx79.jpg

輸入圖像是:http://i51.tinypic.com/15n9ifc.jpg

我不知道如何解決它...我proporcion()函數返回從照片的縱橫比的新高度和寬度。我檢查,值是正確的,返回的寬度是300(最終的圖像寬度是300 ...但計數黑色區域)。

回答