Resize image that comes by POST in PHP

4

How can I resize an image that comes to me by POST from a form?

$rutaFichero = '/img/' . basename($_FILES['imagenNoticia']['name']);

if (move_uploaded_file($_FILES['imagenNoticia']['tmp_name'], $rutaFichero))
        {
            $imagenOriginal = imagecreatefromjpeg($rutaFichero);
            $maxAncho = 500;
            $maxAlto = 500;
            list($ancho, $alto) = getimagesize($rutaFichero);
            $xRatio = $maxAncho / $ancho;
            $yRatio = $maxAlto / $alto;

            if(($ancho <= $maxAncho) && ($alto <= $maxAlto))
            {
                $anchoFinal = $ancho;
                $altoFinal = $alto;
            }
            else if(($xRatio * $alto) < $maxAlto)
            {
                $altoFinal = ceil($xRatio * $alto);
                $anchoFinal = $maxAncho;
            }
            else
            {
                $anchoFinal = ceil($yRatio * $ancho);
                $altoFinal = $maxAlto;
            }

            //$rsr_org = imagecreatefromjpeg("image.jpg");
            //$tmp = imagescale($imagenOriginal, $anchoFinal, $altoFinal,  IMG_BICUBIC_FIXED);
            //imagejpeg($tmp, basename($_FILES['imagenNoticia']['name']);

            $tmp = imagecreatetruecolor($anchoFinal, $altoFinal);
            $resultado = imagecopyresampled($tmp, $imagenOriginal, 0, 0, 0, 0, $anchoFinal, $altoFinal, $ancho, $alto);
            imagedestroy($imagenOriginal);
        }

I have used this code, but it does not resize my image. Any possible solution ?, Or any class that works with php versions prior to 5.3? Thanks

    
asked by 15.06.2016 в 09:52
source

2 answers

1

I have found this that can help you (I think the problem you have is in the ratio):

$fn = $_FILES['imagenNoticia']['name'];
$size = getimagesize($fn);
$ratio = $size[0]/$size[1]; // width/height
if( $ratio > 1) {
    $width = 500;
    $height = 500/$ratio;
}
else {
    $width = 500*$ratio;
    $height = 500;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width,$height);
imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst,$target_filename_here); // adjust format as needed
imagedestroy($dst);
    
answered by 15.06.2016 / 11:51
source
0

I leave you a function that I use, in case it helps you

function redimensionarJPEG ($origen, $destino, $ancho_max, $alto_max, $fijar) {

$info_imagen= getimagesize($origen);
$ancho=$info_imagen[0];
$alto=$info_imagen[1];
if ($ancho>=$alto)
{
    $nuevo_alto= round($alto * $ancho_max / $ancho,0);
    $nuevo_ancho=$ancho_max;
}
else
{
    $nuevo_ancho= round($ancho * $alto_max / $alto,0);
    $nuevo_alto=$alto_max;
}
switch ($fijar)
{
    case "ancho":
        $nuevo_alto= round($alto * $ancho_max / $ancho,0);
        $nuevo_ancho=$ancho_max;
        break;
    case "alto":
        $nuevo_ancho= round($ancho * $alto_max / $alto,0);
        $nuevo_alto=$alto_max;
        break;
    default:
        $nuevo_ancho=$nuevo_ancho;
        $nuevo_alto=$nuevo_alto;
        break;
}
$imagen_nueva= imagecreatetruecolor($nuevo_ancho,$nuevo_alto);
$imagen_vieja= imagecreatefromjpeg($origen);
imagecopyresampled($imagen_nueva, $imagen_vieja, 0, 0, 0, 0,$nuevo_ancho, $nuevo_alto, $ancho, $alto);
imagejpeg($imagen_nueva,$destino);
imagedestroy($imagen_nueva);
imagedestroy($imagen_vieja);
}
    
answered by 25.06.2016 в 21:52