Doubt with select in if

Asked

Viewed 62 times

-1

Good afternoon

I was trying to adapt a code I already had here so I could put a watermark on a photo. But it’s been a long time since I’ve done html or php. So, you could give me this help to fix the problem of selecting . correct png (watermark) and even reformulate the code to get more simplified ?

<script src="http://deepliquid.com/Jcrop/js/jquery.Jcrop.min.js"></script>
<script src="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: http://ereasalvador.com.br/avatar/index.php

  • What’s the problem with watermark?

  • Whenever I select one of the colors, the script ends up working only with the black avatar, in this case, the first

2 answers

1

Guys, I’m sorry, but I’ve already come up with a solution using a :D switch

The result was so:

<script src="http://deepliquid.com/Jcrop/js/jquery.Jcrop.min.js"></script>
<script src="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>
  • Yes, I get it. In the original code you made a comparison of isset (Boolean) with "black" (string). Javascript was converting the string "black" to boolean, so any value other than "" (empty or null) is considered true, so it always returned the black.

1

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

I imagine 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 ":)";
    }
}

Browser other questions tagged

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