How can I resize the image of a height, fixed width proportionally

0

I have this script to send images to the server, which adds a watermark to the image, but I would like to be able to resize an image giving it a fixed height and that the width adapts proportionally to that height, you could help me, from now on thank you very much, I hope answers, greetings to the community

    <?php

if(isset($_FILES['files']) && $_FILES['files']!='' ){

//Carpeta donde se guarda la marca de agua y las imagenes
$directorio='../upload/';

$nombre_nuevo = time().'.jpg';
$_FILES['files']['name'] = $nombre_nuevo;
//Guardar la imagen
move_uploaded_file($_FILES['files']['tmp_name'], $directorio.$nombre_nuevo  );

//Cargar la marca de agua
$estampa = imagecreatefrompng($directorio.'../upload/retrohits.png');


//Cargar ima imagen recien guardada (jpg y png)
if($_FILES['files']['type']=='image/jpg' or $_FILES['files']['type']=='image/jpeg'){
    $im = imagecreatefromjpeg($directorio.$_FILES['files']['name']);
} else if($_FILES['files']['type']=='image/gif'){
    $im = imagecreatefrompng($directorio.$_FILES['files']['name']);
}

//Establecer los márgenes para la estampa
$margen_dcho = 10;
$margen_inf = 10;
$sx = imagesx($estampa);
$sy = imagesy($estampa);

// Copiar la imagen de la estampa sobre nuestra foto usando los índices de márgen y el 
imagecopy($im, $estampa, imagesx($im) - $sx - $margen_dcho, imagesy($im) - $sy - $margen_inf, 0, 0, imagesx($estampa), imagesy($estampa));

//Remplazar la imagen con la marca de agua
if($_FILES['files']['type']=='image/jpg' or $_FILES['files']['type']=='image/jpeg'){
    imagejpeg($im,$directorio.$_FILES['files']['name']);
} else if($_FILES['files']['type']=='image/gif'){
    imagegif($im,$directorio.$_FILES['files']['name']);

}

}

?>
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="files">
    <input type="submit" value="subir">
</form>
    
asked by konchikuzo 09.11.2018 в 13:32
source

2 answers

0

The problem you have can be easily solved by means of CSS and HTML .

What you could do is "wrap" the images you want to resize with a div that contains them.

HTML

<div class="imagen">
  <img src="https://gnwebprensalibrerootwest.s3.us-west-2.amazonaws.com/mmediafiles/pl/e4/e45b9a86-25e4-4226-ae84-a72763aaf83a_749_499.jpg" />
</div>

That div is the one you use to restrict the height of the element to the desired pixels and make the contained image fit 100% of the height of your container and the width you let it be fit alone.

CSS

.imagen {
  height: 100px;
  position: relative;
}

.imagen img {
  width: auto;
  height: 100%;
}
    
answered by 09.11.2018 в 15:24
0

You can review this class ...

<?php

class redimensionar {

    private $imagen;
    private $ancho;
    private $alto;
    private $nueva_imagen;

    public function __construct ($nombre_archivo) {
        $this->imagen = $this->abrir_imagen($nombre_archivo);
        $this->ancho = imagesx($this->imagen);
        $this->alto = imagesy($this->imagen);
    }

    public function abrir_imagen($archivo) {
        $matriz_ext = explode(".", $archivo);
        $tipo_extension = end($matriz_ext);
        $tipo_extension = strtolower($tipo_extension);
        switch($tipo_extension) {
            case "jpg":
            case "jpeg":
                $tipo_archivo = imagecreatefromjpeg($archivo);
            break;
            case "gif":
                $tipo_archivo = imagecreatefromgif($archivo);
            break;
            case "png":
                $tipo_archivo = imagecreatefrompng($archivo);
            break;
        }
        return $tipo_archivo;
    }

    public function imagen_redimensionada($nuevo_ancho, $nuevo_alto) {

            // imagen tamano fijo
            $ancho_fijo = $nuevo_ancho;
            $alto_fijo = $nuevo_alto;

            $rango_ancho = $nuevo_ancho / $this->ancho;
            $rango_alto = $nuevo_alto / $this->alto;
            $rango = min($rango_alto, $rango_ancho);
            $nuevo_ancho = intval($rango * $this->ancho);
            $nuevo_alto = intval($rango * $this->alto);

            $this->nueva_imagen = @imagecreatetruecolor($nuevo_ancho, $nuevo_alto) or die("Error al crear la imagen.");
            imagecopyresampled($this->nueva_imagen, $this->imagen, 0, 0, 0, 0, $nuevo_ancho, $nuevo_alto, $this->ancho, $this->alto);

        }

    public function grabar_imagen($ruta, $calidad="100") {
            $matriz_ext = explode(".", $ruta);
            $tipo_extension = end($matriz_ext);
            $tipo_extension = strtolower($tipo_extension);

            switch($tipo_extension)
            {
                case "jpg":
                case "jpeg":
                    if (imagetypes() & IMG_JPG) {
                        imagejpeg($this->nueva_imagen, $ruta, $calidad);
                    }
                break;
                case "gif":
                    if (imagetypes() & IMG_GIF) {
                        imagegif($this->nueva_imagen, $ruta);
                    }
                break;
                case "png":
                    $calidad_png = round(($calidad/100) * 9);
                    $calidad_invertida = 9 - $calidad_png;

                    if (imagetypes() & IMG_PNG) {
                         imagepng($this->nueva_imagen, $ruta, $calidad_invertida);
                    }
                    break;
                default:
                    //Archivo no compatible
                break;
            }

            imagedestroy($this->nueva_imagen);
        }       
    }
?>

And then you use it this way ...

if (isset($_FILES["in_subir"])){
    $nombre = $_FILES["in_subir"]["name"];

    list($ancho, $alto) = getimagesize($nombre)

    // Verifica caracteres no compatibles
    $charset = "UTF-8";
    $nombre = iconv($charset, "ASCII//TRANSLIT", $nombre);
    $nombre = preg_replace("/[^A-Za-z0-9.\-_]/", '-', $nombre);

    // Guarda la imagen en el tamaño original
    move_uploaded_file($_FILES["in_subir"]["tmp_name"], "../galeria/" . $nombre);

    /* Redimensionar 30 % */

    $porcentaje_redimension = 30

    // Asigno el nuevo ancho y alto en proporcion al porcentaje que quiero redimensionar
    $nuevo_ancho = floor(($ancho * $porcentaje_redimension) / 100);
    $nuevo_alto = floor(($alto * $porcentaje_redimension) / 100);

    // Asigno al constructor la ruta de la imagen original
    $nueva_imagen = new redimensionar("../galeria/" . $nombre);

    // Le indico el nuevo alto y ancho de la imagen
    $nueva_imagen->imagen_redimensionada($nuevo_ancho, $nuevo_alto);

    // La guardo en otra ubicacion, en este caso una carpeta de miniatura
    $nueva_imagen->grabar_imagen("../galeria/min/" . $nombre, 100);
}
else {
    echo "Ocurrio un error al intentar subir la imagen.";
    exit();
}

You tell me how it went ... greetings.

    
answered by 09.11.2018 в 15:56