[PHP]Image to HTML

Results 1 to 4 of 4
  1. #1
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    [PHP]Image to HTML

    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 Code:
    <?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->cURLCURLOPT_URL$URL);
            
    curl_setopt($this->cURLCURLOPT_HEADER0);
            
    curl_setopt($this->cURLCURLOPT_RETURNTRANSFERtrue);
            
    curl_setopt($this->cURLCURLOPT_TIMEOUT5);
            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 shitty span tags so now it looks way better.
    Last edited by holthelper; 20-05-10 at 08:13 AM.


  2. #2
    Enthusiast Apolva is offline
    MemberRank
    Apr 2010 Join Date
    37Posts

    Re: [PHP]Image to HTML

    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" ?

  3. #3
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP]Image to HTML

    Quote Originally Posted by Apolva View Post
    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 by holthelper; 23-04-10 at 02:55 AM.

  4. #4
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP]Image to HTML

    up-dated the code to look exactly like the picture, except for the transparent pixels.



Advertisement