Jump to content

Check if remote image exists


Username:

Recommended Posts

:shrug: :shrug: :shrug: :shrug:

Please help before this script drives me insane.

It tries to download a skin from Minecraft.

 

It works fine, except, if it returns an error code, (301, 302, 404), don't download, and using my image (char.png) as a replacement.

I can get the error code, except, no matter how I compare it, it will a) still say it's 302 or b) not work at all.

 

 

Here's my code:

 

userskin.inc.php >

<?php
require('class.mineuser.php');
if(!isset($_GET['width']) || strlen(trim($_GET['width'])) == 0 || !isset($_GET['height']) || strlen(trim($_GET['height'])) == 0) {
$x = 16;
$y = 16;
} else {
$x = $_GET['width'];
$y = $_GET['height'];
}

if(isset($_GET['refresh'])) 
$refresh = true;
else
$refresh = false;

if(isset($_GET['debug'])) 
$debug = true;
else
$debug = false;

$http = curl_init("http://www.minecraft.net/skin/{$_GET['user']}.png");
curl_setopt($http, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($http);
$http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
$resp = preg_match("/302/", $http_status, $match);
$code = $match[0];
if($code == 302) {
mineuser::downloadSkin($_GET['user'], $_GET['path'], $code, $refresh);
mineuser::getFace($_GET['user'].".png", $x, $y, $_GET['path'], $code, $debug);
} else {
mineuser::getFace("char.png", $x, $y, $_GET['path'], $code, $debug);
}

 

class.mineuser.php >

<?php
class mineuser {
	public static function downloadSkin($user, $path = "", $resp, $new = false) {
		if($resp !== 302) {
		//Checks if $new is true, if it is, then delete $user.png and download a new one.
		if($new == true) unlink("$path/$user.png");

		//Checks if the users skin already exists (faster loading time)
		if(!file_exists("$path/$user.png")) { 
			$fh = fopen("$path/$user.png", "w"); 
			$file = file("http://www.minecraft.net/skin/$user.png");

			foreach($file as $char) //Since it's an array, we use a foreach loop and write each character individually
				fwrite($fh, $char);

			fclose($fh); //Close our file handler. 
		}
		} else {

		}
	} //End of function downloadSkin()

	public static function getFace($user, $width, $height, $path, $resp, $debug = true) {
		$im = imagecreatetruecolor($width, $height);
		if($debug == false) {
		header("Content-type: image/png");
		}
		if($resp === 302) {
			$image = imagecreatefrompng("$path/char.png");
		} else {
			$image = imagecreatefrompng("$path/$user");
		}
		imagecopyresampled($im, $image, 0, 0, 8, 8, $width, $height, 8, ;

		imagepng($im);
		imagedestroy($im);

	} //End of function getFace()
}
?>

 

 

please help before i go insane  :-\

 

Thanks in advance  :)

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.