function generateImage($token) { $iFont = 5; // Font ID $iSpacing = 2; // Spacing between characters $iDisplacement = 5; // Vertical chracter displacement // Establish font metric and image size $iCharWidth = ImageFontWidth($iFont); $iCharHeight = ImageFontHeight($iFont); $iWidth = strlen($token) * ($iCharWidth + $iSpacing); $iHeight = $iCharHeight + 2 * $iDisplacement; // Create the image $pic = ImageCreate($iWidth, $iHeight); // Allocate a background and foreground colour $col = array('white' => ImageColorAllocate($pic, 255, 255, 255), 'blue' => ImageColorAllocate($pic, 45, 45, 100), 'green' => ImageColorAllocate($pic, 45, 100, 45), 'red' => ImageColorAllocate($pic, 100, 45, 45), 'purple' => ImageColorAllocate($pic, 100, 45, 100), 'grey' => ImageColorAllocate($pic, 225, 225, 225), 'grey2' => ImageColorAllocate($pic, 200, 200, 200)); for ($x = 0; $x < $iWidth; $x += 2) { for ($y = 0; $y < $iHeight; $y += 2) { ImageSetPixel($pic, $x, $y, $col['grey']); } } $iX = 1; for ($i = 0; $i < strlen($token); $i++) { ImageChar($pic, $iFont - 1, $iX, $iDisplacement - rand(-$iDisplacement, $iDisplacement), $token[$i], $col['grey2']); $iX += $iCharWidth + $iSpacing; } $iX = 2; $c = array('blue', 'green', 'red', 'purple'); for ($i = 0; $i < strlen($token); $i++) { $colour = $c[rand(0, count($c) - 1)]; ImageChar($pic, $iFont, $iX, $iDisplacement - rand(-$iDisplacement, $iDisplacement), $token[$i], $col[$colour]); $iX += $iCharWidth + $iSpacing; } for ($x = 1; $x < $iWidth; $x += 4) { for ($y = 1; $y < $iHeight; $y += 4) { ImageSetPixel($pic, $x, $y, $col['white']); } } // Draw some lines for ($i = 0; $i < 4; $i++) { ImageLine($pic, rand(0, $iWidth / 2), rand(0, $iHeight / 2), rand($iWidth / 2, $iWidth), rand($iHeight / 2, $iHeight), $col['white']); } ob_start(); if (function_exists('imagejpeg')) { ImageJPEG($pic); } elseif (function_exists('imagepng')) { ImagePNG($pic); } else { ob_end_clean(); return false; } $data = ob_get_contents(); ob_end_clean(); ImageDestroy($pic); return $data; }
function UnsharpMask($img, $amount, $radius, $threshold) { //////////////////////////////////////////////////////////////////////////////////////////////// //// //// p h p U n s h a r p M a s k //// //// Unsharp mask algorithm by Torstein H�nsi 2003. //// thoensi_at_netcom_dot_no. //// Please leave this notice. //// /////////////////////////////////////////////////////////////////////////////////////////////// if ($amount > 500) { $amount = 500; } $amount = $amount * 0.016; if ($radius > 50) { $radius = 50; } $radius = $radius * 2; if ($threshold > 255) { $threshold = 255; } $radius = abs(round($radius)); // Only integers make sense. if ($radius == 0) { return $img; imagedestroy($img); break; } $w = imagesx($img); $h = imagesy($img); $imgCanvas = imagecreatetruecolor($w, $h); $imgCanvas2 = imagecreatetruecolor($w, $h); $imgBlur = imagecreatetruecolor($w, $h); $imgBlur2 = imagecreatetruecolor($w, $h); imagecopy($imgCanvas, $img, 0, 0, 0, 0, $w, $h); imagecopy($imgCanvas2, $img, 0, 0, 0, 0, $w, $h); for ($i = 0; $i < $radius; $i++) { imagecopy($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1); // up left imagecopymerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down right imagecopymerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // up right imagecopymerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // right imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20); // up imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // down imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); // center imagecopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h); imagecopy($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 20); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 16.666667); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); imagecopy($imgCanvas2, $imgBlur2, 0, 0, 0, 0, $w, $h); } for ($x = 0; $x < $w; $x++) { // each row for ($y = 0; $y < $h; $y++) { // each pixel $rgbOrig = ImageColorAt($imgCanvas2, $x, $y); $rOrig = $rgbOrig >> 16 & 0xff; $gOrig = $rgbOrig >> 8 & 0xff; $bOrig = $rgbOrig & 0xff; $rgbBlur = ImageColorAt($imgCanvas, $x, $y); $rBlur = $rgbBlur >> 16 & 0xff; $gBlur = $rgbBlur >> 8 & 0xff; $bBlur = $rgbBlur & 0xff; $rNew = abs($rOrig - $rBlur) >= $threshold ? max(0, min(255, $amount * ($rOrig - $rBlur) + $rOrig)) : $rOrig; $gNew = abs($gOrig - $gBlur) >= $threshold ? max(0, min(255, $amount * ($gOrig - $gBlur) + $gOrig)) : $gOrig; $bNew = abs($bOrig - $bBlur) >= $threshold ? max(0, min(255, $amount * ($bOrig - $bBlur) + $bOrig)) : $bOrig; if ($rOrig != $rNew || $gOrig != $gNew || $bOrig != $bNew) { $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew); ImageSetPixel($img, $x, $y, $pixCol); } } } imagedestroy($imgCanvas); imagedestroy($imgCanvas2); imagedestroy($imgBlur); imagedestroy($imgBlur2); return $img; }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = imagecreatetruecolor($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, 255, 0, 255); $col[1] = ImageColorAllocate($base_image, 0, 0, 0); imagecolortransparent($base_image, $col[0]); imagealphablending($base_image, true); imagesavealpha($base_image, true); // imagefill($base_image, 0, 0, $col[0]); imagefill($base_image, 0, 0, 0x7fff0000); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xffffff, $fore_color = 0x0) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = ImageCreate($imgW, $imgH); // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...) $r1 = round(($fore_color & 0xff0000) >> 16, 5); $b1 = round(($fore_color & 0xff00) >> 8, 5); $g1 = round($fore_color & 0xff, 5); // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...) $r2 = round(($back_color & 0xff0000) >> 16, 5); $b2 = round(($back_color & 0xff00) >> 8, 5); $g2 = round($back_color & 0xff, 5); $col[0] = ImageColorAllocate($base_image, $r2, $b2, $g2); $col[1] = ImageColorAllocate($base_image, $r1, $b1, $g1); imagefill($base_image, 0, 0, $col[0]); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $wh = false) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = ImageCreate($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, 255, 255, 255); $col[1] = ImageColorAllocate($base_image, 0, 0, 0); imagefill($base_image, 0, 0, $col[0]); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $www = $imgW * $pixelPerPoint; $hhh = $imgH * $pixelPerPoint; if (is_array($wh)) { $www = $wh[0]; $hhh = $wh[1]; } $target_image = ImageCreate($www, $hhh); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $www, $hhh, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
/** * * long description * @global object * @param object $dst_img * @param object $src_img * @param int $dst_x * @param int $dst_y * @param int $src_x * @param int $src_y * @param int $dst_w * @param int $dst_h * @param int $src_w * @param int $src_h * @return bool * @todo Finish documenting this function */ function ImageCopyBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { global $CFG; if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) { return ImageCopyResampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } $totalcolors = imagecolorstotal($src_img); for ($i = 0; $i < $totalcolors; $i++) { if ($colors = ImageColorsForIndex($src_img, $i)) { ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); } } $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = $scaleX / 2.0; $scaleY2 = $scaleY / 2.0; for ($j = 0; $j < $dst_h; $j++) { $sY = $j * $scaleY; for ($i = 0; $i < $dst_w; $i++) { $sX = $i * $scaleX; $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY + $scaleY2)); $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY)); $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY + $scaleY2)); $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY)); $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4); $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4); $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4); $color = ImageColorClosest($dst_img, $red, $green, $blue); ImageSetPixel($dst_img, $i + $dst_x, $j + $dst_y, $color); } } }
/** * $IMG_WIDTH = 100; //图像宽度 $IMG_HEIGHT = 23; //图像高度 $SESSION_VDCODE = ''; //Session变量名称 * $operator = '+-'; //运算符 */ public static function display($IMG_WIDTH = 70, $IMG_HEIGHT = 25, $SESSION_VDCODE = 'vdcode', $operator = '+') { $session_id = session_id(); if (empty($session_id)) { @session_start(); } $code = array(); $num1 = $code[] = mt_rand(1, 9); $code[] = '+'; //{mt_rand(0,1)}; $num2 = $code[] = mt_rand(1, 9); $codestr = implode('', $code); //eval("\$result = ".implode('',$code).";"); $code[] = '='; $_SESSION[$SESSION_VDCODE] = (int) $num1 + (int) $num2; $img = ImageCreate($IMG_WIDTH, $IMG_HEIGHT); ImageColorAllocate($img, mt_rand(230, 250), mt_rand(230, 250), mt_rand(230, 250)); $color = ImageColorAllocate($img, 0, 0, 0); $offset = 0; foreach ($code as $char) { $offset += 15; $txtcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 150), mt_rand(0, 255)); ImageChar($img, mt_rand(3, 5), $offset, mt_rand(1, 5), $char, $txtcolor); } for ($i = 0; $i < 100; $i++) { $pxcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageSetPixel($img, mt_rand(0, $IMG_WIDTH), mt_rand(0, $IMG_HEIGHT), $pxcolor); } header('Content-type: image/png'); ImagePng($img); exit; }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $color = array()) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = ImageCreate($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, 255, 255, 255); if ($color) { $col[1] = ImageColorAllocate($base_image, $color[0], $color[1], $color[2]); } else { $col[1] = ImageColorAllocate($base_image, 0, 0, 0); } imagefill($base_image, 0, 0, $col[0]); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
function ApplyMask(&$image, $mask) { // Create copy of mask as mask may not be the same size as image $mask_resized = ImageCreateTrueColor(ImageSX($image), ImageSY($image)); ImageCopyResampled($mask_resized, $mask, 0, 0, 0, 0, ImageSX($image), ImageSY($image), ImageSX($mask), ImageSY($mask)); // Create working temp $mask_blendtemp = ImageCreateTrueColor(ImageSX($image), ImageSY($image)); $color_background = ImageColorAllocate($mask_blendtemp, 0, 0, 0); ImageFilledRectangle($mask_blendtemp, 0, 0, ImageSX($mask_blendtemp), ImageSY($mask_blendtemp), $color_background); // switch off single color alph and switch on full alpha channel ImageAlphaBlending($mask_blendtemp, false); ImageSaveAlpha($mask_blendtemp, true); // loop the entire image and set pixels, this will be slow for large images for ($x = 0; $x < ImageSX($image); $x++) { for ($y = 0; $y < ImageSY($image); $y++) { $RealPixel = GetPixelColor($image, $x, $y); $MaskPixel = GrayscalePixel(GetPixelColor($mask_resized, $x, $y)); $MaskAlpha = 127 - Floor($MaskPixel['red'] / 2) * (1 - $RealPixel['alpha'] / 127); $newcolor = ImageColorAllocateAlpha($mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha); ImageSetPixel($mask_blendtemp, $x, $y, $newcolor); } } // don't need the mask copy anymore ImageDestroy($mask_resized); // switch off single color alph and switch on full alpha channel ImageAlphaBlending($image, false); ImageSaveAlpha($image, true); // replace the image with the blended temp ImageCopy($image, $mask_blendtemp, 0, 0, 0, 0, ImageSX($mask_blendtemp), ImageSY($mask_blendtemp)); ImageDestroy($mask_blendtemp); }
function show_gd_img($content = "") { $content = ' ' . preg_replace("/(\\w)/", "\\1 ", $content) . ' '; $gd_version = 2; @header("Content-Type: image/jpeg"); $tmp_x = 140; $tmp_y = 20; $image_x = 210; $image_y = 65; $circles = 3; if ($gd_version == 1) { $tmp = imagecreate($tmp_x, $tmp_y); $im = imagecreate($image_x, $image_y); } else { $tmp = imagecreatetruecolor($tmp_x, $tmp_y); $im = imagecreatetruecolor($image_x, $image_y); } $white = ImageColorAllocate($tmp, 255, 255, 255); $black = ImageColorAllocate($tmp, 0, 0, 0); $grey = ImageColorAllocate($tmp, 210, 210, 210); imagefill($tmp, 0, 0, $white); for ($i = 1; $i <= $circles; $i++) { $values = array(0 => rand(0, $tmp_x - 10), 1 => rand(0, $tmp_y - 3), 2 => rand(0, $tmp_x - 10), 3 => rand(0, $tmp_y - 3), 4 => rand(0, $tmp_x - 10), 5 => rand(0, $tmp_y - 3), 6 => rand(0, $tmp_x - 10), 7 => rand(0, $tmp_y - 3), 8 => rand(0, $tmp_x - 10), 9 => rand(0, $tmp_y - 3), 10 => rand(0, $tmp_x - 10), 11 => rand(0, $tmp_y - 3)); $randomcolor = imagecolorallocate($tmp, rand(100, 255), rand(100, 255), rand(100, 255)); imagefilledpolygon($tmp, $values, 6, $randomcolor); } imagestring($tmp, 5, 0, 2, $content, $black); //----------------------------------------- // Distort by resizing //----------------------------------------- imagecopyresized($im, $tmp, 0, 0, 0, 0, $image_x, $image_y, $tmp_x, $tmp_y); imagedestroy($tmp); $white = ImageColorAllocate($im, 255, 255, 255); $black = ImageColorAllocate($im, 0, 0, 0); $grey = ImageColorAllocate($im, 100, 100, 100); $random_pixels = $image_x * $image_y / 10; for ($i = 0; $i < $random_pixels; $i++) { ImageSetPixel($im, rand(0, $image_x), rand(0, $image_y), $black); } $no_x_lines = ($image_x - 1) / 5; for ($i = 0; $i <= $no_x_lines; $i++) { // X lines ImageLine($im, $i * $no_x_lines, 0, $i * $no_x_lines, $image_y, $grey); // Diag lines ImageLine($im, $i * $no_x_lines, 0, $i * $no_x_lines + $no_x_lines, $image_y, $grey); } $no_y_lines = ($image_y - 1) / 5; for ($i = 0; $i <= $no_y_lines; $i++) { ImageLine($im, 0, $i * $no_y_lines, $image_x, $i * $no_y_lines, $grey); } ImageJPEG($im); ImageDestroy($im); exit; }
function IndexAction() { $str = "23456789ABCDEFGHJKMNPQRSTUVWXYZ"; $code_str = str_shuffle($str); $code = str_split(substr($code_str, 0, 4)); $_SESSION['VerifyCode'] = strtolower(implode('', $code)); $width = 115; $height = 29; $im = ImageCreate($width, $height); // 创建图形 ImageColorAllocate($im, 255, 255, 255); // 填充背景颜色为白色 // 用淡色给图形添加杂色 for ($i = 0; $i < 100; $i++) { $pxcolor = ImageColorAllocate($im, 230, 104, 66); ImageSetPixel($im, mt_rand(0, $width), mt_rand(0, $height), $pxcolor); } // 用深色调绘制边框 $bordercolor = ImageColorAllocate($im, 255, 255, 255); ImageRectangle($im, 0, 0, $width - 1, $height - 1, $bordercolor); $offset = rand(10, 30); $font = array('View/font/UniversityRomanStd.otf'); foreach ($code as $char) { $textcolor = ImageColorAllocate($im, 230, 104, 106); shuffle($font); imagettftext($im, 22, rand(-20, 40), $offset, 26, $textcolor, $font[0], $char); $offset += $width / 5 - rand(0, 2); } $code_str = str_shuffle($str); $code = str_split(substr($code_str, 0, 5)); // 干扰字符 $offset = rand(10, 30); foreach ($code as $char) { $textcolor = ImageColorAllocate($im, 230, 104, 66); shuffle($font); imagettftext($im, 8, rand(-20, 40), $offset, 26, $textcolor, $font[0], $char); $offset += rand(5, 10); } // 禁止缓存 header("pragma:no-cache\r\n"); header("Cache-Control:no-cache\r\n"); header("Expires:0\r\n"); if (ImageTypes() & IMG_PNG) { header('Content-Type:image/png'); ImagePNG($im); } elseif (ImageTypes() & IMG_JPEG) { header('Content-Type:image/jpeg'); ImageJPEG($im); } else { header('Content-Type:image/gif'); ImageGif($im); } }
function image_resize_sepia(&$src_im, $quality = 60) { $src_x = ceil(imagesx($src_im)); $src_y = ceil(imagesy($src_im)); $dst_x = $src_x; $dst_y = $src_y; $dst_im = ImageCreateTrueColor($dst_x, $dst_y); // Benötigt PHP > 4.3.2 if (function_exists('imageantialias')) { imageantialias($dst_im, TRUE); } ImageCopyResampled($dst_im, $src_im, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y); // Change style of image pixelwise for ($y = 0; $y < $dst_y; $y++) { for ($x = 0; $x < $dst_x; $x++) { $col = ImageColorAt($dst_im, $x, $y); $r = ($col & 0xff0000) >> 16; $g = ($col & 0xff00) >> 8; $b = $col & 0xff; $grey = (min($r, $g, $b) + max($r, $g, $b)) / 2; // Boost colors $boost = 1.2; $boostborder = 250; for ($i = 0; $i < 25; $i++) { if ($grey > $boostborder) { $grey = $grey * $boost; break; } $boost -= 0.01; $boostborder -= 10; } // Set sepia palette $r = $grey * 1.01; $g = $grey * 0.98; $b = $grey * 0.9; // Correct max values $r = $r > 255 ? 255 : $r; $g = $g > 255 ? 255 : $g; $b = $b > 255 ? 255 : $b; $col = ImageColorAllocate($dst_im, $r, $g, $b); ImageSetPixel($dst_im, $x, $y, $col); } } $src_im = $dst_im; }
public function createRainGraph($hrf_list) { $image = imagecreate($this->x_size, $this->y_size); $color[0] = imagecolorallocate($image, 250, 250, 250); //$no_data_color $color[1] = imagecolorallocate($image, 255, 200, 0); //$sun_color $color[2] = imagecolorallocate($image, 156, 156, 255); //$low_rain_color $color[3] = imagecolorallocate($image, 0, 0, 255); //$moderate_rain_color $color[4] = imagecolorallocate($image, 0, 0, 128); //$heavy_rain_color foreach ($hrf_list as $hrf) { $date = DateTime::createFromFormat('Ymd', $hrf->getDate()); $x = date('z', $date->format('U')) - 1; $y = $this->y_size - (60 * $hrf->getHour() + $hrf->getMinutes()) / 5 - 1; ImageSetPixel($image, $x, $y, $color[$hrf->getValues()[0]]); } imagepng($image, dirname(__FILE__) . "/../../year_rain_graph.png"); }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $QR_COLOR, $QR_COLORBG) { $h = count($frame); $w = strlen(trim($frame[0])); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = ImageCreate($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, $QR_COLORBG["R"], $QR_COLORBG["G"], $QR_COLORBG["B"]); $col[1] = ImageColorAllocate($base_image, $QR_COLOR["R"], $QR_COLOR["G"], $QR_COLOR["B"]); imagefill($base_image, 0, 0, $col[0]); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
public static function make() { $sessionvar = 'vdcode'; //Session变量名称 $width = 150; //图像宽度 $height = 20; //图像高度 $ans = mt_rand(0, 9); $p1 = mt_rand(1, 9); if ($p1 <= $ans) { $p2 = $ans - $p1; $operator = '+'; } else { $p2 = $p1 - $ans; $operator = '-'; } $code = array(); $code[] = $p1; $code[] = $operator; $code[] = $p2; $code[] = '='; $code[] = $ans; $_SESSION[$sessionvar] = $ans; $img = ImageCreate($width, $height); ImageColorAllocate($img, mt_rand(230, 250), mt_rand(230, 250), mt_rand(230, 250)); $color = ImageColorAllocate($img, 0, 0, 0); $offset = 0; foreach ($code as $char) { $offset += 20; $txtcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 150), mt_rand(0, 255)); ImageChar($img, mt_rand(3, 5), $offset, mt_rand(3, 5), $char, $txtcolor); } for ($i = 0; $i < 100; $i++) { $pxcolor = ImageColorAllocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageSetPixel($img, mt_rand(0, $width), mt_rand(0, $height), $pxcolor); } return ImagePng($img); }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = imagecreate($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, QRImage::$black[0], QRImage::$black[1], QRImage::$black[2]); $col[1] = ImageColorAllocate($base_image, QRImage::$white[0], QRImage::$white[1], QRImage::$white[2]); imagefill($base_image, 0, 0, $col[0]); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $target_image = imagecreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
/** * @param $dst_img * @param $src_img * @param $dst_x * @param $dst_y * @param $src_x * @param $src_y * @param $dst_w * @param $dst_h * @param $src_w * @param $src_h */ function ImageCopyResampleBicubic(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { $palsize = ImageColorsTotal($src_img); for ($i = 0; $i < $palsize; $i++) { // get palette. $colors = ImageColorsForIndex($src_img, $i); ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); } $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = (int) ($scaleX / 2); $scaleY2 = (int) ($scaleY / 2); for ($j = $src_y; $j < $dst_h; $j++) { $sY = (int) ($j * $scaleY); $y13 = $sY + $scaleY2; for ($i = $src_x; $i < $dst_w; $i++) { $sX = (int) ($i * $scaleX); $x34 = $sX + $scaleX2; $color1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $sX, $y13)); $color2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $sX, $sY)); $color3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $x34, $y13)); $color4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $x34, $sY)); $red = ($color1['red'] + $color2['red'] + $color3['red'] + $color4['red']) / 4; $green = ($color1['green'] + $color2['green'] + $color3['green'] + $color4['green']) / 4; $blue = ($color1['blue'] + $color2['blue'] + $color3['blue'] + $color4['blue']) / 4; ImageSetPixel($dst_img, $i + $dst_x - $src_x, $j + $dst_y - $src_y, ImageColorClosest($dst_img, $red, $green, $blue)); } } }
function _ImageCreateFromBMP($filename) { // 序章:以二進位模式開啟檔案流 if (!($f1 = fopen($filename, 'rb'))) { return FALSE; } // 第一步:讀取BMP檔頭 $FILE = unpack('vfile_type/Vfile_size/Vreserved/Vbitmap_offset', fread($f1, 14)); if ($FILE['file_type'] != 19778) { return FALSE; } // BM // 第二步:讀取BMP資訊 // 僅支援BITMAPINFOHEADER,不支援BITMAPV4HEADER及BITMAPV5HEADER $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel/Vcompression/Vsize_bitmap/Vhoriz_resolution/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40)); $BMP['colors'] = pow(2, $BMP['bits_per_pixel']); if ($BMP['size_bitmap'] == 0) { $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset']; } $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel'] / 8; $BMP['decal'] = $BMP['width'] * $BMP['bytes_per_pixel'] / 4; $BMP['decal'] -= floor($BMP['width'] * $BMP['bytes_per_pixel'] / 4); $BMP['decal'] = 4 - 4 * $BMP['decal']; if ($BMP['decal'] == 4) { $BMP['decal'] = 0; } // 第三步:讀取色盤資訊 $PALETTE = array(); if ($BMP['colors'] <= 256 || $BMP['compression'] == 3) { if ($BMP['compression'] == 3) { // BI_BITFIELDS $PALETTE = unpack('V3', fread($f1, 12)); } else { $PALETTE = unpack('V' . $BMP['colors'], fread($f1, $BMP['colors'] * 4)); } } if ($BMP['compression'] == 3) { // BI_BITFIELDS $mask = array(array($PALETTE[1], $PALETTE[2], $PALETTE[3]), array($this->_GetRightShiftCount($PALETTE[1]), $this->_GetRightShiftCount($PALETTE[2]), $this->_GetRightShiftCount($PALETTE[3])), array($this->_GetLeftShiftCount($PALETTE[1]), $this->_GetLeftShiftCount($PALETTE[2]), $this->_GetLeftShiftCount($PALETTE[3]))); } else { if ($BMP['colors'] == 65536) { $mask = array(array(0x7c00, 0x3e0, 0x1f), array(10, 5, 0), array(3, 3, 3)); } } // 第四步:關閉檔案,變換每一個畫素 $IMG = fread($f1, $BMP['size_bitmap']); fclose($f1); $VIDE = chr(0); $res = ImageCreateTrueColor($BMP['width'], $BMP['height']); $P = 0; $Y = $BMP['height'] - 1; if ($BMP['compression'] == 1 && $BMP['colors'] == 256) { // BI_RLE8 $imgDataLen = strlen($IMG); $RLEData = ''; while (true) { $prefix = ord(substr($IMG, floor($P++), 1)); $suffix = ord(substr($IMG, floor($P++), 1)); if ($prefix == 0 && $suffix == 1) { break; } // end of RLE stream if ($P >= $imgDataLen) { break; } while (!($prefix == 0 && $suffix == 0)) { // ! end of RLE line if ($prefix == 0) { // Command $RLEData .= substr($IMG, floor($P), $suffix); $P += $suffix; if ($P % 2 == 1) { $P++; } } elseif ($prefix > 0) { // Repeat for ($r = 0; $r < $prefix; $r++) { $RLEData .= chr($suffix); } } $prefix = ord(substr($IMG, floor($P++), 1)); $suffix = ord(substr($IMG, floor($P++), 1)); } for ($X = 0; $X < strlen($RLEData); $X++) { // Write ImageSetPixel($res, $X, $Y, $PALETTE[ord($RLEData[$X]) + 1]); } $RLEData = ''; $Y--; } } elseif ($BMP['compression'] == 2 && $BMP['colors'] == 16) { // BI_RLE4 $imgDataLen = strlen($IMG); $RLEData = ''; while (true) { $prefix = ord(substr($IMG, floor($P++), 1)); $suffix = ord(substr($IMG, floor($P++), 1)); if ($prefix == 0 && $suffix == 1) { break; } // end of RLE stream if ($P >= $imgDataLen) { break; } while (!($prefix == 0 && $suffix == 0)) { // ! end of RLE line if ($prefix == 0) { // Command for ($h = 0; $h < $suffix; $h++) { $COLOR = ord(substr($IMG, floor($P), 1)); $RLEData .= $h % 2 == 0 ? chr($COLOR >> 4) : chr($COLOR & 0xf); $P += $BMP['bytes_per_pixel']; } $P = ceil($P); if ($P % 2 == 1) { $P++; } } elseif ($prefix > 0) { // Repeat for ($r = 0; $r < $prefix; $r++) { $RLEData .= $r % 2 == 0 ? chr($suffix >> 4) : chr($suffix & 0xf); } } $prefix = ord(substr($IMG, floor($P++), 1)); $suffix = ord(substr($IMG, floor($P++), 1)); } for ($X = 0; $X < strlen($RLEData); $X++) { // Write ImageSetPixel($res, $X, $Y, $PALETTE[ord($RLEData[$X]) + 1]); } $RLEData = ''; $Y--; } } else { while ($Y >= 0) { $X = 0; while ($X < $BMP['width']) { switch ($BMP['bits_per_pixel']) { case 32: $COLOR = unpack('V', substr($IMG, $P, 4)); $COLOR[1] &= 0xffffff; // 不處理Alpha break; case 24: $COLOR = unpack('V', substr($IMG, $P, 3) . $VIDE); break; case 16: $COLOR = unpack("v", substr($IMG, $P, 2)); $COLOR[1] = ($COLOR[1] & $mask[0][0]) >> $mask[1][0] << $mask[2][0] << 16 | ($COLOR[1] & $mask[0][1]) >> $mask[1][1] << $mask[2][1] << 8 | ($COLOR[1] & $mask[0][2]) >> $mask[1][2] << $mask[2][2]; break; case 8: $COLOR = unpack('n', $VIDE . substr($IMG, $P, 1)); break; case 4: $COLOR = unpack('n', $VIDE . substr($IMG, floor($P), 1)); if ($P * 2 % 2 == 0) { $COLOR[1] = $COLOR[1] >> 4; } else { $COLOR[1] = $COLOR[1] & 0xf; } break; case 1: $COLOR = unpack('n', $VIDE . substr($IMG, floor($P), 1)); switch ($P * 8 % 8) { case 0: $COLOR[1] = $COLOR[1] >> 7; break; case 1: $COLOR[1] = ($COLOR[1] & 0x40) >> 6; break; case 2: $COLOR[1] = ($COLOR[1] & 0x20) >> 5; break; case 3: $COLOR[1] = ($COLOR[1] & 0x10) >> 4; break; case 4: $COLOR[1] = ($COLOR[1] & 0x8) >> 3; break; case 5: $COLOR[1] = ($COLOR[1] & 0x4) >> 2; break; case 6: $COLOR[1] = ($COLOR[1] & 0x2) >> 1; break; case 7: $COLOR[1] = $COLOR[1] & 0x1; } break; default: return FALSE; } if ($BMP['bits_per_pixel'] < 16) { $COLOR[1] = $PALETTE[$COLOR[1] + 1]; } ImageSetPixel($res, $X, $Y, $COLOR[1]); $X++; $P += $BMP['bytes_per_pixel']; } $Y--; $P += $BMP['decal']; } } // 終章:回傳新圖像 return $res; }
function ImageCopyRespectAlpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity_pct = 100) { $opacipct = $opacity_pct / 100; for ($x = $src_x; $x < $src_w; $x++) { for ($y = $src_y; $y < $src_h; $y++) { $RealPixel = phpthumb_functions::GetPixelColor($dst_im, $dst_x + $x, $dst_y + $y); $OverlayPixel = phpthumb_functions::GetPixelColor($src_im, $x, $y); $alphapct = $OverlayPixel['alpha'] / 127; $overlaypct = (1 - $alphapct) * $opacipct; $newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($dst_im, round($RealPixel['red'] * (1 - $overlaypct)) + $OverlayPixel['red'] * $overlaypct, round($RealPixel['green'] * (1 - $overlaypct)) + $OverlayPixel['green'] * $overlaypct, round($RealPixel['blue'] * (1 - $overlaypct)) + $OverlayPixel['blue'] * $overlaypct, 0); ImageSetPixel($dst_im, $dst_x + $x, $dst_y + $y, $newcolor); } } return true; }
/** * Antialias'es the pixel around x,y with weights a,b * * @param int $x X point * @param int $y Y point * @param int $a The weight of the current color * @param int $b The weight of the applied/wanted color * @param mixed $color The pixel color * @access private */ function _antialisedSubPixel($x, $y, $a, $b, $color) { $x = $this->_getX($x); $y = $this->_getX($y); if ($x >= 0 && $y >= 0 && $x < $this->getWidth() && $y < $this->getHeight()) { $tempColor = ImageColorsForIndex($this->_canvas, ImageColorAt($this->_canvas, $x, $y)); $newColor[0] = min(255, round($tempColor['red'] * $a + $color['red'] * $b)); $newColor[1] = min(255, round($tempColor['green'] * $a + $color['green'] * $b)); $newColor[2] = min(255, round($tempColor['blue'] * $a + $color['blue'] * $b)); //$newColor[3] = 0; $color = '#'; foreach ($newColor as $acolor) { $color .= sprintf('%02s', dechex($acolor)); } $newColor = $this->_color($color); //,'rgb(' . $newColor[0] . ',' . $newColor[1] . ',' . $newColor[2] .')'; ImageSetPixel($this->_canvas, $x, $y, $newColor); } }
/** * Unsharp mask. * * Unsharp mask algorithm by Torstein Hønsi 2003 (thoensi_at_netcom_dot_no) * Christoph Erdmann: changed it a little, because I could not reproduce the * darker blurred image, now it is up to 15% faster with same results * * @author Torstein Hønsi * @author Christoph Erdmann * @param resource $img Image as a resource * @param int $amount Filter parameter * @param int $radius Filter parameter * @param int $threshold Filter parameter * @return resource Sharpened image as a resource. * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. */ function UnsharpMask($img, $amount, $radius, $threshold) { // Attempt to calibrate the parameters to Photoshop: if ($amount > 500) { $amount = 500; } $amount = $amount * 0.016; if ($radius > 50) { $radius = 50; } $radius = $radius * 2; if ($threshold > 255) { $threshold = 255; } $radius = abs(round($radius)); // Only integers make sense. if ($radius == 0) { return $img; } $w = imagesx($img); $h = imagesy($img); $imgCanvas = $img; $imgCanvas2 = $img; $imgBlur = imagecreatetruecolor($w, $h); // Gaussian blur matrix: // 1 2 1 // 2 4 2 // 1 2 1 // Move copies of the image around one pixel at the time and merge them // with weight according to the matrix. The same matrix is simply // repeated for higher radii. for ($i = 0; $i < $radius; $i++) { imagecopy($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1); // up left imagecopymerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down right imagecopymerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // up right imagecopymerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // right imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20); // up imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // down imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); // center } $imgCanvas = $imgBlur; // Calculate the difference between the blurred pixels and the original // and set the pixels. for ($x = 0; $x < $w; $x++) { // Each row. for ($y = 0; $y < $h; $y++) { // Each pixel. $rgbOrig = ImageColorAt($imgCanvas2, $x, $y); $rOrig = $rgbOrig >> 16 & 0xff; $gOrig = $rgbOrig >> 8 & 0xff; $bOrig = $rgbOrig & 0xff; $rgbBlur = ImageColorAt($imgCanvas, $x, $y); $rBlur = $rgbBlur >> 16 & 0xff; $gBlur = $rgbBlur >> 8 & 0xff; $bBlur = $rgbBlur & 0xff; // When the masked pixels differ less from the original than the // threshold specifies, they are set to their original value. $rNew = abs($rOrig - $rBlur) >= $threshold ? max(0, min(255, $amount * ($rOrig - $rBlur) + $rOrig)) : $rOrig; $gNew = abs($gOrig - $gBlur) >= $threshold ? max(0, min(255, $amount * ($gOrig - $gBlur) + $gOrig)) : $gOrig; $bNew = abs($bOrig - $bBlur) >= $threshold ? max(0, min(255, $amount * ($bOrig - $bBlur) + $bOrig)) : $bOrig; if ($rOrig != $rNew || $gOrig != $gNew || $bOrig != $bNew) { $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew); ImageSetPixel($img, $x, $y, $pixCol); } } } return $img; }
function PlotBMP(&$BMPinfo) { $starttime = time(); if (!isset($BMPinfo['bmp']['data']) || !is_array($BMPinfo['bmp']['data'])) { echo 'ERROR: no pixel data<BR>'; return false; } set_time_limit(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000)); if ($im = ImageCreateTrueColor($BMPinfo['resolution_x'], $BMPinfo['resolution_y'])) { for ($row = 0; $row < $BMPinfo['resolution_y']; $row++) { for ($col = 0; $col < $BMPinfo['resolution_x']; $col++) { if (isset($BMPinfo['bmp']['data'][$row][$col])) { $red = ($BMPinfo['bmp']['data'][$row][$col] & 0xff0000) >> 16; $green = ($BMPinfo['bmp']['data'][$row][$col] & 0xff00) >> 8; $blue = $BMPinfo['bmp']['data'][$row][$col] & 0xff; $pixelcolor = ImageColorAllocate($im, $red, $green, $blue); ImageSetPixel($im, $col, $row, $pixelcolor); } else { //echo 'ERROR: no data for pixel '.$row.' x '.$col.'<BR>'; //return false; } } } if (headers_sent()) { echo 'plotted ' . $BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] . ' pixels in ' . (time() - $starttime) . ' seconds<BR>'; ImageDestroy($im); exit; } else { header('Content-type: image/png'); ImagePNG($im); ImageDestroy($im); return true; } } return false; }
/** * Applies the effect. */ public function apply() { $amount = $this->_params['amount']; $radius = $this->_params['radius']; $threshold = $this->_params['threshold']; // Attempt to calibrate the parameters to Photoshop: $amount = min($amount, 500); $amount = $amount * 0.016; if ($amount == 0) { return true; } $radius = min($radius, 50); $radius = $radius * 2; $threshold = min($threshold, 255); $radius = abs(round($radius)); // Only integers make sense. if ($radius == 0) { return true; } $img = $this->_image->_im; $w = ImageSX($img); $h = ImageSY($img); $imgCanvas = ImageCreateTrueColor($w, $h); $imgCanvas2 = ImageCreateTrueColor($w, $h); $imgBlur = ImageCreateTrueColor($w, $h); $imgBlur2 = ImageCreateTrueColor($w, $h); ImageCopy($imgCanvas, $img, 0, 0, 0, 0, $w, $h); ImageCopy($imgCanvas2, $img, 0, 0, 0, 0, $w, $h); // Gaussian blur matrix: // // 1 2 1 // 2 4 2 // 1 2 1 // ////////////////////////////////////////////////// // Move copies of the image around one pixel at the time and merge them // with weight according to the matrix. The same matrix is simply // repeated for higher radii. for ($i = 0; $i < $radius; $i++) { // up left ImageCopy($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1); // down right ImageCopyMerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down left ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // up right ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // left ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // right ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // up ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20); // down ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // center ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); ImageCopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h); // During the loop above the blurred copy darkens, possibly due to // a roundoff error. Therefore the sharp picture has to go through // the same loop to produce a similar image for comparison. This is // not a good thing, as processing time increases heavily. ImageCopy($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 20); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 16.666667); ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); ImageCopy($imgCanvas2, $imgBlur2, 0, 0, 0, 0, $w, $h); } // Calculate the difference between the blurred pixels and the original // and set the pixels for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $rgbOrig = ImageColorAt($imgCanvas2, $x, $y); $rOrig = $rgbOrig >> 16 & 0xff; $gOrig = $rgbOrig >> 8 & 0xff; $bOrig = $rgbOrig & 0xff; $rgbBlur = ImageColorAt($imgCanvas, $x, $y); $rBlur = $rgbBlur >> 16 & 0xff; $gBlur = $rgbBlur >> 8 & 0xff; $bBlur = $rgbBlur & 0xff; // When the masked pixels differ less from the original than // the threshold specifies, they are set to their original // value. $rNew = abs($rOrig - $rBlur) >= $threshold ? max(0, min(255, $amount * ($rOrig - $rBlur) + $rOrig)) : $rOrig; $gNew = abs($gOrig - $gBlur) >= $threshold ? max(0, min(255, $amount * ($gOrig - $gBlur) + $gOrig)) : $gOrig; $bNew = abs($bOrig - $bBlur) >= $threshold ? max(0, min(255, $amount * ($bOrig - $bBlur) + $bOrig)) : $bOrig; if ($rOrig != $rNew || $gOrig != $gNew || $bOrig != $bNew) { $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew); ImageSetPixel($img, $x, $y, $pixCol); } } } ImageDestroy($imgCanvas); ImageDestroy($imgCanvas2); ImageDestroy($imgBlur); ImageDestroy($imgBlur2); }
public function WhiteBalance(&$gdimg, $targetColor = '') { if (phpthumb_functions::IsHexColor($targetColor)) { $targetPixel = array('red' => hexdec(substr($targetColor, 0, 2)), 'green' => hexdec(substr($targetColor, 2, 2)), 'blue' => hexdec(substr($targetColor, 4, 2))); } else { $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, false); $targetPixel = array('red' => max(array_keys($Analysis['red'])), 'green' => max(array_keys($Analysis['green'])), 'blue' => max(array_keys($Analysis['blue']))); } $grayValue = phpthumb_functions::GrayscaleValue($targetPixel['red'], $targetPixel['green'], $targetPixel['blue']); $scaleR = $grayValue / $targetPixel['red']; $scaleG = $grayValue / $targetPixel['green']; $scaleB = $grayValue / $targetPixel['blue']; for ($x = 0; $x < ImageSX($gdimg); $x++) { for ($y = 0; $y < ImageSY($gdimg); $y++) { $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, max(0, min(255, round($currentPixel['red'] * $scaleR))), max(0, min(255, round($currentPixel['green'] * $scaleG))), max(0, min(255, round($currentPixel['blue'] * $scaleB))), $currentPixel['alpha']); ImageSetPixel($gdimg, $x, $y, $newColor); } } return true; }
/** * alternative workaround function for bicubic resizing. works well for downsizing and upsizing. VERY VERY slow. taken from php.net comments * * @access private */ function _imageCopyResampledWorkaround2(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { ImagePaletteCopy($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $w = 0; for ($y = $dst_y; $y < $dst_h; $y++) { $ow = $w; $w = round(($y + 1) * $rY); $t = 0; for ($x = $dst_x; $x < $dst_w; $x++) { $r = $g = $b = 0; $a = 0; $ot = $t; $t = round(($x + 1) * $rX); for ($u = 0; $u < $w - $ow; $u++) { for ($p = 0; $p < $t - $ot; $p++) { $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $ot + $p, $ow + $u)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; } } ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a)); } } }
// Start building the image $cas_image = @imagecreate($cas_imgwidth, $cas_imgheight) or die("Cannot Initialize new GD image stream"); $cas_bgcolor = imagecolorallocate($cas_image, $cas_bgcolor[0], $cas_bgcolor[1], $cas_bgcolor[2]); $cas_fontcolor = imagecolorallocate($cas_image, $cas_textcolor[0], $cas_textcolor[1], $cas_textcolor[2]); // Add noice lines if ($cas_noise_line) { for ($i = 0; $i < $wordcount; $i++) { $color_line = ImageColorAllocate($cas_image, mt_rand(155, 255), mt_rand(155, 255), mt_rand(155, 255)); ImageArc($cas_image, mt_rand(-5, $cas_imgwidth), mt_rand(-5, $cas_imgheight), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color_line); } } // Add noice points if ($cas_noise_point) { for ($i = 0; $i < $wordcount * 50; $i++) { $color_point = ImageColorAllocate($cas_image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); ImageSetPixel($cas_image, mt_rand(0, $cas_imgwidth), mt_rand(0, $cas_imgheight), $color_point); } } // Check for freetype lib, if not found default to ugly built in capability using imagechar (Lee's mod) // Also check that the chosen TrueType font is available if (function_exists('imagettftext') && file_exists($cas_font)) { $cas_angle = 3; // Degrees to tilt the text $cas_offset = 5; // Pixels to offset the text from the border // Use png is available, since it produces clearer text images ; default option if (function_exists('imagepng') && $cas_UsePngNotJpeg) { imagettftext($cas_image, $cas_font_size, $cas_angle, $cas_offset, $cas_imgheight - $cas_offset, $cas_fontcolor, $cas_font, $cas_antispam); header("Content-type: image/png"); imagepng($cas_image); } else {
function plot($x, $y, $type, $size, $colour, $offset) { //print("drawing point of type: $type, at offset: $offset"); $u = $x + $offset; $v = $this->calculated['inner_border']['bottom'] - $y + $offset; $half = $size / 2; switch ($type) { case 'square': ImageFilledRectangle($this->image, $u - $half, $v - $half, $u + $half, $v + $half, $this->colour[$colour]); break; case 'square-open': ImageRectangle($this->image, $u - $half, $v - $half, $u + $half, $v + $half, $this->colour[$colour]); break; case 'circle': ImageArc($this->image, $u, $v, $size, $size, 0, 360, $this->colour[$colour]); ImageFillToBorder($this->image, $u, $v, $this->colour[$colour], $this->colour[$colour]); break; case 'circle-open': ImageArc($this->image, $u, $v, $size, $size, 0, 360, $this->colour[$colour]); break; case 'diamond': ImageFilledPolygon($this->image, array($u, $v - $half, $u + $half, $v, $u, $v + $half, $u - $half, $v), 4, $this->colour[$colour]); break; case 'diamond-open': ImagePolygon($this->image, array($u, $v - $half, $u + $half, $v, $u, $v + $half, $u - $half, $v), 4, $this->colour[$colour]); break; case 'triangle': ImageFilledPolygon($this->image, array($u, $v - $half, $u + $half, $v + $half, $u - $half, $v + $half), 3, $this->colour[$colour]); break; case 'triangle-open': ImagePolygon($this->image, array($u, $v - $half, $u + $half, $v + $half, $u - $half, $v + $half), 3, $this->colour[$colour]); break; case 'dot': ImageSetPixel($this->image, $u, $v, $this->colour[$colour]); break; } }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = ImageCreate($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, 255, 255, 255); $col[1] = ImageColorAllocate($base_image, 0, 0, 0); imagefill($base_image, 0, 0, $col[0]); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } //$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); //ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); //Get Desired size of image. //Insert the following code to generate desired size QR Code //Example:----- (Note: this should be written in the file where we are going to generate the QR Code) //------define('IMAGE_WIDTH',160); //------define('IMAGE_HEIGHT',160); //------QRcode::png(); $targetW = defined('IMAGE_WIDTH') ? IMAGE_WIDTH : $imgW * $pixelPerPoint; $targetH = defined('IMAGE_HEIGHT') ? IMAGE_HEIGHT : $imgH * $pixelPerPoint; $target_image = ImageCreate($targetW, $targetH); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $targetW, $targetH, $imgW, $imgH); //Transparent Background QR code ImageColorTransparent($target_image, $col[0]); ImageDestroy($base_image); return $target_image; }
function mkimage($data) { $data_array = explode("\n", $data); $c = count($data_array) - 1; $image_size = $c; $output_size = ($c + $this->quiet_zone * 2) * $this->module_size; $img = ImageCreate($image_size, $image_size); $white = ImageColorAllocate($img, 255, 255, 255); $black = ImageColorAllocate($img, 0, 0, 0); $im = ImageCreate($output_size, $output_size); $white2 = ImageColorAllocate($im, 255, 255, 255); ImageFill($im, 0, 0, $white2); $y = 0; foreach ($data_array as $row) { $x = 0; while ($x < $image_size) { if (substr($row, $x, 1) == "1") { ImageSetPixel($img, $x, $y, $black); } $x++; } $y++; } $quiet_zone_offset = $this->quiet_zone * $this->module_size; $image_width = $image_size * $this->module_size; ImageCopyResized($im, $img, $quiet_zone_offset, $quiet_zone_offset, 0, 0, $image_width, $image_width, $image_size, $image_size); return $im; }
function UnsharpMask(&$img, $amount, $radius, $threshold) { //////////////////////////////////////////////////////////////////////////////////////////////// //// //// Unsharp Mask for PHP - version 2.0 //// //// Unsharp mask algorithm by Torstein Honsi 2003-06. //// thoensi_at_netcom_dot_no. //// Please leave this notice. //// /////////////////////////////////////////////////////////////////////////////////////////////// return; //if($_SERVER['REMOTE_ADDR'] != '82.207.122.192') return; // $img is an image that is already created within php using // imgcreatetruecolor. No url! $img must be a truecolor image. // Attempt to calibrate the parameters to Photoshop: if ($amount > 500) { $amount = 500; } $amount = $amount * 0.016; if ($radius > 50) { $radius = 50; } $radius = $radius * 2; if ($threshold > 255) { $threshold = 255; } $radius = abs(round($radius)); // Only integers make sense. if ($radius == 0) { return $img; } $w = imagesx($img); $h = imagesy($img); $imgCanvas = imagecreatetruecolor($w, $h); $imgCanvas2 = imagecreatetruecolor($w, $h); $imgBlur = imagecreatetruecolor($w, $h); $imgBlur2 = imagecreatetruecolor($w, $h); imagecopyresampled($imgCanvas, $img, 0, 0, 0, 0, $w, $h, $w, $h); imagecopyresampled($imgCanvas2, $img, 0, 0, 0, 0, $w, $h, $w, $h); // Gaussian blur matrix: // // 1 2 1 // 2 4 2 // 1 2 1 // ////////////////////////////////////////////////// imagecopyresampled($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, $w, $h); // background for ($i = 0; $i < $radius; $i++) { if (function_exists('imageconvolution')) { // PHP >= 5.1 $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)); imageconvolution($imgCanvas, $matrix, 16, 0); } else { // Move copies of the image around one pixel at the time and merge them with weight // according to the matrix. The same matrix is simply repeated for higher radii. imagecopyresampled($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1, $w - 1, $h - 1); // up left imagecopymerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down right imagecopymerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // up right imagecopymerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // right imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20); // up imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // down imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); // center imagecopyresampled($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h, $w, $h); // During the loop above the blurred copy darkens, possibly due to a roundoff // error. Therefore the sharp picture has to go through the same loop to // produce a similar image for comparison. This is not a good thing, as processing // time increases heavily. imagecopyresampled($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, $w, $h); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 20); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 16.666667); imagecopymerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); imagecopyresampled($imgCanvas2, $imgBlur2, 0, 0, 0, 0, $w, $h, $w, $h); } } //return $imgBlur; // Calculate the difference between the blurred pixels and the original // and set the pixels for ($x = 0; $x < $w; $x++) { // each row for ($y = 0; $y < $h; $y++) { // each pixel $rgbOrig = ImageColorAt($imgCanvas2, $x, $y); $rOrig = $rgbOrig >> 16 & 0xff; $gOrig = $rgbOrig >> 8 & 0xff; $bOrig = $rgbOrig & 0xff; $rgbBlur = ImageColorAt($imgCanvas, $x, $y); $rBlur = $rgbBlur >> 16 & 0xff; $gBlur = $rgbBlur >> 8 & 0xff; $bBlur = $rgbBlur & 0xff; // When the masked pixels differ less from the original // than the threshold specifies, they are set to their original value. $rNew = abs($rOrig - $rBlur) >= $threshold ? max(0, min(255, $amount * ($rOrig - $rBlur) + $rOrig)) : $rOrig; $gNew = abs($gOrig - $gBlur) >= $threshold ? max(0, min(255, $amount * ($gOrig - $gBlur) + $gOrig)) : $gOrig; $bNew = abs($bOrig - $bBlur) >= $threshold ? max(0, min(255, $amount * ($bOrig - $bBlur) + $bOrig)) : $bOrig; if ($rOrig != $rNew || $gOrig != $gNew || $bOrig != $bNew) { $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew); ImageSetPixel($img, $x, $y, $pixCol); } } } }