2

Estou querendo que ao inserir a imagem no site pelo type="file" ela redimensione para 400x300, já tentei de tudo mas não consigo.

Código de verificação da imagem para enviar pro BD e inserir no site:

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();
    }
2
  • Só uma dica, nada a ver com a questão: utilize o método pathinfo() para verificar a extensão. 8/11/2016 às 17:55
  • O que você usa para manipulação de imagens, imagick? 8/11/2016 às 17:57

2 Respostas 2

2

Existe uma extensão para trabalhar com imagens, se chama php-gd, sua instalação será necessária.

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;
}

Envie como parâmetro da função o conteúdo do arquivo enviado

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

Depois é só escolher em que local do disco irá salvar o retorno

2

Você precisa usar as funções ImageMagick ou GD do PHP para trabalhar com imagens.

Com GD, Voce pode fazer desta forma

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;
}

E chamar esta função assim...

$img = resize_image(‘$destinoM/$nomeM.$tipoM’, 400, 300);
2
  • Essa função não vem instalada por padrão no PHP. O ideal seria informá-lo. 8/11/2016 às 17:58
  • Verdade respondi aqui do trampo vou melhorar a resposta 8/11/2016 às 18:04

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .