Resizing image when inserting with php

Asked

Viewed 441 times

2

I’m wanting that when inserting the image on the site by type="file" it resizes to 400x300, I’ve tried everything but I can’t.

Image verification code to send pro BD and insert on the website:

require_once("conn.php");
$imgm=$_FILES['imageM'];
$imgf=$_FILES['imageF'];
$destinoM = 'img/mini/';
$tipoM = $imgm["type"];
$sizeM = $imgm["size"];
$tempM = $imgm["tmp_name"];
$extM = end(explode(".",$imgm["name"]));
$nomeM = "mini_" . "_" . rand(0,99999) . date("zdmYHisu") . "." . $extM;

    echo $destinoM . "<br />" . $tipoM . "<br />" . $sizeM . "<br />" . $tempM . "<br />" . $extM . "<br />" . $nomeM; 

    if (preg_match("/^image\/(gif|jpeg|jpg|png)$/",$tipoM)){
        $caminhoM = $destinoM . $nomeM;
        move_uploaded_file($tempM,$caminhoM);
    }
    $query = "INSERT INTO `TABELAPT` (`nome`, `tipo`, `desc`, `menu`, `imageM`, `imageF`) VALUES ('".$nome."', '".$tipo."', '".$desc."', '".$menu."', '".$nomeM."', '".$nomeF."')";

    // Executa a query
    $inserir = mysql_query($query);

    if ($inserir) {
    echo "Post inserido com sucesso!";
    } else {
    echo "Não foi possível inserir o Post, tente novamente.";
    // Exibe dados sobre o erro:
    echo "Dados sobre o erro:" . mysql_error();
    }
  • Just a hint, nothing to do with the question: use the method pathinfo() to check the extension.

  • What you use for image manipulation, Imagick?

2 answers

2

You need to use the functions Imagemagick or GD PHP to work with images.

With GD, you can do it this way

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}

And call this function so...

$img = resize_image(‘$destinoM/$nomeM.$tipoM’, 400, 300);
  • This function is not installed by default in PHP. The ideal would be to inform you.

  • Truth I answered here from the job I’ll improve the answer

2


There is an extension to work with images, called php-Gd, your installation will be required.

private function resizeImage(&$file) {
    $source = imagecreatefromstring($file);

    $width = imagesx($source);
    $height = imagesy($source);

    $newwidth = 400;
    $newheight = 300;

    $thumb = imagecreatetruecolor($newwidth, $newheight);

    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    ob_start();
    imagejpeg($thumb);
    $imagedata = ob_get_contents();
    ob_end_clean();

    imagedestroy($source);
    imagedestroy($thumb);

    return $imagedata;
}

Send as a function parameter the contents of the submitted file

$file = file_get_contents($_FILES['imageM']['tmp_name']);

Then just choose which disk location will save the return

Browser other questions tagged

You are not signed in. Login or sign up in order to post.