buenas a todos. estuve investigando en foros del web y vi ejemplos de como se redimensionar imagenes. consegui una aplicacion para que redimensione una imagen. pero tengo unas dudas que quiero que me respondan por favor.
supongamos que tengo una imagen de 1280 x 1240 px y quiero redimensionarlo a 500 x 400 px. subo el archivo y el codigo que hice me lo redimensiona. pero me di con la sorpresa que cuando lo he asignado los tamaños correspondientes, los redimensiona a tamaños distintos, es decir, no lo redimensiona a los tamaños que le asignado.
esta es la clase para la redimension de las imagenes. lo saque de la pagina de phpclasses.org.
Código PHP:
Ver original<?php
##############################################
# Shiege Iseng Resize Class
# 11 March 2003
# shiegege_at_yahoo.com
# View Demo :
# http://shiege.com/scripts/thumbnail/
/*############################################
Sample :
$thumb=new thumbnail("./shiegege.jpg"); // generate image_file, set filename to resize
$thumb->size_width(100); // set width for thumbnail, or
$thumb->size_height(300); // set height for thumbnail, or
$thumb->size_auto(200); // set the biggest width or height for thumbnail
$thumb->jpeg_quality(75); // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
$thumb->show(); // show your thumbnail
$thumb->save("./huhu.jpg"); // save your thumbnail to file
----------------------------------------------
Note :
- GD must Enabled
- Autodetect file extension (.jpg/jpeg, .png, .gif, .wbmp)
but some server can't generate .gif / .wbmp file types
- If your GD not support 'ImageCreateTrueColor' function,
change one line from 'ImageCreateTrueColor' to 'ImageCreate'
(the position in 'show' and 'save' function)
*/############################################
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";
} elseif ($this->img["format"]=="PNG") {
//PNG
$this->img["format"]="PNG";
} elseif ($this->img["format"]=="GIF") {
//GIF
$this->img["format"]="GIF";
} elseif ($this->img["format"]=="WBMP") {
//WBMP
$this->img["format"]="WBMP";
} else {
//DEFAULT
echo "Not Supported File";
}
@$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*/
@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
} elseif ($this->img["format"]=="GIF") {
//GIF
} elseif ($this->img["format"]=="WBMP") {
//WBMP
}
}
function save($save="")
{
//save thumb
/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
@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
} elseif ($this->img["format"]=="GIF") {
//GIF
} elseif ($this->img["format"]=="WBMP") {
//WBMP
}
}
}
?>
el codigo que redimensiona la imagen.
Código PHP:
Ver original<?php
include("includes/resize.php");
$imagen = $_FILES['imagen']['name'];
$uploadtempname = $_FILES['imagen']['tmp_name'];
#video de la noticia.
$tamano = $_FILES['imagen']['size']; // Leemos el tamaño del fichero
$tamano_max = "4194304"; // Tamaño maximo permitido
if($tamano <= $tamano_max){ // Comprovamos el tamaño*/
$path = "imagenes/";
$path_thumbnail = "thumbs/";
$thumb=new thumbnail($path.$nuevo_imagen); // prepare to generate "shiegege.jpg" in directory "/www/imagenes"
$thumb->size_width(500); // set width for thumbnail with 500 pixels
$thumb->size_height(400); // set the biggest width or height for thumbnail
$thumb->jpeg_quality(75); // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
$thumb->save($path_thumbnail."thb_".$nuevo_imagen); // save my thumbnail to file "huhu.jpg" in directory "/www/thumbs
echo "El archivo ".$nuevo_archivo." se ha subido corectamente.";
}else{
echo "<span align = 'center'>El archivo que intenta grabar, excede el peso requerido. El máximo de peso es 4MB.</span>";
echo "<br /><br />";
echo "<a href='javascript:history.back()'>Clic aqui para regresar </a>";
}
}else{
echo "<span align = 'center'>El archivo que intenta grabar, debe ser de formato jpg, jpeg, gif y png</span>";
echo "<br /><br />";
echo "<a href='javascript:history.back()'>Clic aqui para regresar </a>";
}
?>