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;
}
示例#2
0
/**
 *
 * 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);
        }
    }
}
示例#3
0
/**
 * Image to Text
 *
 * Takes a given system image and recreates it with a given string
 * Function accepts arbitrary elements to use for markup
 *
 * @access	public
 * @param	string
 * @param	string
 * @param	string
 * @return	string
 */
function image_to_text($data, $txt, $new_width = NULL, $new_height = NULL, $inline_element = 'span')
{
    $img = imagecreatefromstring($data);
    $width = imagesx($img);
    $height = imagesy($img);
    // add abiilty to resize on the fly
    if ($new_width and $new_height) {
        $new = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled($new, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        unset($img);
        $img = $new;
        $width = $new_width;
        $height = $new_height;
        unset($new);
    }
    $counter = 0;
    $output = "";
    for ($i = 0; $i < $height; $i++) {
        for ($j = 0; $j < $width; $j++) {
            $counter = $counter % strlen($txt);
            $cindex = ImageColorAt($img, $j, $i);
            $rgb = ImageColorsForIndex($img, $cindex);
            $hex = rgb2hex(array($rgb['red'], $rgb['green'], $rgb['blue']));
            $output .= '<' . $inline_element . ' style="color:#' . $hex . ';">' . substr($txt, $counter, 1) . '</' . $inline_element . '>';
            $counter++;
        }
        $output .= "<br />";
    }
    return $output;
}
示例#4
0
 function get_hex($location, $extensions = array('PNG', 'png', 'Png', 'JPG', 'jpg', 'Jpg', 'JPEG', 'jpeg', 'Jpeg', 'GIF', 'gif', 'Gif'), $postvar = "myimage", $getvar = "imgclix")
 {
     if (isset($_GET[$getvar])) {
         foreach ($extensions as $var) {
             if (file_exists($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var)) {
                 if (stristr($var, 'png')) {
                     $im = ImageCreateFromPng($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var);
                 } elseif (stristr($var, 'gif')) {
                     $im = ImageCreateFromGIF($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var);
                 } elseif (stristr($var, 'jpg') || stristr($var, 'jpeg')) {
                     $im = ImageCreateFromJpeg($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var);
                 } else {
                     return FALSE;
                 }
                 $rgb = ImageColorAt($im, $_POST[$postvar . '_x'], $_POST[$postvar . '_y']);
                 $rgb = imagecolorsforindex($im, $rgb);
                 $hex = sprintf('#%02X%02X%02X', $rgb['red'], $rgb['green'], $rgb['blue']);
                 break;
             }
         }
     } else {
         return FALSE;
     }
     if (!isset($hex) || $hex == '') {
         return FALSE;
     }
     return $hex;
 }
示例#5
0
 protected function computeII($canvas, $image_width, $image_height)
 {
     $ii_w = $image_width + 1;
     $ii_h = $image_height + 1;
     $ii = array();
     $ii2 = array();
     for ($i = 0; $i < $ii_w; $i++) {
         $ii[$i] = 0;
         $ii2[$i] = 0;
     }
     for ($i = 1; $i < $ii_h - 1; $i++) {
         $ii[$i * $ii_w] = 0;
         $ii2[$i * $ii_w] = 0;
         $rowsum = 0;
         $rowsum2 = 0;
         for ($j = 1; $j < $ii_w - 1; $j++) {
             $rgb = ImageColorAt($canvas, $j, $i);
             $red = $rgb >> 16 & 0xff;
             $green = $rgb >> 8 & 0xff;
             $blue = $rgb & 0xff;
             $grey = 0.2989 * $red + 0.587 * $green + 0.114 * $blue >> 0;
             // this is what matlab uses
             $rowsum += $grey;
             $rowsum2 += $grey * $grey;
             $ii_above = ($i - 1) * $ii_w + $j;
             $ii_this = $i * $ii_w + $j;
             $ii[$ii_this] = $ii[$ii_above] + $rowsum;
             $ii2[$ii_this] = $ii2[$ii_above] + $rowsum2;
         }
     }
     return array('ii' => $ii, 'ii2' => $ii2);
 }
示例#6
0
function getTextFromImage($file)
{
    $background = imagecreatefromjpeg('background.jpg');
    $image = imagecreatefromstring($file);
    $black = imagecolorallocate($image, 0, 0, 0);
    $min_visible_y = $max_y = imagesy($image);
    $min_visible_x = $max_x = imagesx($image);
    $max_visible_x = $max_visible_y = 0;
    for ($y = 0; $y < $max_y; $y++) {
        for ($x = 0; $x < $max_x; $x++) {
            $pixel = ImageColorAt($image, $x, $y);
            $colors = imagecolorsforindex($image, $pixel);
            $pixel_bg = ImageColorAt($background, $x, $y);
            $colors_bg = imagecolorsforindex($background, $pixel_bg);
            $range = 35;
            if ($colors['red'] + $range > $colors_bg['red'] && $colors['red'] - $range < $colors_bg['red']) {
                imagesetpixel($image, $x, $y, $black);
            } else {
                $min_visible_x = $min_visible_x > $x ? $x : $min_visible_x;
                $max_visible_x = $max_visible_x < $x ? $x : $max_visible_x;
                $min_visible_y = $min_visible_y > $y ? $y : $min_visible_y;
                $max_visible_y = $max_visible_y < $y ? $y : $max_visible_y;
            }
        }
    }
    $image = imagecrop($image, ['x' => $min_visible_x, 'y' => $min_visible_y, 'width' => $max_visible_x, 'height' => $max_visible_y]);
    imagefilter($image, IMG_FILTER_GRAYSCALE);
    $tmpfname = tempnam("/tmp", "OCR");
    imagepng($image, $tmpfname);
    $txt = $ocr->recognize($tmpfname, ['eng'], 3);
    unlink($tmpfname);
    return str_replace("\n", "", $txt);
}
示例#7
0
 function calcIconImgFeature(&$appImg)
 {
     $features = array();
     $featurePadding = 6;
     //角丸アイコン対策
     $featureWidth = (self::appWidth - $featurePadding) / 3;
     $featureHeight = (self::appHeight - $featurePadding) / 3;
     echo "<br>\n";
     for ($fy = 0; $fy < 3; $fy++) {
         for ($fx = 0; $fx < 3; $fx++) {
             $orgX = $featureWidth * $fx + $featurePadding;
             $orgY = $featureHeight * $fy + $featurePadding;
             $r = $g = $b = 0;
             for ($y = 0; $y < $featureHeight; $y++) {
                 for ($x = 0; $x < $featureWidth; $x++) {
                     $rgb = ImageColorAt($appImg, $orgX + $x, $orgY + $y);
                     $r += $rgb >> 16 & 0xff;
                     $g += $rgb >> 8 & 0xff;
                     $b += $rgb & 0xff;
                 }
             }
             $totalNum = $featureHeight * $featureWidth;
             $rgb = dechex(intval($r / $totalNum)) . dechex(intval($g / $totalNum)) . dechex(intval($b / $totalNum));
             echo "<font color='{$rgb}'>■</font>";
         }
         echo "<br>\n";
     }
     return $features;
 }
示例#8
0
 /**
  * Return the img tag of grayscale image
  * and put the grayscale image under images/grayscale
  * @param $filename
  * @return string
  */
 public function convert_to_grayscale($filename)
 {
     $destination = 'images/grayscale/' . $filename;
     $filename = base_url() . '/images/' . $filename;
     $image = imagecreatefromjpeg($filename);
     $imgw = imagesx($image);
     $imgh = imagesy($image);
     for ($i = 0; $i < $imgw; $i++) {
         for ($j = 0; $j < $imgh; $j++) {
             // get the rgb value for current pixel
             $rgb = ImageColorAt($image, $i, $j);
             // extract each value for r, g, b
             $rr = $rgb >> 16 & 0xff;
             $gg = $rgb >> 8 & 0xff;
             $bb = $rgb & 0xff;
             // get the Value from the RGB value
             $g = round(($rr + $gg + $bb) / 3);
             // grayscale values have r=g=b=g
             $val = imagecolorallocate($image, $g, $g, $g);
             // set the gray value
             imagesetpixel($image, $i, $j, $val);
         }
     }
     //        imagefilter($image, IMG_FILTER_GRAYSCALE);
     imagejpeg($image, $destination);
     //        $rgb = imagecolorat($image, 10, 15);
     //        $r = ($rgb >> 16) & 0xFF;
     //        $g = ($rgb >> 8) & 0xFF;
     //        $b = $rgb & 0xFF;
     //
     //        var_dump($r, $g, $b);
     return "<img src='" . base_url() . "/" . $destination . "'/>";
 }
 function by_lines($image, &$size_x, &$size_y)
 {
     $lines = array();
     $size_x = imagesx($image->get_handle());
     $size_y = imagesy($image->get_handle());
     $dest_img = imagecreatetruecolor($size_x, $size_y);
     imagecopymerge($dest_img, $image->get_handle(), 0, 0, 0, 0, $size_x, $size_y, 100);
     // initialize line length counter
     $ctr = 0;
     for ($y = 0; $y < $size_y; $y++) {
         $line = "";
         for ($x = 0; $x < $size_x; $x++) {
             // Save image pixel to the stream data
             $rgb = ImageColorAt($dest_img, $x, $y);
             $r = $rgb >> 16 & 0xff;
             $g = $rgb >> 8 & 0xff;
             $b = $rgb & 0xff;
             $line .= sprintf("%02X%02X%02X", min(max($r, 0), 255), min(max($g, 0), 255), min(max($b, 0), 255));
             // Increate the line length counter; check if stream line needs to be terminated
             $ctr += 6;
             if ($ctr > MAX_LINE_LENGTH) {
                 $line .= "\n";
                 $ctr = 0;
             }
         }
         $lines[] = $line;
     }
     return $lines;
 }
/**
 * Converting an image to ascii art.
 *
 * @author	PHPPRO	http://www.phpro.org/examples/Image-To-Ascii-Art-With-PHP.html
 * 
 * @param mixed	binary or string of an image
 **/
function image2ascii($image)
{
    // return value
    $ret = '';
    // open the image
    // $img = imagecreatefromjpeg($image); PHPPRO originally do this
    $img = imagecreatefromstring($image);
    // my little hack
    // get width and height
    $width = imagesx($img);
    $height = imagesy($img);
    // loop for height
    for ($h = 0; $h < $height; $h++) {
        // loop for height
        for ($w = 0; $w <= $width; $w++) {
            // add color
            $rgb = ImageColorAt($img, $w, $h);
            $r = $rgb >> 16 & 0xff;
            $g = $rgb >> 8 & 0xff;
            $b = $rgb & 0xff;
            // create a hex value from the rgb
            $hex = '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
            // now add to the return string and we are done
            if ($w == $width) {
                $ret .= '<br>';
            } else {
                $ret .= '<b style="color:' . $hex . ';">#</b>';
            }
        }
    }
    return $ret;
}
示例#11
0
function mainColor($image)
{
    $i = 0;
    for ($x = 0; $x < imagesx($image); $x++) {
        for ($y = 0; $y < imagesy($image); $y++) {
            $rgb = ImageColorAt($image, $x, $y);
            $r += $rgb >> 16 & 0xff;
            $g += $rgb >> 8 & 0xff;
            $b += $rgb & 0xff;
            $i++;
        }
    }
    return imageColorAllocate($image, $r / $i, $g / $i, $b / $i);
}
 private function getColors($image, $width, $height)
 {
     $colors = array();
     for ($i = 0; $i < $width; $i++) {
         for ($j = 0; $j < $height; $j++) {
             $rgb = ImageColorAt($image, $i, $j);
             $r = $rgb >> 16 & 0xff;
             $g = $rgb >> 8 & 0xff;
             $b = $rgb & 0xff;
             $colors[serialize(array($r, $g, $b))]++;
         }
     }
     $colors = $this->sortColors($colors);
     return $colors;
 }
示例#13
0
 function NewColorizeImage($img, $targetColor, $baseColor)
 {
     $targetHexArr = $this->c->hex82hex($targetColor);
     $targetColor = $targetHexArr[0];
     $alpha = $targetHexArr[1];
     $c1 = $this->c->hex2hsl($baseColor);
     $c2 = $this->c->hex2hsl($targetColor);
     $im = imagecreatefrompng($img);
     $height = imagesy($im);
     $width = imagesx($im);
     $imnew = imagecreatetruecolor($width, $height);
     imagealphablending($imnew, false);
     imagesavealpha($imnew, true);
     $transparent = imagecolorallocatealpha($imnew, 255, 255, 255, 127);
     imagefilledrectangle($imnew, 0, 0, $width, $height, $transparent);
     $rgb = $this->rgb2array($targetColor);
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             $rgba = ImageColorAt($im, $x, $y);
             $rgb = array($rgba >> 16 & 0xff, $rgba >> 8 & 0xff, $rgba & 0xff);
             $hsl = $this->c->rgb2hsl($rgb);
             $a[0] = $hsl[0] + ($c2[0] - $c1[0]);
             $a[1] = $hsl[1] * ($c2[1] / ($c1[1] == 0 ? 1.0E-5 : $c1[1]));
             if ($a[1] > 1) {
                 $a[1] = 1;
             }
             $a[2] = exp(log($hsl[2]) * log($c2[2]) / log($c1[2]));
             if ($a[2] > 1) {
                 $a[2] = 1;
             }
             $rgb = $this->c->hsl2rgb($a);
             $A = 0xfe - ($rgba >> 24) * 2 & 0xff;
             $A = (int) ($A * (hexdec($alpha) / 0xfe));
             if ($A > 0xff) {
                 $A = 0xff;
             }
             $A = (int) ((0xfe - $A) / 2);
             imagesetpixel($imnew, $x, $y, imagecolorallocatealpha($imnew, $rgb[0], $rgb[1], $rgb[2], $A));
         }
     }
     $hash = md5($img . $targetColor . $alpha) . '.png';
     imagepng($imnew, $this->cache . DS . $hash);
     imagedestroy($imnew);
     imagedestroy($im);
     return $hash;
 }
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;
}
示例#15
0
 function colorizeImage($img, $targetColor, $baseColor)
 {
     $cachefile = $this->_folder . 'colorize' . md5($img) . $targetColor . $baseColor . '.' . $this->_filetype;
     if (!NextendFilesystem::existsFile($cachefile)) {
         $targetHexArr = NextendColor::hex82hex($targetColor);
         $targetColor = $targetHexArr[0];
         $alpha = hexdec($targetHexArr[1]);
         $c1 = NextendColor::hex2hsl($baseColor);
         $c2 = NextendColor::hex2hsl($targetColor);
         $im = imagecreatefrompng($img);
         $width = imagesx($im);
         $height = imagesy($im);
         $this->createIm($width, $height);
         $rgb = NextendColor::rgb2array($targetColor);
         for ($x = 0; $x < $width; $x++) {
             for ($y = 0; $y < $height; $y++) {
                 $rgba = ImageColorAt($im, $x, $y);
                 $rgb = array($rgba >> 16 & 0xff, $rgba >> 8 & 0xff, $rgba & 0xff);
                 $hsl = NextendColor::rgb2hsl($rgb);
                 $a[0] = $hsl[0] + ($c2[0] - $c1[0]);
                 $a[1] = $hsl[1] * ($c2[1] / $c1[1]);
                 if ($a[1] > 1) {
                     $a[1] = 1;
                 }
                 $a[2] = exp(log($hsl[2]) * log($c2[2]) / log($c1[2]));
                 if ($a[2] > 1) {
                     $a[2] = 1;
                 }
                 $rgb = NextendColor::hsl2rgb($a);
                 $A = 0xff - ($rgba >> 24) * 2 & 0xff;
                 $A = (int) ($A * ($alpha / 0xff));
                 if ($A > 0xff) {
                     $A = 0xff;
                 }
                 $A = (int) ((0xff - $A) / 2);
                 imagesetpixel($this->_im, $x, $y, imagecolorallocatealpha($this->_im, $rgb[0], $rgb[1], $rgb[2], $A));
             }
         }
         $this->saveIm($cachefile);
         imagedestroy($im);
     }
     return NextendFilesystem::pathToAbsoluteURL($cachefile);
 }
 function _initColors()
 {
     $im = imagecreatefrompng($this->urlScale);
     $size = getimagesize($this->urlScale);
     $width = $size[0];
     $height = $size[1];
     $go = 1;
     //print("$height et $width");
     $step = (int) (($width - $this->colorNb) / $this->colorNb);
     while ($go < $width) {
         $rgb = ImageColorAt($im, $go, $height - 3);
         $go += $step;
         $rgb = imagecolorsforindex($im, $rgb);
         // RGB
         $tcomposantes[] = array($rgb['red'], $rgb['green'], $rgb['blue']);
     }
     $this->colorScaleInfo = $tcomposantes;
     $this->fontSize = 8;
 }
示例#17
0
function __grayJpeg($imgname)
{
    $im = @imagecreatefromjpeg($imgname);
    if (!$im) {
        $im = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        imagestring($im, 10, 5, 5, 'Error loading ' . $imgname, $tc);
    } else {
        $img_width = ImageSX($im);
        $img_height = ImageSY($im);
        for ($y = 0; $y < $img_height; $y++) {
            for ($x = 0; $x < $img_width; $x++) {
                $gray = ImageColorAt($im, $x, $y) >> 8 & 0xff;
                imagesetpixel($im, $x, $y, ImageColorAllocate($im, $gray, $gray, $gray));
            }
        }
    }
    return $im;
}
示例#18
0
 public function getDomColor()
 {
     $maxheight = 300;
     $barwidth = 2;
     $im = $this->getHandle();
     $imgw = imagesx($im);
     $imgh = imagesy($im);
     $n = $imgw * $imgh;
     // n = total number or pixels
     $histo = array();
     for ($i = 0; $i < $imgw; $i++) {
         for ($j = 0; $j < $imgh; $j++) {
             $rgb = ImageColorAt($im, $i, $j);
             // get the rgb value for current pixel
             $r = $rgb >> 16 & 0xff;
             // extract each value for r, g, b
             $g = $rgb >> 8 & 0xff;
             $b = $rgb & 0xff;
             $V = round(($r + $g + $b) / 3);
             // get the Value from the RGB value
             $histo[$V] += $V / $n;
             // add the point to the histogram
             // find the maximum in the histogram in order to display a normated graph
             $max = 0;
             for ($i = 0; $i < 255; $i++) {
                 if ($histo[$i] > $max) {
                     $max = $histo[$i];
                 }
             }
             echo "<div style='width: " . 256 * $barwidth . "px; border: 1px solid'>";
             for ($i = 0; $i < 255; $i++) {
                 $val += $histo[$i];
                 $h = $histo[$i] / $max * $maxheight;
                 echo "<img src=\"img.gif\" width=\"" . $barwidth . "\"\nheight=\"" . $h . "\" border=\"0\">";
             }
             echo "</div>";
         }
     }
 }
示例#19
0
function image_float($img, $align, $texte, $margin = 10)
{
    list($w, $h) = getimagesize($img);
    $pixels = imagecreatefrompng($img);
    $right = $align == "right";
    $style = "float: {$align}; clear: {$align}; height: 1px; width: ";
    $forme = '';
    for ($j = 0; $j < $h; $j++) {
        $larg = $w + $margin;
        for ($i = 1; $i <= $w; $i++) {
            $c = ImageColorAt($pixels, $right ? $i : $w - $i, $j);
            if (($c >> 24 & 0xff) > 125) {
                $larg--;
            } else {
                break;
            }
        }
        $forme .= "\n<div style='{$style}{$larg}" . "px;'></div>";
    }
    $c = $w + round($w / 2);
    return "<div style='border: 1px solid; width: {$c}px; padding: 8px;'>" . "<div style='position: relative; float: {$align};'><span style='background-image:url({$img}); position: absolute; {$align}: 0px;width: {$w}px; height:{$h}px;'></span></div>{$forme}<p>{$texte}</p></div>";
}
示例#20
0
 /**
  * 根据rgb去噪
  * 原理是因为验证码和背景色的rgb不同
  * @return image
  */
 public function quzhao()
 {
     $data = array();
     for ($x = 0; $x < $this->white; $x++) {
         for ($y = 0; $y < $this->hight; $y++) {
             $index = ImageColorAt($this->Iresource, $x, $y);
             $rgbarray = imagecolorsforindex($this->Iresource, $index);
             if ($rgbarray['red'] < 78 || $rgbarray['green'] < 110 || $rgbarray['blue'] < 110) {
                 array_push($data, 1);
                 //   echo "*";
             } else {
                 array_push($data, 0);
                 imagefill($this->Iresource, $x, $y, self::getWhitePaint());
                 //  echo "-";
             }
             //  echo " ";
         }
         // echo " <br/>";
     }
     // echo "<br/>";
     $this->dataArray = $data;
     return $this->Iresource;
 }
示例#21
0
/**
 * 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;
}
示例#22
0
 /**
  * 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));
         }
     }
 }
示例#23
0
文件: page.php 项目: Nakei/FoOlSlide
	/**
	 * With a heavy pixel-per-pixel, it checks if the image is black and white.
	 * For better performance, it uses the thumbnail to check on less pixels.
	 * 
	 * @author woxxy, and a loop taken from a site...
	 * @return string "bw" if black and white, "rgb" if colors, false on failure 
	 */
	public function is_bw() {
		// Make sure the chapter and comic are selected
		$this->get_chapter();

		// Get the thumbnail
		$rel = 'content/comics/' . $this->chapter->comic->directory() . '/' . $this->chapter->directory() . '/' . $this->thumbnail . $this->filename;

		// We need to know the image type to spawn the right imagecreate function
		switch ($this->mime) {
			case "image/jpeg":
				$im = imagecreatefromjpeg($rel); //jpeg file
				break;
			case "image/gif":
				$im = imagecreatefromgif($rel); //gif file
				break;
			case "image/png":
				$im = imagecreatefrompng($rel); //png file
				break;
			default:
				log_message('error', 'page.php/is_bw(): no mime found');
				return false;
		}

		// Get image two sizes
		$imgw = imagesx($im);
		$imgh = imagesy($im);

		$r = array();
		$g = array();
		$b = array();

		$c = 0;

		for ($i = 0; $i < $imgw; $i++) {
			for ($j = 0; $j < $imgh; $j++) {

				// get the rgb value for current pixel
				$rgb = ImageColorAt($im, $i, $j);

				// extract each value for r, g, b
				$r[$i][$j] = ($rgb >> 16) & 0xFF;
				$g[$i][$j] = ($rgb >> 8) & 0xFF;
				$b[$i][$j] = $rgb & 0xFF;

				// count gray pixels (r=g=b)
				if ($r[$i][$j] == $g[$i][$j] && $r[$i][$j] == $b[$i][$j]) {
					$c++;
				}
			}
		}

		// If every pixel is black and white, return bw
		if ($c == ($imgw * $imgh)) {
			return "bw";
		}
		else {
			// The image is in colors
			return "rgb";
		}
	}
示例#24
0
 /**
  * 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);
 }
示例#25
0
 /**
  * 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);
     }
 }
示例#26
0
文件: game.php 项目: zhy9934/AoWoW
function coord_mangos2wow($mapid, $x, $y, $global)
{
    // Карты
    global $map_images;
    // Подключение к базе
    global $DB;
    $rows = $DB->select("SELECT * FROM ?_zones WHERE (mapID=? and x_min<? and x_max>? and y_min<? and y_max>?)", $mapid, $x, $x, $y, $y);
    foreach ($rows as $numRow => $row) {
        // Сохраяняем имя карты и координаты
        $wow['zone'] = $row['areatableID'];
        $wow['name'] = $row['name'];
        // Т.к. в игре координаты начинают отсчёт с левого верхнего угла
        //  а в системе координат сервера с правого нижнего,
        //  делаем соответствующее преобразование.
        $tx = 100 - ($y - $row["y_min"]) / (($row["y_max"] - $row["y_min"]) / 100);
        $ty = 100 - ($x - $row["x_min"]) / (($row["x_max"] - $row["x_min"]) / 100);
        // А если ещё и с цветом совпала - нах цикл, это всё наше :) Оо
        // Если ещё не загружена - загружаем.
        if (!isset($map_images[$wow['zone']])) {
            $mapname = str_replace("\\", "/", getcwd()) . '/images/tmp/' . $row['areatableID'] . '.png';
            if (file_exists($mapname)) {
                $map_images[$wow['zone']] = @ImageCreateFromPNG($mapname);
            } else {
                echo "<font color=red>....Map {$mapname} not found (ID=" . $wow['zone'] . ")</font><br>";
            }
        }
        // Если так и не загрузилась... Возможно такой карты ещё просто нету :)
        if ($map_images[$wow['zone']]) {
            if (@ImageColorAt($map_images[$wow['zone']], round($tx * 10), round($ty * 10)) === 0) {
                break;
            }
        }
    }
    if (count($rows) == 0) {
        // Ничего не найдено. Мб инста??
        $row = $DB->selectRow("SELECT * FROM ?_zones WHERE (mapID=? and x_min=0 and x_max=0 and y_min=0 and y_max=0)", $mapid);
        if ($row) {
            $wow['zone'] = $row['areatableID'];
            $wow['name'] = $row['name'];
        } else {
            echo "<font color=red>....Location for Map with ID={$mapid} not found</font><br>";
            return;
        }
    }
    $wow['x'] = round($tx, 4);
    $wow['y'] = round($ty, 4);
    return $wow;
}
示例#27
0
 function drawQSLine($image, $x1, $y1, $x2, $y2, $r, $g, $b)
 {
     $icr = $r;
     $icg = $g;
     $icb = $b;
     $dcol = ImageColorAllocate($image, $icr, $icg, $icb);
     if ($y1 == $y2) {
         imageline($image, $x1, $y1, $x2, $y1, $dcol);
     } else {
         if ($x1 == $x2) {
             imageline($image, $x1, $y1, $x1, $y2, $dcol);
         } else {
             $m = ($y2 - $y1) / ($x2 - $x1);
             $b = $y1 - $m * $x1;
             if (abs($m) < 2) {
                 $x = min($x1, $x2);
                 $endx = max($x1, $x2) + 1;
                 while ($x < $endx) {
                     $y = $m * $x + $b;
                     $y == floor($y) ? $ya = 1 : ($ya = $y - floor($y));
                     $yb = ceil($y) - $y;
                     $trgb = ImageColorAt($image, $x, floor($y));
                     $tcr = $trgb >> 16 & 0xff;
                     $tcg = $trgb >> 8 & 0xff;
                     $tcb = $trgb & 0xff;
                     imagesetpixel($image, $x, floor($y), imagecolorallocate($image, $tcr * $ya + $icr * $yb, $tcg * $ya + $icg * $yb, $tcb * $ya + $icb * $yb));
                     $trgb = ImageColorAt($image, $x, ceil($y));
                     $tcr = $trgb >> 16 & 0xff;
                     $tcg = $trgb >> 8 & 0xff;
                     $tcb = $trgb & 0xff;
                     imagesetpixel($image, $x, ceil($y), imagecolorallocate($image, $tcr * $yb + $icr * $ya, $tcg * $yb + $icg * $ya, $tcb * $yb + $icb * $ya));
                     $x++;
                 }
                 # while_x_end
             } else {
                 # else_abs_end
                 $y = min($y1, $y2);
                 $endy = max($y1, $y2) + 1;
                 while ($y < $endy) {
                     $x = ($y - $b) / $m;
                     $x == floor($x) ? $xa = 1 : ($xa = $x - floor($x));
                     $xb = ceil($x) - $x;
                     $trgb = ImageColorAt($image, floor($x), $y);
                     $tcr = $trgb >> 16 & 0xff;
                     $tcg = $trgb >> 8 & 0xff;
                     $tcb = $trgb & 0xff;
                     imagesetpixel($image, floor($x), $y, imagecolorallocate($image, $tcr * $xa + $icr * $xb, $tcg * $xa + $icg * $xb, $tcb * $xa + $icb * $xb));
                     $trgb = ImageColorAt($image, ceil($x), $y);
                     $tcr = $trgb >> 16 & 0xff;
                     $tcg = $trgb >> 8 & 0xff;
                     $tcb = $trgb & 0xff;
                     imagesetpixel($image, ceil($x), $y, imagecolorallocate($image, $tcr * $xb + $icr * $xa, $tcg * $xb + $icg * $xa, $tcb * $xb + $icb * $xa));
                     $y++;
                 }
                 # while_y_end
             }
             # else_abs_end
         }
     }
     # else_y=y_x=x_end
 }
 /**
  * Retourne la couleur d'un pixel dans une image
  *
  * @param ressource $img
  * @param int $x
  * @param int $y
  * @return array|bool
  */
 public static function GetPixelColor(&$img, $x, $y)
 {
     if (!is_resource($img)) {
         return false;
     }
     return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
 }
示例#29
0
 /**
 * @return float
 * @param resource $im
 * @param int $x
 * @param int $y
 * @desc Determines the intensity value of a pixel at the passed co-ordinates
 */
 function _getThresholdValue($im, $x, $y)
 {
     $rgb = ImageColorAt($im, $x, $y);
     $r = $rgb >> 16 & 0xff;
     $g = $rgb >> 8 & 0xff;
     $b = $rgb & 0xff;
     $intensity = ($r + $g + $b) / 3;
     return $intensity;
 }
示例#30
-1
 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);
             }
         }
     }
 }