Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP]Image to HTML

Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
i thought it would be nice to release mine hehe. have fun but please add some credit to me if you use mine or give credit to apolva if he is releases his (link - http://forum.ragezone.com/f86/image2html-656985/).

PHP:
<?php
class Image {

	function __construct() {
		$this->cURL = curl_init();
	}
	
	public function setImage($image) {
		$ext = pathinfo($image);
		switch($ext['extension']) {
			case "gif":$getImage=@ImageCreateFromGIF($image);break;
			case "jpg":$getImage=@ImageCreateFromJPEG($image);break;
			case "jpeg":$getImage=@ImageCreateFromJPEG($image);break; //?
			case "png":$getImage=@ImageCreateFromPNG($image);break;
			default:die("Not Supported Image Extension.");break;
		}
		!$getImage?die("Error in File Extension"):$this->image = $getImage;
		$this->xy = $image;
	}
	
	public function rgb2hex($r,$g,$b) {
		$r = dechex($r);
		$g = dechex($g);
		$b = dechex($b);
	
		$color .= (strlen($r)<2?'0':'').$r;
		$color .= (strlen($g)<2?'0':'').$g;
		$color .= (strlen($b)<2?'0':'').$b;
		return "#{$color}";
	}
	
	public function getPixel($x,$y) {
		$RGB = ImageColorAt($this->image, $x, $y);
		$color = ImageColorsForIndex($this->image, $RGB);
		return "<td bgcolor=\"{$this->rgb2hex($color[red],$color[green],$color[blue])}\" width=\"1\"></td>";
	}
	
	public function getXY($xy) {
		list($imageWidth, $imageHeight, $imageType, $imageAttr) = GetImageSize($this->xy);
		if($xy == 'x')
            return $imageWidth;
        else
            return $imageHeight;
    }
    
	public function checkURL($URL) {
		curl_setopt($this->cURL, CURLOPT_URL, $URL);
		curl_setopt($this->cURL, CURLOPT_HEADER, 0);
		curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($this->cURL, CURLOPT_TIMEOUT, 5);
		if(!curl_exec($this->cURL))
			return false;
		else
			return true;
	}
	
	function __destruct() {
		curl_close($this->cURL);
	}
}

$Image = new Image;

if(!$_POST['submit']){
	echo "<form method=\"POST\">";
	echo "<center><table border=\"0\" width=\"300\"><tbody>";
	echo "<tr><td style=\"font-weight: bold; text-align: right;\">Image URL:</td><td><input value=\"http://\" name=\"url\"></td></tr>";
	echo "<tr><td style=\"text-align: center;\" colspan=\"2\"><input type=\"submit\" style=\"margin: 10px;\" value=\"Convert\" name=\"submit\"></td></tr>";
	echo "</tbody></table></center>";
	echo "</form>";
} else {
	if($Image->checkURL($_POST['url'])) {
		$Image->setImage($_POST['url']);
		$width = $Image->getXY('x');
		$height = $Image->getXY('y');
		echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
		for(;$y<$height;$y++) {
			echo "<tr height=\"1\">";
			for($x=0;$x<$width;$x++) {
				echo $Image->getPixel($x,$y);
			}
			echo "</tr>";
		}
		echo "</table>";
	} else {
		die("Please Enter a Valid URL.");
	}
}
?>
it uses cURL to check if the link is valid. then it checks for the file extension to use the correct image grabber thingy. i also added a feature to change the output string.

EDIT:
updated the getPixel function. the old one outputted crappy span tags so now it looks way better.
 
Last edited:
Newbie Spellweaver
Joined
Apr 9, 2010
Messages
34
Reaction score
0
COPY !! 1> :( lol

Nah, anyway I have a few suggestions...

  1. Make the text white
  2. Use the TD tags instad of span tags for the colour css (save markup)
  3. Suppress errors (with @function() or die();) (I tried a broken image earlier and got thousands of php erros lol)
  4. Get rid of id="at" ?
 
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
COPY !! 1> :( lol

Nah, anyway I have a few suggestions...

  1. Make the text white
  2. Use the TD tags instad of span tags for the colour css (save markup)
  3. Suppress errors (with @function() or die();) (I tried a broken image earlier and got thousands of php erros lol)
  4. Get rid of id="at" ?
about the thousands of errors, just add a check for that and it will get rid of it.

i also put this together in like 2 hours. so there are many errors to come from it.

EDIT:
updated script with all your suggestions
 
Last edited:
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
up-dated the code to look exactly like the picture, except for the transparent pixels.
 
Back
Top