-4

What I am trying to do is:

  • Get person profile pic;
  • Get a randon pic that is set with de switch
  • And mix the random pic with the person profile pic
  • And show it to the guy!

Can do this?

require_once("src/facebook.php");

$configurar = array();
$configurar['appId'] = '2033xxxxx389';
$configurar['secret'] = 'cc3xxxx9b1';
$configurar['fileUpload'] = true;

$facebook = new Facebook($configurar);

$signed_request = $facebook->getSignedRequest();
$__idPagina = $signed_request["page"]["id"];
$__adminPagina = $signed_request["page"]["admin"];
$__statusLike = $signed_request["page"]["liked"];
$__pais = $signed_request["user"]["country"];
$__locale = $signed_request["user"]["locale"];

$_idUsuario = $facebook->getUser();
?>
<html>
    <head></head>
    <body>

        <?
            if($_idUsuario)
            {
                try
                {
                    if($__statusLike)
                    {
                        //AQUI VAI A PARTE QUE FUNCIONA A MÁGICA DA FOTO! POORRA
                        // DEFINIR VARIAVEIS
                        $foto_x = $_idUsuario."_x.jpg";
                        $foto_y = $_idUsuario."_y.jpg";
                        $img = $_idUsuario.".jpg";

                        copy('http://graph.facebook.com/'.$_idUsuario.'/picture?width=150&height=150', 'lixeira/'.$foto_x);

                        //ARMA ALEATÓRIA NA BAGAÇA
                        $armaRandomica = rand(1,10);

                        //DEFINE O MOLDE DE ACORDO COMO O NUMERO QUE PEGOU AI /\

                        switch ($armaRandomica) {
                            case 1:
                                $moldeArma = "molde/_bazuka.jpg";
                                break;
                            case 2:
                                $moldeArma = "molde/_espada.jpg";
                                break;
                            case 3:
                                $moldeArma = "molde/_espingarda.jpg";
                                break;
                            case 4:
                                $moldeArma = "molde/_faca.jpg";
                                break;
                            case 5:
                                $moldeArma = "molde/_granada.jpg";
                                break;
                            case 6:
                                $moldeArma = "molde/_metralhadora.jpg";
                                break;
                            case 7:
                                $moldeArma = "molde/_nokia.jpg";
                                break;
                            case 8:
                                $moldeArma = "molde/_pistola.jpg";
                                break;
                            case 9:
                                $moldeArma = "molde/_shotgun.jpg";
                                break;
                            case 10:
                                $moldeArma = "molde/_sniper.jpg";
                                break;
                            }

                        echo '<img src="'.$moldeArma.'" />';

                        //JUNTAR A FOTO DO PERFIL COM O MOLDE!

                        header("Content-type:image/jpeg");

                        $stamp = imagecreatefromjpeg($foto_x);
                        $im = imagecreatefromjpeg($moldeArma);
                        $marge_right = 20;
                        $marge_bottom = 330;
                        $sx = imagesx($stamp);
                        $sy = imagesy($stamp);
                        imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
                        imagejpeg($im, $foto_y,100);
                        imagedestroy($im);                      
                        exit;

                        //USA ISSO COMO $perfilUsuario['name'] e talz!
                        $perfilUsuario = $facebook->api('/me','GET');

                        //MOSTRA A FOTO PRO NEGRO!
                        echo '<img src="lixeira/'.$foto_x.'" />';
                    }
                    else
                    {
                        echo '<center><img src="imagens/curta.png"/></center>';
                    }
                }
                catch(FacebookApiException $e)
                {
                    $urlLogin = $facebook->getLoginUrl();
                    echo 'Porfavor <a href="'.$urlLogin.'">LOGUE!</a>';
                    error_log($e->getType());
                    error_log($e->getMessage());
                }
            }
            else
            {
                $urlLogin = $facebook->getLoginUrl();
                echo 'Porfavor <a href="'.$urlLogin.'">LOGUE!</a>';
            }
        ?>
    </body>
</html>

And this is what I get from that code, what should I do to fix this error?

https://i.stack.imgur.com/lrFKd.png

Help me people =D

Shoe
  • 74,840
  • 36
  • 166
  • 272

1 Answers1

0

There are two problems with your code:

  1. On line 81, you're outputting a content-type header. A single file can only output 1 content-type it's either a HTML file or an Image file. More on this later

  2. On line 83, you're trying to load an image from Facebook (imagecreatefromjpeg($foto_x);), which cannot be loaded. You're only specifying the name of the image here (1100000000_x.jpg), without a directory or location, so PHP assumes is a local file and is trying to load a file that is in the same directory as the PHP script. I'm no familiar with the Facebook API, but I suspect that the image should be downloaded from their server, via HTTP, depending on your server settings this might or might not be allowed;

    $stamp = imagecreatefromjpeg('http://facebook.com/path/to/image/' . $foto_x);

Back to the first issue; It is not possible to output both an image AND the HTML in a single PHP script. One option might be to load the image from Facebook, manipulate it, write it to a file and use that as 'src' for the IMG-tag, for example:

imagejpeg($im, $foto_y,100, '/full/path/to/mynewfilename.jpg');

However, this will probably fill-up your server fast

Another option is to split your script in two separate files;

1) A file that outputs the HTML

2) A file that generates the image

This page will output the HTML and contains a IMG-tag that points to your image-generating .php page

<!DOCTYPE HTML>
<html>
    <head>.....</head>
    <body>
        <img src='./my_super_image.php' /><!-- <<<< The name of your image script -->
    </body>
</html>

This page will generate and output an image (only an image, nothing else)!:

<?php
require_once("src/facebook.php");

.....

header("Content-type:image/jpeg");

$stamp = imagecreatefromjpeg('http://facebook.com/path/to/' . $foto_x);
$im = imagecreatefromjpeg($moldeArma);
$marge_right = 20;
$marge_bottom = 330;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
imagejpeg($im, $foto_y,100);
imagedestroy($im);                      
?>

The code above is just for illustration, nothing has been tested!

thaJeztah
  • 27,738
  • 9
  • 73
  • 92