Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/topic.php on line 737
Форумы портала PHP.SU :: Помощь с капчей

 PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи


 Страниц (1): [1]   

> Без описания
ВэйДлин
Отправлено: 10 Декабря, 2011 - 13:53:54
Post Id


Гость


Покинул форум
Сообщений всего: 84
Дата рег-ции: Май 2011  


Помог: 0 раз(а)




Никогда не работал с изображениями в php, помогите мне убрать фон у капчи (сделать прозрачным)
PHP:
скопировать код в буфер обмена
  1. $font=imagecreatefrompng(dirname(__FILE__).DIRECTORY_SEPARATOR."font.png");
  2. imagealphablending($font, true);
  3. $fontfile_width=imagesx($font);
  4. $fontfile_height=imagesy($font)-1;
  5. $font_metrics=array();
  6. $symbol=0;
  7. $reading_symbol=false;
  8.  
  9. $alphabet = "0123456789";
  10. $alphabet_length = strlen($alphabet);
  11.  
  12. for($i=0;$i<$fontfile_width && $symbol<$alphabet_length;$i++){
  13.     $transparent = (imagecolorat($font, $i, 0) >> 24) == 127;
  14.  
  15.     if(!$reading_symbol && !$transparent){
  16.         $font_metrics[$alphabet{$symbol}]=array('start'=>$i);
  17.         $reading_symbol=true;
  18.         continue;
  19.     }
  20.  
  21.     if($reading_symbol && $transparent){
  22.         $font_metrics[$alphabet{$symbol}]['end']=$i;
  23.         $reading_symbol=false;
  24.         $symbol++;
  25.         continue;
  26.     }
  27. }
  28.  
  29. $image = imagecreatetruecolor(165,60);
  30. $width = ImageSX($image);
  31. $height = ImageSY($image);
  32.  
  33. imagealphablending($image, true);
  34.  
  35. $background_color = array(255,255,255);
  36. $foreground_color = array(70,70,70);
  37.  
  38. $white=imagecolorallocate($image, 255, 255, 255);
  39. $black=imagecolorallocate($image, 128, 128, 0);
  40.  
  41. imagefilledrectangle($image, 0, 0, ImageSX($image)-1, ImageSY($image)-1, $white);
  42.  
  43. $string = '';
  44. $x = 5;
  45. $y = 5;
  46.  
  47. for ($i=0;$i<6;$i++) {
  48.     $char = rand(0,9);
  49.     $string .= $char;
  50.     if ($x > 15) $x = $x - 2;
  51.     imagecopy($image,$font,$x,$y,$font_metrics[$char]['start'],1,$font_metrics[$char]['end']-$font_metrics[$char]['start'],42);
  52.     $x += $font_metrics[$char]['end']-$font_metrics[$char]['start'];
  53. }
  54.  
  55. for ($i=0;$i<10;$i++) {
  56.     ImageSmoothAlphaLine($image,Rand(5,$width*0.5),Rand(5,$height-5),rand($width*0.5,$width-5),rand(5,$height-5),255,255,255);
  57. }
  58.  
  59. for ($i=0;$i<2;$i++) {
  60.     ImageSmoothAlphaLine($image,rand(5,10),Rand(5,$height-5),rand($width-10,$width-5),rand(5,$height-5),0,0,0);
  61. }
  62.  
  63. $center=$x/2;
  64.  
  65. $img2=imagecreatetruecolor(ImageSX($image), ImageSY($image));
  66.  
  67.  
  68. $rand1=mt_rand(750000,1200000)/10000000;
  69. $rand2=mt_rand(750000,1200000)/10000000;
  70. $rand3=mt_rand(750000,1200000)/10000000;
  71. $rand4=mt_rand(750000,1200000)/10000000;
  72.  
  73. $rand5=mt_rand(0,3141592)/500000;
  74. $rand6=mt_rand(0,3141592)/500000;
  75. $rand7=mt_rand(0,3141592)/500000;
  76. $rand8=mt_rand(0,3141592)/500000;
  77.  
  78. $rand9=mt_rand(330,420)/110;
  79. $rand10=mt_rand(330,450)/110;
  80.  
  81.  
  82. for($x=0;$x<ImageSX($image);$x++){
  83.     for($y=0;$y<ImageSY($image);$y++){
  84.         $sx=$x+(sin($x*$rand1+$rand5)+sin($y*$rand3+$rand6))*$rand9-$width/2+$center+1;
  85.         $sy=$y+(sin($x*$rand2+$rand7)+sin($y*$rand4+$rand8))*$rand10;
  86.         if($sx<0 || $sy<0 || $sx>=$width-1 || $sy>=$height-1){
  87.             $color=255;
  88.             $color_x=255;
  89.             $color_y=255;
  90.             $color_xy=255;
  91.         }else{
  92.             $color=imagecolorat($image, $sx, $sy) & 0xFF;
  93.             $color_x=imagecolorat($image, $sx+1, $sy) & 0xFF;
  94.             $color_y=imagecolorat($image, $sx, $sy+1) & 0xFF;
  95.             $color_xy=imagecolorat($image, $sx+1, $sy+1) & 0xFF;
  96.         }
  97.         if($color==0 && $color_x==0 && $color_y==0 && $color_xy==0){
  98.             $newred=$foreground_color[0];
  99.             $newgreen=$foreground_color[1];
  100.             $newblue=$foreground_color[2];
  101.         }else if($color==255 && $color_x==255 && $color_y==255 && $color_xy==255){
  102.             $newred=$background_color[0];
  103.             $newgreen=$background_color[1];
  104.             $newblue=$background_color[2];
  105.         }else{
  106.             $frsx=$sx-floor($sx);
  107.             $frsy=$sy-floor($sy);
  108.             $frsx1=1-$frsx;
  109.             $frsy1=1-$frsy;
  110.             $newcolor=(
  111.                 $color*$frsx1*$frsy1+
  112.                 $color_x*$frsx*$frsy1+
  113.                 $color_y*$frsx1*$frsy+
  114.                 $color_xy*$frsx*$frsy);
  115.  
  116.             if($newcolor>255) $newcolor=255;
  117.  
  118.             $newcolor=$newcolor/255;
  119.             $newcolor0=1-$newcolor;
  120.             $newred=$newcolor0*$foreground_color[0]+$newcolor*$background_color[0];
  121.             $newgreen=$newcolor0*$foreground_color[1]+$newcolor*$background_color[1];
  122.             $newblue=$newcolor0*$foreground_color[2]+$newcolor*$background_color[2];
  123.         }
  124.         imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newred, $newgreen, $newblue));
  125.     }
  126. }
  127. $_SESSION['sec_code_session'] = $string;
  128.  
  129. $watermark = imagecreatefromgif(dirname(__FILE__).DIRECTORY_SEPARATOR.'watermark.gif');
  130. imagealphablending($watermark, true);
  131. $wm_width=imagesx($watermark);
  132. $wm_height=imagesy($watermark);
  133. $wm_pos_x = ((160) - ($wm_width));
  134.  
  135. imagecopy($img2, $watermark, $wm_pos_x, 1, 0, 0, $wm_width, $wm_height);
  136.  
  137. header("content-type: image/png");
  138. imagepng($img2);
  139.  
  140. imagedestroy($watermark);
  141. imagedestroy($img2);
  142.  
  143. function ImageSmoothAlphaLine ($image, $x1, $y1, $x2, $y2, $r, $g, $b, $alpha=0) {
  144.   $icr = $r;
  145.   $icg = $g;
  146.   $icb = $b;
  147.   $dcol = imagecolorallocatealpha($image, $icr, $icg, $icb, $alpha);
  148.  
  149.   if ($y1 == $y2 || $x1 == $x2)
  150.     imageline($image, $x1, $y2, $x1, $y2, $dcol);
  151.   else {
  152.     $m = ($y2 - $y1) / ($x2 - $x1);
  153.     $b = $y1 - $m * $x1;
  154.  
  155.     if (abs ($m) <2) {
  156.       $x = min($x1, $x2);
  157.       $endx = max($x1, $x2) + 1;
  158.  
  159.       while ($x < $endx) {
  160.         $y = $m * $x + $b;
  161.         $ya = ($y == floor($y) ? 1: $y - floor($y));
  162.         $yb = ceil($y) - $y;
  163.  
  164.         $trgb = ImageColorAt($image, $x, floor($y));
  165.         $tcr = ($trgb >> 16) & 0xFF;
  166.         $tcg = ($trgb >> 8) & 0xFF;
  167.         $tcb = $trgb & 0xFF;
  168.         imagesetpixel($image, $x, floor($y), imagecolorallocatealpha($image, ($tcr * $ya + $icr * $yb), ($tcg * $ya + $icg * $yb), ($tcb * $ya + $icb * $yb), $alpha));
  169.  
  170.         $trgb = ImageColorAt($image, $x, ceil($y));
  171.         $tcr = ($trgb >> 16) & 0xFF;
  172.         $tcg = ($trgb >> 8) & 0xFF;
  173.         $tcb = $trgb & 0xFF;
  174.         imagesetpixel($image, $x, ceil($y), imagecolorallocatealpha($image, ($tcr * $yb + $icr * $ya), ($tcg * $yb + $icg * $ya), ($tcb * $yb + $icb * $ya), $alpha));
  175.  
  176.         $x++;
  177.       }
  178.     } else {
  179.       $y = min($y1, $y2);
  180.       $endy = max($y1, $y2) + 1;
  181.  
  182.       while ($y < $endy) {
  183.         $x = ($y - $b) / $m;
  184.         $xa = ($x == floor($x) ? 1: $x - floor($x));
  185.         $xb = ceil($x) - $x;
  186.  
  187.         $trgb = ImageColorAt($image, floor($x), $y);
  188.         $tcr = ($trgb >> 16) & 0xFF;
  189.         $tcg = ($trgb >> 8) & 0xFF;
  190.         $tcb = $trgb & 0xFF;
  191.         imagesetpixel($image, floor($x), $y, imagecolorallocatealpha($image, ($tcr * $xa + $icr * $xb), ($tcg * $xa + $icg * $xb), ($tcb * $xa + $icb * $xb), $alpha));
  192.  
  193.         $trgb = ImageColorAt($image, ceil($x), $y);
  194.         $tcr = ($trgb >> 16) & 0xFF;
  195.         $tcg = ($trgb >> 8) & 0xFF;
  196.         $tcb = $trgb & 0xFF;
  197.         imagesetpixel ($image, ceil($x), $y, imagecolorallocatealpha($image, ($tcr * $xb + $icr * $xa), ($tcg * $xb + $icg * $xa), ($tcb * $xb + $icb * $xa), $alpha));
  198.  
  199.         $y ++;
  200.       }
  201.     }
  202.   }
  203. }
 
 Top
Страниц (1): [1]
Сейчас эту тему просматривают: 0 (гостей: 0, зарегистрированных: 0)
« Вопросы новичков »


Все гости форума могут просматривать этот раздел.
Только зарегистрированные пользователи могут создавать новые темы в этом разделе.
Только зарегистрированные пользователи могут отвечать на сообщения в этом разделе.
 



Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB