Jump to content

More than one If don't work


ItsMeKam59
Go to solution Solved by Ofarchades,

Recommended Posts

Hello, I have a problem with my code :

<?php
    //// BEGIN CONSTANTS ////
    // coordinates for the skin's face
    $face_x = 8;
    $face_y = 8;
    $face_width = 8;
    $face_height = 8;
   
    // coordinates for the skin's "mask", i.e. the layer that is overlaid
    // on top of the face
    $mask_x = 40;
    $mask_y = 8;
    $mask_width = 8;
    $mask_height = 8;
 
    // size of the output image
    $avatar_width = 96;
    $avatar_height = 96;
   
    // The default skin. All hail Steve!
    $default_skin_url = 'http://halcyon-pvp.fr/dl/img/char.png';
    //// END CONSTANTS ////
   
    // Try to load the user's skin from the Amazon S3 storage
    if (isset($_GET['user'])) {
        $user = $_GET['user'];
        $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
    }
   
    if (!$skin) {
        // If skin could not be retrieved, display Steve
        $skin = imagecreatefrompng($default_skin_url);
    }
   
    // Set up a blank image to write to
    $avatar = imagecreatetruecolor($avatar_width, $avatar_height);
 
    // Resize and overlay the face region, as defined by the constants above
    imagecopyresized($avatar, $skin, 0, 0, $face_x, $face_y,
        $avatar_width, $avatar_height, $face_width, $face_height);
 
    // Resize and overlay the mask region
    imagecopyresized($avatar, $skin, 0, 0, $mask_x, $mask_y,
        $avatar_width, $avatar_height, $mask_width, $mask_height);
   
    // Finally, return the processed image as a png
    header('Content-Type: image/png');
    imagepng($avatar);
    imagedestroy($avatar);
?>

Here's the problem :

    if (isset($_GET['user'])) {
        $user = $_GET['user'];
        $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
    }
   
    if (!$skin) {
        // If skin could not be retrieved, display Steve
        $skin = imagecreatefrompng($default_skin_url);
    }

The code works properly, but I want to add a new if, so i wrote this :

    if (isset($_GET['user'])) {
        $user = $_GET['user'];
        $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
    }
   
    elseif (isset($_GET['user'])) {
        $user = $_GET['user'];
        $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
    }

    else (!$skin) {
        // If skin could not be retrieved, display Steve
        $skin = imagecreatefrompng($default_skin_url);
    }

But it doesn't work

Could someone help me ?
Thanks !

Edited by ItsMeKam59
Link to comment
Share on other sites

if (isset($_GET['user'])) {
 
}
elseif (isset($_GET['user'])) { //since this is checking the exact same thing as the first IF(), this will never get triggered since it's in the elseif() which will only happen if the first IF() isn't true, which it is
 
}
Link to comment
Share on other sites

if (isset($_GET['user'])) {
 
}
elseif (isset($_GET['user'])) { //since this is checking the exact same thing as the first IF(), this will never get triggered since it's in the elseif() which will only happen if the first IF() isn't true, which it is
 
}

That what I want to do, if the IF is false, it goes at the Elseif, and if the Elseif is false, it goes at the else

 

 

 

Try changing this

else (!$skin) {

To this

elseif (!$skin) {

It didn't worked

 

 

I want to check if the PNG is in the first link, if false, it checks on the second link, if it's a second time false, it sets a default image

Edited by ItsMeKam59
Link to comment
Share on other sites

But you have a logic problem and you're essentially saying this:

 

 

if ($something == 'a') {
  echo 'found in the IF';
} else if ($something == 'a') {
  echo 'found in the elseif';
}

 

If something does == 'a', what will be echoed? It will never reach the elseif if something == 'a'.

Edited by CroNiX
Link to comment
Share on other sites

But you have a logic problem and you're essentially saying this:

if ($something == 'a') {
  echo 'found in the IF';
} else if ($something == 'a') {
  echo 'found in the elseif';
}

If something does == 'a', what will be echoed? It will never reach the elseif if something == 'a'.

You didn't understood I think, it want :

-if the file exists on http://abc.com, it gets it

-if the if is false, it gets the file on http://othersite.com

-if the both sites don't have any file, it sets a default image

 

 

It may be beneficial to identify what it's not doing that you'd like it to be doing?

I will do this, wait

Link to comment
Share on other sites

I want to check if the PNG is in the first link, if false, it checks on the second link, if it's a second time false, it sets a default image

 

If you always want the last part to take place when the first two parts fail, you'll want to use "else".

<?php
if (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
}
 
elseif (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
}
 
else {
     // If skin could not be retrieved, display Steve
     $skin = imagecreatefrompng($default_skin_url);
}
?>
 
Note that you don't indicate a condition with else.
Link to comment
Share on other sites

 

If you always want the last part to take place when the first two parts fail, you'll want to use "else".

<?php
if (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
}
 
elseif (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
}
 
else {
     // If skin could not be retrieved, display Steve
     $skin = imagecreatefrompng($default_skin_url);
}
?>
 
Note that you don't indicate a condition with else.

 

How could I indicate a condition so ?

 

Because I tried this :

	if(isset($_GET['user'])) {
		$user = htmlspecialchars($_GET['user']);
	}
	
	if (file_exists("http://halcyon-pvp.fr/skins/$user.png")) {
		$skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
	}
	
	elseif(file_exists("http://skins.minecraft.net/MinecraftSkins/$user.png"))
	{
		$skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
	}
	else {
		$skin = imagecreatefrompng($default_skin_url);
	}

It sets the default image directly, or the file exists

Edited by ItsMeKam59
Link to comment
Share on other sites

Just to clarify, are we still trying to get the following if/elseif/else structure working?

if (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
}
 
elseif (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
}
 
else {
     // If skin could not be retrieved, display Steve
     $skin = imagecreatefrompng($default_skin_url);
}
 
Or do you have different code now?
Link to comment
Share on other sites

 

Just to clarify, are we still trying to get the following if/elseif/else structure working?

if (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");
}
 
elseif (isset($_GET['user'])) {
     $user = $_GET['user'];
     $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
}
 
else {
     // If skin could not be retrieved, display Steve
     $skin = imagecreatefrompng($default_skin_url);
}
 
Or do you have different code now?

 

It's almost the same code, i just added (file_exists and added the $user above the if to store properly the code

Link to comment
Share on other sites

you actually need separate if() statements, for the if(!$skin)... tests.

 

the value you are testing is dependent on the previous if() statement, not exclusive of it.

 

an if/elseif/else string of test are exclusive of each other.

Edited by mac_gyver
Link to comment
Share on other sites

I'm going to guess that what you're trying to do is something like...

if (isset($_GET['user'])) {
    $user = $_GET['user'];
    $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");

    if (!$skin) {
        $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
    }
}
 
if (!isset($skin) || !$skin)
     // If skin could not be retrieved, display Steve
     $skin = imagecreatefrompng($default_skin_url);
}

?

 

(I feel like there's so much wrong with all of this code, but I'm just going to let it slide :P)

Link to comment
Share on other sites

$image = $default_skin_url; //used as default
if (isset($_GET['user'])) { //is the user set?
  $user = $_GET['user'];
  //Does the user image exist on site A?
  $site_a = "http://halcyon-pvp.fr/skins/$user.png";
  $site_b = "http://skins.minecraft.net/MinecraftSkins/$user.png";
 
  if (file_get_contents($site_a) !== FALSE) $image = $site_a;
  else if (file_get_contents($site_b) !== FALSE) $image = $site_b;
}
 
$skin = imagecreatefrompng($image);

1) If image exists on site_a, it uses that.

2) If image does not exist on site_a, it checks site_b. If it exists there, it uses that.

3) If image does not exist on site_a or site_b, it uses the default.

Edited by CroNiX
Link to comment
Share on other sites

I'm going to guess that what you're trying to do is something like...

if (isset($_GET['user'])) {
    $user = $_GET['user'];
    $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");

    if (!$skin) {
        $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
    }
}
 
if (!isset($skin) || !$skin)
     // If skin could not be retrieved, display Steve
     $skin = imagecreatefrompng($default_skin_url);
}

?

 

(I feel like there's so much wrong with all of this code, but I'm just going to let it slide :P)

I fixed the errors, but this code don't work, there are many errors with the imagecopyresized (bottom of the code)

 

 

$image = $default_skin_url; //used as default
if (isset($_GET['user'])) { //is the user set?
  $user = $_GET['user'];
  //Does the user image exist on site A?
  $site_a = "http://halcyon-pvp.fr/skins/$user.png";
  $site_b = "http://skins.minecraft.net/MinecraftSkins/$user.png";
 
  if (file_get_contents($site_a) !== FALSE) $image = $site_a;
  else if (file_get_contents($site_b) !== FALSE) $image = $site_b;
}
 
$skin = imagecreatefrompng($image);

1) If image exists on site_a, it uses that.

2) If image does not exist on site_a, it checks site_b. If it exists there, it uses that.

3) If image does not exist on site_a or site_b, it uses the default.

 

This code just work for the site_a, but when It should checks the site_b and the default, it says me :


Warning: file_get_contents(http://halcyon-pvp.fr/skins/Blabla.png): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/halcyonp/public_html/skinHead.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /home/halcyonp/public_html/skinHead.php:31) in /home/halcyonp/public_html/skinHead.php on line 77
‰PNG  IHDR``múàoÏIDATxœíÜ1JAFáÉîHŒ&õb!6±ðÚÚ ^@ˆ…­…Bì¯ ØÚÚ‰x‹ØY¦PlD!wõoÀ ßWÿ¸ËsšÁ˜ÚùÑN ƒá'n^>†¸©Ê 7£q‰›rÌïó•ð¬µ•%Üd¸øçbk®Ž£Ç·wÜdY>‰÷ ³ ›Qàwïk·ý'Üx‚€€€€€€€€€€@ì?¿â(ßü“j O›Âûš'Ä<ðgùBÎS>8÷5O0000000000ˆ1%Qõw÷µƒí ~Ôê.nš­&nö;ë¸ñ˜åüw¨øû4RîkÝÎfÊ;¡…oîîpÓ^nãÆb}&âˆÿ«*„³Ó+ÜìmMæ.¶8Ͽע(psqØÁ'Äf£•0à¢wyüû·IÔ»ægU7'¸Iù®~O0000000000øMÀEÿ+G4¯IEND®B`‚

It doesn't go on the others links, it stays on the site_a

Link to comment
Share on other sites

I fixed the errors, but this code don't work, there are many errors with the imagecopyresized (bottom of the code)

 

What do you mean by this?

 

With the other code, the warning is being thrown by the first file_get_contents not finding the file. Try adding @ before the function calls, I guess.

Edited by Ofarchades
Link to comment
Share on other sites

 

Instead of using file_get_contents() here:

if (file_get_contents($site_a) !== FALSE) $image = $site_a;
else if (file_get_contents($site_b) !== FALSE) $image = $site_b;
 
You could try using file_exists(), like you did before.

 

 

Ideally once we've established what he's trying to achieve, we'd want to get him to change it so that the image doesn't need to be downloaded twice (once for the file_get_contents/whatever and then again for the imagecreatefrompng) i.e. store the result in a variable and pass it to imagecreatefromstring. At this stage, I fear that may just cause more confusion than there is already. :P

Link to comment
Share on other sites

 

Instead of using file_get_contents() here:

if (file_get_contents($site_a) !== FALSE) $image = $site_a;
else if (file_get_contents($site_b) !== FALSE) $image = $site_b;
 
You could try using file_exists(), like you did before.

 

If i do this, it directly goes on the default image

 

 

What do you mean by this?

 

With the other code, the warning is being thrown by the first file_get_contents not finding the file. Try adding @ before the function calls, I guess.

It didn't worked

Link to comment
Share on other sites

Here's the FULL code (I have cancelled all the wrong things since Cronix's answer) :

<?php
    //// BEGIN CONSTANTS ////
    // coordinates for the skin's face
    $face_x = 8;
    $face_y = 8;
    $face_width = 8;
    $face_height = 8;
   
    // coordinates for the skin's "mask", i.e. the layer that is overlaid
    // on top of the face
    $mask_x = 40;
    $mask_y = 8;
    $mask_width = 8;
    $mask_height = 8;
 
    // size of the output image
    $avatar_width = 96;
    $avatar_height = 96;
   
    // The default skin. All hail Steve!
    $default_skin_url = 'http://halcyon-pvp.fr/dl/img/char.png';
    //// END CONSTANTS ////
   
	$image = $default_skin_url; //used as default
	if (isset($_GET['user'])) { //is the user set?
		$user = $_GET['user'];
		//Does the user image exist on site A?
		$site_a = "http://halcyon-pvp.fr/skins/$user.png";
		$site_b = "http://skins.minecraft.net/MinecraftSkins/$user.png";
 
	if (file_get_contents($site_a) !== FALSE) $image = $site_a;
	else if (file_get_contents($site_b) !== FALSE) $image = $site_b;
	}
 
	$skin = imagecreatefrompng($image);
   
    // Set up a blank image to write to
    $avatar = imagecreatetruecolor($avatar_width, $avatar_height);
 
    // Resize and overlay the face region, as defined by the constants above
    imagecopyresized($avatar, $skin, 0, 0, $face_x, $face_y,
        $avatar_width, $avatar_height, $face_width, $face_height);
 
    // Resize and overlay the mask region
    imagecopyresized($avatar, $skin, 0, 0, $mask_x, $mask_y,
        $avatar_width, $avatar_height, $mask_width, $mask_height);
   
    // Finally, return the processed image as a png
    header('Content-Type: image/png');
    imagepng($avatar);
    imagedestroy($avatar);
?>

It maybe comes cause of the Resize or the Header ?
You could try the code, drop this code on a file like skin.php, drag in on your FTP, and try "http://yoursite.com/skin.php?user=AxploOdee"
It's the site_a, the Head will Appears
To try the site_b write "http://yoursite.com/skin.php?user=Ewearys"
To try the defaut, write the name of your choice. The AxploOdee image exists on the site_a, and Ewearys on the site_b

Edited by ItsMeKam59
Link to comment
Share on other sites

  • Solution

I just tested it. Like I said, change

if (file_get_contents($site_a) !== FALSE) $image = $site_a;
else if (file_get_contents($site_b) !== FALSE) $image = $site_b;

to

if (@file_get_contents($site_a) !== FALSE) $image = $site_a;
else if (@file_get_contents($site_b) !== FALSE) $image = $site_b;

and it works.

 

Also the code I posted earlier would have worked, but it was missing an opening brace that I wasn't able to see because I wrote it in this site's edit box. You should have easily seen it in your code editor, though. You are using a code editor... aren't you? :P

 

Anyway, as mentioned before, you don't really want to call both file_get_contents and imagecreatefrompng because that means the image will be downloaded twice from the remote server. Instead, try:

<?php

    //// BEGIN CONSTANTS ////
    // coordinates for the skin's face
    $face_x = 8;
    $face_y = 8;
    $face_width = 8;
    $face_height = 8;
   
    // coordinates for the skin's "mask", i.e. the layer that is overlaid
    // on top of the face
    $mask_x = 40;
    $mask_y = 8;
    $mask_width = 8;
    $mask_height = 8;
 
    // size of the output image
    $avatar_width = 96;
    $avatar_height = 96;
   
    // The default skin. All hail Steve!
    $default_skin_url = 'http://halcyon-pvp.fr/dl/img/char.png';
    //// END CONSTANTS ////
    
    if (isset($_GET['user'])) {
        $user = $_GET['user'];
        $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");

        if (!$skin) {
            $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
        }
    }

    if (!isset($skin) || !$skin) {
         // If skin could not be retrieved, display Steve
         $skin = imagecreatefrompng($default_skin_url);
    }

    // Set up a blank image to write to
    $avatar = imagecreatetruecolor($avatar_width, $avatar_height);
 
    // Resize and overlay the face region, as defined by the constants above
    imagecopyresized($avatar, $skin, 0, 0, $face_x, $face_y,
        $avatar_width, $avatar_height, $face_width, $face_height);
 
    // Resize and overlay the mask region
    imagecopyresized($avatar, $skin, 0, 0, $mask_x, $mask_y,
        $avatar_width, $avatar_height, $mask_width, $mask_height);
   
    // Finally, return the processed image as a png
    header('Content-Type: image/png');
    imagepng($avatar);
    imagedestroy($avatar);
?>

Alternatively, you could keep the existing file_get_contents code, but save the result to a variable and pass it to imagecreatefromstring. The only problem with that is that imagecreatefromstring uses more memory.

Edited by Ofarchades
Link to comment
Share on other sites

I just tested it. Like I said, change

if (file_get_contents($site_a) !== FALSE) $image = $site_a;
else if (file_get_contents($site_b) !== FALSE) $image = $site_b;

to

if (@file_get_contents($site_a) !== FALSE) $image = $site_a;
else if (@file_get_contents($site_b) !== FALSE) $image = $site_b;

and it works.

 

Also the code I posted earlier would have worked, but it was missing an opening brace that I wasn't able to see because I wrote it in this site's edit box. You should have easily seen it in your code editor, though. You are using a code editor... aren't you? :P

 

Anyway, as mentioned before, you don't really want to call both file_get_contents and imagecreatefrompng because that means the image will be downloaded twice from the remote server. Instead, try:

<?php

    //// BEGIN CONSTANTS ////
    // coordinates for the skin's face
    $face_x = 8;
    $face_y = 8;
    $face_width = 8;
    $face_height = 8;
   
    // coordinates for the skin's "mask", i.e. the layer that is overlaid
    // on top of the face
    $mask_x = 40;
    $mask_y = 8;
    $mask_width = 8;
    $mask_height = 8;
 
    // size of the output image
    $avatar_width = 96;
    $avatar_height = 96;
   
    // The default skin. All hail Steve!
    $default_skin_url = 'http://halcyon-pvp.fr/dl/img/char.png';
    //// END CONSTANTS ////
    
    if (isset($_GET['user'])) {
        $user = $_GET['user'];
        $skin = @imagecreatefrompng("http://halcyon-pvp.fr/skins/$user.png");

        if (!$skin) {
            $skin = @imagecreatefrompng("http://skins.minecraft.net/MinecraftSkins/$user.png");
        }
    }

    if (!isset($skin) || !$skin) {
         // If skin could not be retrieved, display Steve
         $skin = imagecreatefrompng($default_skin_url);
    }

    // Set up a blank image to write to
    $avatar = imagecreatetruecolor($avatar_width, $avatar_height);
 
    // Resize and overlay the face region, as defined by the constants above
    imagecopyresized($avatar, $skin, 0, 0, $face_x, $face_y,
        $avatar_width, $avatar_height, $face_width, $face_height);
 
    // Resize and overlay the mask region
    imagecopyresized($avatar, $skin, 0, 0, $mask_x, $mask_y,
        $avatar_width, $avatar_height, $mask_width, $mask_height);
   
    // Finally, return the processed image as a png
    header('Content-Type: image/png');
    imagepng($avatar);
    imagedestroy($avatar);
?>

Alternatively, you could keep the existing file_get_contents code, but save the result to a variable and pass it to imagecreatefromstring. The only problem with that is that imagecreatefromstring uses more memory.

Oh yes, it works! I think I mistook something, thanks ! :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.