Doubt with select in if

-1

Good afternoon

I was trying to adapt a code I already had here to be able to by a watermark on a photo. But there's plenty of time I do not deal with html or php. So could you guys give me this help to fix the problem of selecting the correct .png (watermark) and even reformatting the code to make it simpler?

<script src="http://deepliquid.com/Jcrop/js/jquery.Jcrop.min.js"></script><scriptsrc="js/jquery.Jcrop.js"></script>
<link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
<link rel="stylesheet" href="demo_files/demos.css" type="text/css" />
<?php

    if(isset($_POST['cor']) == "preto") {
          $image_path = "avatares/preto.png";
    } elseif (isset($_POST['cor']) == "branco") {
        $image_path = "avatares/branco.png";
    } elseif (isset($_POST['cor']) == "colorido") {
        $image_path = "avatares/colorido.png";
    } else {
        echo ":)";
    }

function watermark_image($oldimage_name, $new_image_name){
    global $image_path;
    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width  = 800;
    $height = 800;    
    $im = imagecreatetruecolor($width, $height);
    $img_src = imagecreatefromjpeg($oldimage_name);
    imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    $watermark = imagecreatefrompng($image_path);
    list($w_width, $w_height) = getimagesize($image_path);        
    $pos_x = $width - $w_width; 
    $pos_y = $height - $w_height;
    imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
    imagejpeg($im, $new_image_name, 100);
    imagedestroy($im);
    unlink($oldimage_name);
    return true;
}

$demo_image= "";
if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit"){
    $path = "uploads/";
    $valid_formats = array("jpg","bmp","jpeg");
    $name = $_FILES['imgfile']['name'];
    if(strlen($name))
{
   list($txt, $ext) = explode(".", $name);
   if(in_array($ext,$valid_formats)&& $_FILES['imgfile']['size'] <= 10*256*1024)
    {
    $upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);
    if($upload_status){
        $new_name = $path.time().".jpg";
        if(watermark_image($path.$_FILES['imgfile']['name'], $new_name))
                $demo_image = $new_name;

    }
    }
    else
    $msg = "A foto tem que ter menos que 2,5mb.";
    }
}

    # Conta quantos arquivos existem na pasta de upload
    $diretorio = scandir("uploads/");
    $qtd = count($diretorio) - 2;

?>
    <html lang="pt-br">

    <head>
        <meta charset="UTF-8">
        <title>Avatarizador</title>
        <link rel="stylesheet" href="css/avatar.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head>

    <body>
        <div id="conteudo">
            <form name="imageUpload" id="imageUpload" enctype="multipart/form-data" method="post" action="">
                <h1>Avatarizador !</h1>
                <div class="giro">V 2.0</div>
                <p class="texto">Já foram criadas <strong><?PHP echo ("$qtd"); ?></strong> fotos com o avatar do EREA Salvador.</p>
                <input style="margin-bottom:15px;" type="file" name="imgfile" id="imgfile" />
                <select name="cor">
                    <option value="preto">Preto</option>
                    <option value="branco">Branco</option>
                    <option value="colorido">Colorido</option>
                </select>
                <br>
                <input type="submit" name="createmark" id="createmark" value="Submit" />
                <br>
                <?php
                    if(!empty($demo_image)){
                        echo '
                            <center>
                                <b>Click na imagem para fazer o download.</b>
                                <br><br>
                                <a href="'.$demo_image.'" download>
                                <img id="avatar" src="'.$demo_image.'" />
                                </a>
                            </center>
                            ';

                        $arquivo = "rastro.txt";
                        date_default_timezone_set('America/Bahia');
                        $data = date('d/m/Y H:i:s', time());
                        $ip = $_SERVER['REMOTE_ADDR'];    
                        $browser = $_SERVER['HTTP_USER_AGENT']; 
                        $fp = fopen($arquivo, "a+");   
                        fwrite($fp,"Arquivo: $new_name | Data: $data | IP: $ip | Navegador: $browser \n\r");   
                        fclose($fp);

                    }
                ?>
            </form>
        </div>
    </body>

    </html>

For those who want to know how the code is behaving now, just access: link

    
asked by anonymous 03.11.2016 / 19:02

2 answers

1

Dear, sorry, but I've come to a solution using a switch: D

The result looks like this:

<script src="http://deepliquid.com/Jcrop/js/jquery.Jcrop.min.js"></script><scriptsrc="js/jquery.Jcrop.js"></script>
<link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
<link rel="stylesheet" href="demo_files/demos.css" type="text/css" />
<?php

switch($_POST['cor']){
    case 'preto' : 
        $image_path = "avatares/preto.png";
        break;
    case 'branco':
        $image_path = "avatares/branco.png";
        break;
    case 'colorido':
        $image_path = "avatares/colorido.png";
        break;
    default:
        echo "Eita, deu erro";
}


function watermark_image($oldimage_name, $new_image_name){
    global $image_path;
    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width  = 800;
    $height = 800;    
    $im = imagecreatetruecolor($width, $height);
    $img_src = imagecreatefromjpeg($oldimage_name);
    imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    $watermark = imagecreatefrompng($image_path);
    list($w_width, $w_height) = getimagesize($image_path);        
    $pos_x = $width - $w_width; 
    $pos_y = $height - $w_height;
    imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
    imagejpeg($im, $new_image_name, 100);
    imagedestroy($im);
    unlink($oldimage_name);
    return true;
}

$demo_image= "";
if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit"){
    $path = "uploads/";
    $valid_formats = array("jpg","bmp","jpeg");
    $name = $_FILES['imgfile']['name'];
    if(strlen($name))
{
   list($txt, $ext) = explode(".", $name);
   if(in_array($ext,$valid_formats)&& $_FILES['imgfile']['size'] <= 10*256*1024)
    {
    $upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);
    if($upload_status){
        $new_name = $path.time().".jpg";
        if(watermark_image($path.$_FILES['imgfile']['name'], $new_name))
                $demo_image = $new_name;

    }
    }
    else
    $msg = "A foto tem que ter menos que 2,5mb.";
    }
}

    # Conta quantos arquivos existem na pasta de upload
    $diretorio = scandir("uploads/");
    $qtd = count($diretorio) - 2;

?>
    <html lang="pt-br">

    <head>
        <meta charset="UTF-8">
        <title>Avatarizador</title>
        <link rel="stylesheet" href="css/avatar.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head>

    <body>
        <div id="conteudo">
            <form name="imageUpload" id="imageUpload" enctype="multipart/form-data" method="post" action="">
                <h1>Avatarizador !</h1>
                <div class="giro">V 2.0</div>
                <p class="texto">Já foram criadas <strong><?PHP echo ("$qtd"); ?></strong> fotos com o avatar do EREA Salvador.</p>
                <input style="margin-bottom:15px;" type="file" name="imgfile" id="imgfile" />
                <select name="cor">
                    <option value="preto">Preto</option>
                    <option value="branco">Branco</option>
                    <option value="colorido">Colorido</option>
                </select>
                <br>
                <input type="submit" name="createmark" id="createmark" value="Submit" />
                <br>
                <?php
                    if(!empty($demo_image)){
                        echo '
                            <center>
                                <b>Click na imagem para fazer o download.</b>
                                <br><br>
                                <a href="'.$demo_image.'" download>
                                <img id="avatar" src="'.$demo_image.'" />
                                </a>
                            </center>
                            ';

                        $arquivo = "rastro.txt";
                        date_default_timezone_set('America/Bahia');
                        $data = date('d/m/Y H:i:s', time());
                        $ip = $_SERVER['REMOTE_ADDR'];    
                        $browser = $_SERVER['HTTP_USER_AGENT']; 
                        $fp = fopen($arquivo, "a+");   
                        fwrite($fp,"Arquivo: $new_name | Data: $data | IP: $ip | Navegador: $browser \n\r");   
                        fclose($fp);

                    }
                ?>
            </form>
        </div>
    </body>

    </html>
    
03.11.2016 / 19:46
1

You made a little confusion when using the "isset ()" function. It checks whether the variable has already been defined and the result is boolean (true or false).

I suppose you want the following:

if(isset($_POST['cor']) {
    switch ($_POST['cor']) {
        case 'preto':
            $image_path = "avatares/preto.png";
            break;
        case 'branco':
            $image_path = "avatares/branco.png";
            break;
        case 'colorido':
            $image_path = "avatares/colorido.png";
            break;
        default:
            echo ":)";
    }
}
    
03.11.2016 / 19:50