Advertisement
Guest User

Convert.php

a guest
Jul 15th, 2013
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2.     $characters = $_POST['characters'];
  3.     $fontsize   = $_POST['fontsize'];
  4.     $img_type   = $_POST['color'];
  5.     $bgcolor    = $_POST['bgcolor'];
  6. ?>
  7. <html>
  8.     <title>Image to text convertor</title>
  9.   <style>
  10.     body{
  11.          background:<?=$bgcolor;?>;
  12.          font-size: <?=$fontsize;?>px;
  13.     }
  14.     .bold{
  15.         color: red;
  16.         font-size: 15px;
  17.         font-weight: bold;
  18.     }
  19.   </style>
  20.   <body>
  21. <?php
  22.     $imagesize = "150";
  23.     $index = 0;
  24.     $path = "images/";
  25.     $allowedExts  = array("jpg", "jpeg", "gif", "png");
  26.     $allowedTypes = array("image/gif", "image/jpeg", "image/jpg", "image/pjpeg","image/png");
  27.     $extension = end(explode(".", $_FILES["image"]["name"]));
  28.  
  29.     $newfilename = time().".".$extension;
  30.  
  31.     $filename = $_FILES["image"]["name"];  
  32.     $filetmp  = $_FILES["image"]["tmp_name"];  
  33.     $filetype = $_FILES["image"]["type"];  
  34.     $filesize = $_FILES["image"]["size"];  
  35.    
  36.     $filesize = number_format(( $filesize / 1024 ) / 1024 , 2);
  37.    
  38.     if(!$_FILES["image"]["error"]){
  39.         if (in_array($filetype,$allowedTypes) && in_array($extension, $allowedExts))
  40.         {
  41.             if(($filesize < 2)) // check if file size less than 2 mb
  42.                 move_uploaded_file($filetmp,"images/" . $newfilename);
  43.             else{
  44.                 echo '<span class="bold">Image is too big</span>';
  45.                 exit;
  46.             }
  47.         }else{
  48.             echo '<span class="bold">Invalid image, Supported formats jpeg, png, gif</span>';
  49.             exit;
  50.         }      
  51.     }
  52.     $thumb = "thumb_".$newfilename;
  53.     imagecreatethumbnail($path.$newfilename,$path.$thumb,$imagesize /2 ,$imagesize /2);
  54.  
  55.     //$thumb = thumbnail_box(imagecreatefromjpeg($path.$newfilename),$path.$thumb, 210, 150);
  56.     //imagedestroy($i);
  57.  
  58.     if(!file_exists($path.$thumb)) {
  59.         echo "File not found";
  60.         exit;
  61.     }
  62.    
  63.     $im = imagecreatefrompng($path.$thumb);
  64.     if(!$im) echo "Cant open image";
  65.     $w = imagesx($im);
  66.     $h = imagesy($im);
  67.     for( $i = 0; $i < $h; $i++ ) {
  68.         for( $j = 0; $j < $w; $j++ ) {      
  69.             //if($j%2 == 0) continue;
  70.             $rgb = imagecolorat($im, $j, $i);
  71.             $a = ($rgb >> 24) & 0xFF;
  72.             $r = ($rgb >> 16) & 0xFF;
  73.             $g = ($rgb >> 8) & 0xFF;
  74.             $b = $rgb & 0xFF;
  75.            
  76.             $pixel = $characters[$index];
  77.  
  78.             if($img_type == "gray"){
  79.                 $gray = round(($r + $g + $b) / 3);
  80.                 //if($gray > 0x7F) $gray = 0xFF;
  81.                 //$gray =  0x00;
  82.                 echo "<span style='color:rgb($gray,$gray,$gray);'>$pixel</span>";
  83.             }else{
  84.                 echo "<span style='color:rgb($r,$g,$b);'>$pixel</span>";
  85.             }
  86.            
  87.             //echo "<div style='background:rgb($r,$g,$b);float:left;width:1px; height:1px;'>@</div>";
  88.             $index++;
  89.             if($index >= count(str_split($characters)))
  90.                 $index = 0;
  91.         }
  92.         echo "<br>";
  93.     }
  94.     //unlink($path.$thumb);
  95.     unlink($path.$newfilename);
  96.  
  97.     function imagecreatethumbnail($file,$output,$max_width = 150,$max_height = 150)
  98.     {
  99.             extract($_POST);
  100.             $img = imagecreatefromstring(file_get_contents($file));
  101.             list($width, $height, $type, $attr) = getimagesize($file);
  102.             if($height > $max_height || $width > $max_width)
  103.             {
  104.                     if($width > $height)
  105.                     {
  106.                             $thumb_width = $max_width;
  107.                             $thumb_height = ceil(($height * $thumb_width)/$width);
  108.                     }
  109.                     else
  110.                     {
  111.                             $thumb_height = $max_height;
  112.                             $thumb_width = ceil(($width * $thumb_height)/$height);
  113.                     }
  114.             } else {
  115.                     $thumb_width = $width;
  116.                     $thumb_height = $height;
  117.             }
  118.             $thumb_width = $thumb_width + ($thumb_width * ($fontsize / 6) );
  119.             imagesavealpha($img,true);
  120.             $thumb = imagecreatetruecolor($thumb_width,$thumb_height);
  121.             imagesavealpha($thumb,true);
  122.             imagealphablending($thumb,false);
  123.             imagecopyresampled($thumb,$img,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
  124.             $return = imagepng($thumb,$output);
  125.             imagedestroy($img);
  126.             imagedestroy($thumb);
  127.             return $return;
  128.     }
  129.  
  130.  
  131. ?>
  132. </body>
  133. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement