さまざまな画像を生成する動的クラスを作成しています。
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です。
私はそれを解決する方法がわかりません... 私のプロポーション()関数は、写真の縦横比から新しい高さと幅を返します...私がチェックしたところ、値は正しく、返された幅は300です(そして最終的な画像の幅は 300 です...ただし、黒い領域をカウントします)。