/** * Method to apply a filter to an image resource. * * @param array $options An array of options for the filter. * * @return void * * @throws InvalidArgumentException * @throws RuntimeException */ public function execute(array $options = array()) { // Verify that image filter support for PHP is available. if (!function_exists('imagefilter')) { throw new RuntimeException('The imagefilter function for PHP is not available.'); } if (empty($options)) { throw new InvalidArgumentException('No valid amount was given. Expected float.'); } $value = (int) array_shift($options); if ($value == 0) { $value = 128; } $width = imagesx($this->handle); $height = imagesy($this->handle); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $index = imagecolorat($this->handle, $x, $y); $rgb = imagecolorsforindex($this->handle, $index); $r = $rgb['red']; $g = $rgb['green']; $b = $rgb['blue']; $a = $rgb['alpha']; $v = round(($r + $g + $b) / 3) >= $value ? 255 : 0; $color = imagecolorallocatealpha($this->handle, $v, $v, $v, $a); if ($color === false) { $color = imagecolorclosestalpha($this->handle, $v, $v, $v, $a); } imagesetpixel($this->handle, $x, $y, $color); } } }
function getClosestColorAlpha($R, $G = null, $B = null, $A = null) { if (is_array($R)) { return imagecolorclosestalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']); } else { return imagecolorclosestalpha($this->handle, $R, $G, $B, $A); } }
/** * Method to apply a filter to an image resource. * * @param array $options An array of options for the filter. * * @return void * * @throws InvalidArgumentException * @throws RuntimeException */ public function execute(array $options = array()) { // Verify that image filter support for PHP is available. if (!function_exists('imagefilter')) { throw new RuntimeException('The imagefilter function for PHP is not available.'); } if (empty($options)) { throw new InvalidArgumentException('No valid amount was given. Expected float.'); } // percent value $value = (int) array_shift($options); if ($value == 0) { return true; } $value = $value / 100; // set max / min $value = max(-1, min(1, $value)); // get width / height $width = imagesx($this->handle); $height = imagesy($this->handle); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $index = imagecolorat($this->handle, $x, $y); $rgb = imagecolorsforindex($this->handle, $index); $r = $rgb['red']; $g = $rgb['green']; $b = $rgb['blue']; $a = $rgb['alpha']; $average = ($r + $g + $b) / 3; $r += round(($r - $average) * $value); $g += round(($g - $average) * $value); $b += round(($b - $average) * $value); // clamp $r = min(255, max(0, $r)); $g = min(255, max(0, $g)); $b = min(255, max(0, $b)); $color = imagecolorallocatealpha($this->handle, $r, $g, $b, $a); if ($color === false) { $color = imagecolorclosestalpha($this->handle, $r, $g, $b, $a); } imagesetpixel($this->handle, $x, $y, $color); } } }
/** * Method to apply a filter to an image resource. * * @param array $options An array of options for the filter. * * @return void * * @throws InvalidArgumentException * @throws RuntimeException */ public function execute(array $options = array()) { // Verify that image filter support for PHP is available. if (!function_exists('imagefilter')) { throw new RuntimeException('The imagefilter function for PHP is not available.'); } if (empty($options)) { throw new InvalidArgumentException('No valid amount was given. Expected float.'); } $amount = (int) array_shift($options); // 39, 14, -36 /*if (imagefilter($this->handle, IMG_FILTER_GRAYSCALE)) { imagefilter($this->handle, IMG_FILTER_COLORIZE, 39, 14, -36); }*/ // Microsoft Sepia $width = imagesx($this->handle); $height = imagesy($this->handle); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $index = imagecolorat($this->handle, $x, $y); $rgb = imagecolorsforindex($this->handle, $index); $r = $rgb['red']; $g = $rgb['green']; $b = $rgb['blue']; $a = $rgb['alpha']; $v1 = $r * 0.393 + $g * 0.769 + $b * 0.189; $v2 = $r * 0.349 + $g * 0.6860000000000001 + $b * 0.168; $v3 = $r * 0.272 + $g * 0.534 + $b * 0.131; // clamp $v1 = min(255, max(0, $v1)); $v2 = min(255, max(0, $v2)); $v3 = min(255, max(0, $v3)); $color = imagecolorallocatealpha($this->handle, $v1, $v2, $v3, $a); if ($color === false) { $color = imagecolorclosestalpha($this->handle, $v1, $v2, $v3, $a); } imagesetpixel($this->handle, $x, $y, $color); } } }
/** * imageconvolution() does not appear in PHP with non-bundled GD libraries. * Because this is written in PHP, it is much slower than the bundled version. * * @param resource $image an image resource * @param array $matrix a 3x3 matrix: an array of three arrays of three floats * @param float $div the divisor of the result of the convolution, used for normalization * @param float $offset color offset * @return bool true on success or false on failure * @access private */ private function imageconvolution($image, $matrix, $div, $offset) { if ($image == null) { return 0; } $srcW = imagesx($image); $srcH = imagesy($image); $pxl = array(1, 1); $tmp = imagecreatetruecolor($srcW, $srcH); imagealphablending($tmp, false); imagealphablending($image, false); imagecopy($tmp, $image, 0, 0, 0, 0, $srcW, $srcH); if ($tmp == null) { return 0; } for ($y = 0; $y < $srcH; ++$y) { for ($x = 0; $x < $srcW; ++$x) { $newR = $newG = $newB = 0; $alpha = imagecolorat($tmp, @$pxl[0], @$pxl[1]); $newA = $alpha >> 24; for ($j = 0; $j < 3; ++$j) { $yv = min(max($y - 1 + $j, 0), $srcH - 1); for ($i = 0; $i < 3; ++$i) { $pxl = array(min(max($x - 1 + $i, 0), $srcW - 1), $yv); $rgb = imagecolorat($tmp, $pxl[0], $pxl[1]); $newR += ($rgb >> 16 & 0xff) * $matrix[$j][$i]; $newG += ($rgb >> 8 & 0xff) * $matrix[$j][$i]; $newB += ($rgb & 0xff) * $matrix[$j][$i]; $newA += ((0x7f000000 & $rgb) >> 24) * $matrix[$j][$i]; } } $newR = $newR / $div + $offset; $newG = $newG / $div + $offset; $newB = $newB / $div + $offset; $newA = $newA / $div + $offset; $newR = $newR > 255 ? 255 : ($newR < 0 ? 0 : $newR); $newG = $newG > 255 ? 255 : ($newG < 0 ? 0 : $newG); $newB = $newB > 255 ? 255 : ($newB < 0 ? 0 : $newB); $newA = $newA > 127 ? 127 : ($newA < 0 ? 0 : $newA); $newCol = imagecolorallocatealpha($image, (int) $newR, (int) $newG, (int) $newB, (int) $newA); if ($newCol == -1) { $newCol = imagecolorclosestalpha($image, (int) $newR, (int) $newG, (int) $newB, (int) $newA); } if ($y >= 0 && $y < $srcH) { imagesetpixel($image, $x, $y, $newCol); } } } imagedestroy($tmp); return 1; }
function imageconvolution($src, $filter, $filter_div, $offset) { if ($src == NULL) { return false; } $sx = imagesx($src); $sy = imagesy($src); $srcback = imagecreatetruecolor($sx, $sy); imagecopy($srcback, $src, 0, 0, 0, 0, $sx, $sy); if ($srcback == NULL) { return 0; } for ($y = 0; $y < $sy; ++$y) { for ($x = 0; $x < $sx; ++$x) { $new_r = $new_g = $new_b = 0; $alpha = imagecolorat($srcback, $pxl[0], $pxl[1]); $new_a = $alpha >> 24; for ($j = 0; $j < 3; ++$j) { $yv = min(max($y - 1 + $j, 0), $sy - 1); for ($i = 0; $i < 3; ++$i) { $pxl = array(min(max($x - 1 + $i, 0), $sx - 1), $yv); $rgb = imagecolorat($srcback, $pxl[0], $pxl[1]); $new_r += ($rgb >> 16 & 0xff) * $filter[$j][$i]; $new_g += ($rgb >> 8 & 0xff) * $filter[$j][$i]; $new_b += ($rgb & 0xff) * $filter[$j][$i]; } } $new_r = $new_r / $filter_div + $offset; $new_g = $new_g / $filter_div + $offset; $new_b = $new_b / $filter_div + $offset; $new_r = $new_r > 255 ? 255 : ($new_r < 0 ? 0 : $new_r); $new_g = $new_g > 255 ? 255 : ($new_g < 0 ? 0 : $new_g); $new_b = $new_b > 255 ? 255 : ($new_b < 0 ? 0 : $new_b); $new_pxl = imagecolorallocatealpha($src, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); if ($new_pxl == -1) { $new_pxl = imagecolorclosestalpha($src, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); } if ($y >= 0 && $y < $sy) { imagesetpixel($src, $x, $y, $new_pxl); } } } imagedestroy($srcback); return true; }
function imageconvolution($src, $filter, $filter_div, $offset) { if ($src == NULL) { return 0; } $sx = imagesx($src); $sy = imagesy($src); $srcback = imagecreatetruecolor($sx, $sy); imagecopy($srcback, $src, 0, 0, 0, 0, $sx, $sy); if ($srcback == NULL) { return 0; } // Fix here // $pxl array was the problem so simply set it with very low values $pxl = array(1, 1); // this little fix worked for me as the undefined array threw out errors for ($y = 0; $y < $sy; ++$y) { for ($x = 0; $x < $sx; ++$x) { $new_r = $new_g = $new_b = 0; $alpha = imagecolorat($srcback, $pxl[0], $pxl[1]); $new_a = $alpha >> 24; for ($j = 0; $j < 3; ++$j) { $yv = min(max($y - 1 + $j, 0), $sy - 1); for ($i = 0; $i < 3; ++$i) { $pxl = array(min(max($x - 1 + $i, 0), $sx - 1), $yv); $rgb = imagecolorat($srcback, $pxl[0], $pxl[1]); $new_r += ($rgb >> 16 & 0xff) * $filter[$j][$i]; $new_g += ($rgb >> 8 & 0xff) * $filter[$j][$i]; $new_b += ($rgb & 0xff) * $filter[$j][$i]; } } $new_r = $new_r / $filter_div + $offset; $new_g = $new_g / $filter_div + $offset; $new_b = $new_b / $filter_div + $offset; $new_r = $new_r > 255 ? 255 : ($new_r < 0 ? 0 : $new_r); $new_g = $new_g > 255 ? 255 : ($new_g < 0 ? 0 : $new_g); $new_b = $new_b > 255 ? 255 : ($new_b < 0 ? 0 : $new_b); $new_pxl = imagecolorallocatealpha($src, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); if ($new_pxl == -1) { $new_pxl = imagecolorclosestalpha($src, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); } if ($y >= 0 && $y < $sy) { imagesetpixel($src, $x, $y, $new_pxl); } } } imagedestroy($srcback); return 1; }
public function getColorIndex($img, $color) { // Now set the destination pixel. $colorIndex = imagecolorallocatealpha($img, $color['red'], $color['green'], $color['blue'], $color['alpha']); // If we failed to allocate the color, try to find the already allocated color // that is closest to what we want. if ($colorIndex === false) { $colorIndex = imagecolorclosestalpha($img, $color['red'], $color['green'], $color['blue'], $color['alpha']); } return $colorIndex; }
function imagefilter($source, $var, $arg1 = null, $arg2 = null, $arg3 = null) { #define('IMAGE_FILTER_NEGATE',0); #define('IMAGE_FILTER_GRAYSCALE',0); #define('IMAGE_FILTER_BRIGHTNESS',2); #define('IMAGE_FILTER_CONTRAST',3); #define('IMAGE_FILTER_COLORIZE',4); #define('IMAGE_FILTER_EDGEDETECT',5); #define('IMAGE_FILTER_EMBOSS',6); #define('IMAGE_FILTER_GAUSSIAN_BLUR',7); #define('IMAGE_FILTER_SELECTIVE_BLUR',8); #define('IMAGE_FILTER_MEAN_REMOVAL',9); #define('IMAGE_FILTER_SMOOTH',10); $max_y = imagesy($source); $max_x = imagesx($source); switch ($var) { case 0: $y = 0; while ($y < $max_y) { $x = 0; while ($x < $max_x) { $rgb = imagecolorat($source, $x, $y); $r = 255 - ($rgb >> 16 & 0xff); $g = 255 - ($rgb >> 8 & 0xff); $b = 255 - ($rgb & 0xff); $a = $rgb >> 24; $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a); if ($new_pxl == false) { $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a); } imagesetpixel($source, $x, $y, $new_pxl); ++$x; } ++$y; } return true; break; case 1: $y = 0; while ($y < $max_y) { $x = 0; while ($x < $max_x) { $rgb = imagecolorat($source, $x, $y); $a = $rgb >> 24; $r = ($rgb >> 16 & 0xff) * 0.299 + ($rgb >> 8 & 0xff) * 0.587 + ($rgb & 0xff) * 0.114; $new_pxl = imagecolorallocatealpha($source, $r, $r, $r, $a); if ($new_pxl == false) { $new_pxl = imagecolorclosestalpha($source, $r, $r, $r, $a); } imagesetpixel($source, $x, $y, $new_pxl); ++$x; } ++$y; } return true; break; case 2: $y = 0; while ($y < $max_y) { $x = 0; while ($x < $max_x) { $rgb = imagecolorat($source, $x, $y); $r = ($rgb >> 16 & 0xff) + $arg1; $g = ($rgb >> 8 & 0xff) + $arg1; $b = ($rgb & 0xff) + $arg1; $a = $rgb >> 24; $r = $r > 255 ? 255 : ($r < 0 ? 0 : $r); $g = $g > 255 ? 255 : ($g < 0 ? 0 : $g); $b = $b > 255 ? 255 : ($b < 0 ? 0 : $b); $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a); if ($new_pxl == false) { $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a); } imagesetpixel($source, $x, $y, $new_pxl); ++$x; } ++$y; } return true; break; case 3: $contrast = pow((100 - $arg1) / 100, 2); $y = 0; while ($y < $max_y) { $x = 0; while ($x < $max_x) { $rgb = imagecolorat($source, $x, $y); $a = $rgb >> 24; $r = ((($rgb >> 16 & 0xff) / 255 - 0.5) * $contrast + 0.5) * 255; $g = ((($rgb >> 8 & 0xff) / 255 - 0.5) * $contrast + 0.5) * 255; $b = ((($rgb & 0xff) / 255 - 0.5) * $contrast + 0.5) * 255; $r = $r > 255 ? 255 : ($r < 0 ? 0 : $r); $g = $g > 255 ? 255 : ($g < 0 ? 0 : $g); $b = $b > 255 ? 255 : ($b < 0 ? 0 : $b); $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a); if ($new_pxl == false) { $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a); } imagesetpixel($source, $x, $y, $new_pxl); ++$x; } ++$y; } return true; break; case 4: $x = 0; while ($x < $max_x) { $y = 0; while ($y < $max_y) { $rgb = imagecolorat($source, $x, $y); $r = ($rgb >> 16 & 0xff) + $arg1; $g = ($rgb >> 8 & 0xff) + $arg2; $b = ($rgb & 0xff) + $arg3; $a = $rgb >> 24; $r = $r > 255 ? 255 : ($r < 0 ? 0 : $r); $g = $g > 255 ? 255 : ($g < 0 ? 0 : $g); $b = $b > 255 ? 255 : ($b < 0 ? 0 : $b); $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a); if ($new_pxl == false) { $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a); } imagesetpixel($source, $x, $y, $new_pxl); ++$y; } ++$x; } return true; break; case 5: return imageconvolution($source, array(array(-1, 0, -1), array(0, 4, 0), array(-1, 0, -1)), 1, 127); break; case 6: return imageconvolution($source, array(array(1.5, 0, 0), array(0, 0, 0), array(0, 0, -1.5)), 1, 127); break; case 7: return imageconvolution($source, array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)), 16, 0); break; case 8: for ($y = 0; $y < $max_y; $y++) { for ($x = 0; $x < $max_x; $x++) { $flt_r_sum = $flt_g_sum = $flt_b_sum = 0; $cpxl = imagecolorat($source, $x, $y); for ($j = 0; $j < 3; $j++) { for ($i = 0; $i < 3; $i++) { if ($j == 1 && $i == 1) { $flt_r[1][1] = $flt_g[1][1] = $flt_b[1][1] = 0.5; } else { $pxl = imagecolorat($source, $x - (3 >> 1) + $i, $y - (3 >> 1) + $j); $new_a = $pxl >> 24; //$r = (($pxl >> 16) & 0xFF); //$g = (($pxl >> 8) & 0xFF); //$b = ($pxl & 0xFF); $new_r = abs(($cpxl >> 16 & 0xff) - ($pxl >> 16 & 0xff)); if ($new_r != 0) { $flt_r[$j][$i] = 1 / $new_r; } else { $flt_r[$j][$i] = 1; } $new_g = abs(($cpxl >> 8 & 0xff) - ($pxl >> 8 & 0xff)); if ($new_g != 0) { $flt_g[$j][$i] = 1 / $new_g; } else { $flt_g[$j][$i] = 1; } $new_b = abs(($cpxl & 0xff) - ($pxl & 0xff)); if ($new_b != 0) { $flt_b[$j][$i] = 1 / $new_b; } else { $flt_b[$j][$i] = 1; } } $flt_r_sum += $flt_r[$j][$i]; $flt_g_sum += $flt_g[$j][$i]; $flt_b_sum += $flt_b[$j][$i]; } } for ($j = 0; $j < 3; $j++) { for ($i = 0; $i < 3; $i++) { if ($flt_r_sum != 0) { $flt_r[$j][$i] /= $flt_r_sum; } if ($flt_g_sum != 0) { $flt_g[$j][$i] /= $flt_g_sum; } if ($flt_b_sum != 0) { $flt_b[$j][$i] /= $flt_b_sum; } } } $new_r = $new_g = $new_b = 0; for ($j = 0; $j < 3; $j++) { for ($i = 0; $i < 3; $i++) { $pxl = imagecolorat($source, $x - (3 >> 1) + $i, $y - (3 >> 1) + $j); $new_r += ($pxl >> 16 & 0xff) * $flt_r[$j][$i]; $new_g += ($pxl >> 8 & 0xff) * $flt_g[$j][$i]; $new_b += ($pxl & 0xff) * $flt_b[$j][$i]; } } $new_r = $new_r > 255 ? 255 : ($new_r < 0 ? 0 : $new_r); $new_g = $new_g > 255 ? 255 : ($new_g < 0 ? 0 : $new_g); $new_b = $new_b > 255 ? 255 : ($new_b < 0 ? 0 : $new_b); $new_pxl = ImageColorAllocateAlpha($source, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); if ($new_pxl == false) { $new_pxl = ImageColorClosestAlpha($source, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); } imagesetpixel($source, $x, $y, $new_pxl); } } return true; break; case 9: return imageconvolution($source, array(array(-1, -1, -1), array(-1, 9, -1), array(-1, -1, -1)), 1, 0); break; case 10: return imageconvolution($source, array(array(1, 1, 1), array(1, $arg1, 1), array(1, 1, 1)), $arg1 + 8, 0); break; } }
public function closest(string $rgb) : int { $rgb = explode('|', $rgb); $red = isset($rgb[0]) ? $rgb[0] : 0; $green = isset($rgb[1]) ? $rgb[1] : 0; $blue = isset($rgb[2]) ? $rgb[2] : 0; $alpha = isset($rgb[3]) ? $rgb[3] : 0; return imagecolorclosestalpha($this->canvas, $red, $green, $blue, $alpha); }
$AF_colorize_RGB = array(base_convert(substr($colorize, 0, 2), 16, 10), base_convert(substr($colorize, 2, 2), 16, 10), base_convert(substr($colorize, 4, 2), 16, 10)); $x = 0; while ($x < $src_w) { $y = 0; while ($y < $src_h) { $rgb = imagecolorat($src_img, $x, $y); $r = ($rgb >> 16 & 0xff) + $AF_colorize_RGB[0]; $g = ($rgb >> 8 & 0xff) + $AF_colorize_RGB[1]; $b = ($rgb & 0xff) + $AF_colorize_RGB[2]; $a = $rgb >> 24; $r = $r > 255 ? 255 : ($r < 0 ? 0 : $r); $g = $g > 255 ? 255 : ($g < 0 ? 0 : $g); $b = $b > 255 ? 255 : ($b < 0 ? 0 : $b); $new_pxl = imagecolorallocatealpha($src_img, $r, $g, $b, $a); if ($new_pxl == false) { $new_pxl = imagecolorclosestalpha($src_img, $r, $g, $b, $a); } imagesetpixel($src_img, $x, $y, $new_pxl); ++$y; } ++$x; } } $dst_img = imagecreatetruecolor($dst_w, $dst_h); $AF_bgcolor_RGB = array(base_convert(substr($bgcolor, 0, 2), 16, 10), base_convert(substr($bgcolor, 2, 2), 16, 10), base_convert(substr($bgcolor, 4, 2), 16, 10)); $AF_BGCOLOR = imagecolorallocatealpha($dst_img, $AF_bgcolor_RGB[0], $AF_bgcolor_RGB[1], $AF_bgcolor_RGB[2], 127); imagefill($dst_img, 0, 0, $AF_BGCOLOR); imagecopyresampled($dst_img, $src_img, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); header('Content-type: image/png'); imagesavealpha($dst_img, true); imagepng($dst_img, NULL, 0);
/** * Watermark - Text Version * * @access public * @return bool */ function text_watermark() { if (!($src_img = $this->image_create_gd())) { return FALSE; } if ($this->wm_use_truetype == TRUE and !file_exists($this->wm_font_path)) { $this->set_error('imglib_missing_font'); return FALSE; } // Fetch source image properties $this->get_image_properties(); // Set RGB values for text and shadow $this->wm_font_color = str_replace('#', '', $this->wm_font_color); $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color); $R1 = hexdec(substr($this->wm_font_color, 0, 2)); $G1 = hexdec(substr($this->wm_font_color, 2, 2)); $B1 = hexdec(substr($this->wm_font_color, 4, 2)); $R2 = hexdec(substr($this->wm_shadow_color, 0, 2)); $G2 = hexdec(substr($this->wm_shadow_color, 2, 2)); $B2 = hexdec(substr($this->wm_shadow_color, 4, 2)); // Reverse the vertical offset // When the image is positioned at the bottom // we don't want the vertical offset to push it // further down. We want the reverse, so we'll // invert the offset. Note: The horizontal // offset flips itself automatically if ($this->wm_vrt_alignment == 'B') { $this->wm_vrt_offset = $this->wm_vrt_offset * -1; } if ($this->wm_hor_alignment == 'R') { $this->wm_hor_offset = $this->wm_hor_offset * -1; } // Set font width and height // These are calculated differently depending on // whether we are using the true type font or not if ($this->wm_use_truetype == TRUE) { if ($this->wm_font_size == '') { $this->wm_font_size = '17'; } //$fontwidth = $this->wm_font_size-($this->wm_font_size/4); //AG_EDIT $fontwidth = $this->wm_font_size - $this->wm_font_size / 4; $fontheight = $this->wm_font_size; //$this->wm_vrt_offset += $this->wm_font_size; } else { $fontwidth = imagefontwidth($this->wm_font_size); $fontheight = imagefontheight($this->wm_font_size); } // Set base X and Y axis values $x_axis = $this->wm_hor_offset + $this->wm_padding; $y_axis = $this->wm_vrt_offset + $this->wm_padding; // Set verticle alignment if ($this->wm_use_drop_shadow == FALSE) { $this->wm_shadow_distance = 0; } $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1)); $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1)); if (!$this->wm_use_truetype) { switch ($this->wm_vrt_alignment) { case "T": break; case "M": $y_axis += $this->orig_height / 2 + $fontheight / 2; break; case "B": $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - $fontheight / 2; break; } $x_shad = $x_axis + $this->wm_shadow_distance; $y_shad = $y_axis + $this->wm_shadow_distance; // Set horizontal alignment switch ($this->wm_hor_alignment) { case "L": break; case "R": //if ($this->wm_use_drop_shadow) //$x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text)); //$x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text)); //AG_EDIT if ($this->wm_use_drop_shadow) { $x_shad += $this->orig_width - $fontwidth * mb_strlen($this->wm_text); } $x_axis += $this->orig_width - $fontwidth * mb_strlen($this->wm_text); break; case "C": //if ($this->wm_use_drop_shadow) //$x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2); //$x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2); //AG_EDIT if ($this->wm_use_drop_shadow) { $x_shad += floor(($this->orig_width - $fontwidth * mb_strlen($this->wm_text)) / 2); } $x_axis += floor(($this->orig_width - $fontwidth * mb_strlen($this->wm_text)) / 2); break; } } // Add the text to the source image if ($this->wm_use_truetype) { //Ширина прямоугольника с текстом $XY = imageftbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text); $Textwidth = $XY[2] - $XY[0]; //Проверка или прямоугольник не больше изображения с отступом от краем if ($Textwidth + $this->wm_shadow_distance > $this->orig_width - $x_axis - $this->orig_width / 20) { //Вычисляем максимально возможный размер шрифта через пропорцию $this->wm_font_size = floor(($this->orig_width - $x_axis - $this->orig_width / 20) * $this->wm_font_size / ($Textwidth + $this->wm_shadow_distance)); //Узнаем ширину прямоугольника с текстом $XY = imageftbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text); $Textwidth = $XY[2] - $XY[0]; } //Ширина создаваемого изображения с водяным знаком учитывая тень $TW = $Textwidth + $this->wm_shadow_distance; //Высота создаваемого изображения с водяным знаком + 50% от величины шрифта + учитывание тени $TH = floor($this->wm_font_size + $this->wm_font_size / 2 + $this->wm_shadow_distance); //Отступ от верхнего угла для imagettftext навно размеру шрифта + 20% - отступ тени $TY = floor($this->wm_font_size + $this->wm_font_size / 7 - $this->wm_shadow_distance); //После этих манипуляций текст не должен вылазить на пределы создаваемого изображения водяного знака $TIMG = imagecreatetruecolor($TW, $TH); imagealphablending($TIMG, TRUE); imagesavealpha($TIMG, true); //Цвет текста без прозрачности и цвет тени с прозрачность <> 40% $txt_color = imagecolorallocatealpha($TIMG, $R1, $G1, $B1, 0); $drp_color = imagecolorallocatealpha($TIMG, $R2, $G2, $B2, 50); $BGcolor = imagecolorclosestalpha($TIMG, 255, 255, 255, 127); imagefill($TIMG, 0, 0, $BGcolor); imagealphablending($TIMG, TRUE); //Наложение текстов на изображение imagettftext($TIMG, $this->wm_font_size, 0, $this->wm_shadow_distance, $TY + $this->wm_shadow_distance, $drp_color, $this->wm_font_path, $this->wm_text); imagettftext($TIMG, $this->wm_font_size, 0, 0, $TY, $txt_color, $this->wm_font_path, $this->wm_text); //Проверка выравнивания водяного знака по оси Y switch ($this->wm_vrt_alignment) { case "T": break; case "M": $y_axis += floor($this->orig_height / 2 - $TH / 2); break; case "B": $y_axis += $this->orig_height - $TH; break; } //Проверка выравнивания водяного знака по оси X switch ($this->wm_hor_alignment) { case "L": $x_axis += $this->orig_width / 40; break; case "R": $x_axis += floor($this->orig_width - $TW - $this->orig_width / 40); break; case "C": $x_axis += floor(($this->orig_width - $TW) / 2); break; } if ($this->wm_opacity > 0 && $this->wm_opacity < 100) { $this->filter_opacity($TIMG, $this->wm_opacity); } imagecopy($src_img, $TIMG, $x_axis, $y_axis, 0, 0, $TW, $TH); imagedestroy($TIMG); } else { if ($this->wm_use_drop_shadow) { imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); } imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); } // Output the final image if ($this->dynamic_output == TRUE) { $this->image_display_gd($src_img); } else { $this->image_save_gd($src_img); } imagedestroy($src_img); return TRUE; }
/** * An Old but Needed function for the Actual Days of PhP * @source http://forums.devnetwork.net/viewtopic.php?f=1&t=103330#p553333 * @author RedMonkey * @editor Claudio Santoro * * "@observation Sorry of this function need to be big. Is because the simpliest approaches found in internet" * "doesn't work as well that these approach.." * "i know oop need small functions, but sorry" * * @param resource $dst Destination Allocated Image * @param resource $src Source Allocated Image * @param int $dst_x Destination Position X * @param int $dst_y Destination Position Y * @param int $src_x Source Position X * @param int $src_y Source Position Y * @param int $w Width * @param int $h Height * @param int $pct Alpha Percent * @return null */ private function image_copy_merge_with_alpha($dst, $src, $dst_x = 0, $dst_y = 0, $src_x = 0, $src_y = 0, $w = 0, $h = 0, $pct = 100) { /* yes divide */ $pct /= 100; /* make sure opacity level is within range before going any further */ $pct = max(min(1, $pct), 0); /* work out if we need to bother correcting for opacity */ if ($pct < 1) { /* we need a copy of the original to work from, only copy the cropped */ /* area of src */ $src_copy = imagecreatetruecolor($w, $h); /* attempt to maintain alpha levels, alpha blending must be *off* */ imagealphablending($src_copy, false); imagesavealpha($src_copy, true); imagecopy($src_copy, $src, 0, 0, $src_x, $src_y, $w, $h); /* we need to know the max transparency of the image */ $max_t = 0; for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { $src_c = imagecolorat($src_copy, $x, $y); $src_a = $src_c >> 24 & 0xff; $max_t = $src_a > $max_t ? $src_a : $max_t; } } /* src has no transparency? set it to use full alpha range */ $max_t = $max_t == 0 ? 127 : $max_t; /* $max_t is now being reused as the correction factor to apply based */ /* on the original transparency range of src */ $max_t /= 127; /* go back through the image adjusting alpha channel as required */ for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { $src_c = imagecolorat($src, $src_x + $x, $src_y + $y); $src_a = $src_c >> 24 & 0xff; $src_r = $src_c >> 16 & 0xff; $src_g = $src_c >> 8 & 0xff; $src_b = $src_c & 0xff; /* alpha channel compensation */ $src_a = ($src_a + 127 - 127 * $pct) * $max_t; $src_a = $src_a > 127 ? 127 : (int) $src_a; /* get and set this pixel's adjusted RGBA colour index */ $rgba = imagecolorallocatealpha($src_copy, $src_r, $src_g, $src_b, $src_a); /* @method /imagecolorclosestalpha returns -1 for PHP versions prior */ /* to 5.1.3 when allocation failed */ if ($rgba === false || $rgba == -1) { $rgba = imagecolorclosestalpha($src_copy, $src_r, $src_g, $src_b, $src_a); } imagesetpixel($src_copy, $x, $y, $rgba); } } /* call image copy passing our alpha adjusted image as src */ imagecopy($dst, $src_copy, $dst_x, $dst_y, 0, 0, $w, $h); /* cleanup, free memory */ imagedestroy($src_copy); return null; } /* still here? no opacity adjustment required so pass straight through to */ /* @method /imagecopy rather than @method /imagecopymerge to retain alpha channels */ imagecopy($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h); return null; }
function chinese_yzimg($str) { $fnt = ROOT_PATH . "inc/font.ttf"; if (WEB_LANG == 'gb2312') { $str = gbk2utf8($str); } $c = 'red'; $x = 0; $y = 0; $size = 11; $image = ROOT_PATH . 'images/default/chinese_yzimg.jpg'; if (!is_file($image)) { die('背景图片不存在'); } $img_array = getimagesize($image); $font_array = ImageTTFBBox($size, 0, $fnt, $str); $font_wight = intval($font_array[2] - $font_array[0]); $font_height = intval($font_array[3] - $font_array[5]); $x || ($x = intval(($img_array[0] - $font_wight) / 2)); $y || ($y = intval($img_array[1] / 2 + $font_height / 2)); $im = imagecreatefromjpeg($image); if ($c == 'blue') { $color = imagecolorclosestalpha($im, 00, 00, 255, 20); $color2 = imagecolorclosestalpha($im, 00, 00, 00, 98); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } elseif ($c == 'white') { $color = imagecolorclosestalpha($im, 255, 255, 255, 20); $color2 = imagecolorclosestalpha($im, 00, 00, 00, 99); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } elseif ($c == 'red') { $color = imagecolorclosestalpha($im, 255, 00, 00, 20); $color2 = imagecolorclosestalpha($im, 255, 255, 255, 20); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } else { $color = imagecolorclosestalpha($im, 00, 00, 00, 20); $color2 = imagecolorclosestalpha($im, 255, 255, 255, 40); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } imagettftext($im, $size, 0, $x, $y, $color, $fnt, $str); ImageJPEG($im); ImageDestroy($im); }
public function closest($rgb = '') { if (!is_string($rgb)) { Error::set(lang('Error', 'stringParameter', '1.(rgb)')); return false; } $rgb = explode('|', $rgb); $red = isset($rgb[0]) ? $rgb[0] : 0; $green = isset($rgb[1]) ? $rgb[1] : 0; $blue = isset($rgb[2]) ? $rgb[2] : 0; $alpha = isset($rgb[3]) ? $rgb[3] : 0; return imagecolorclosestalpha($this->canvas, $red, $green, $blue, $alpha); }
function WatermarkImage(&$obj, $Params = array()) { $file = $Params['file']; if (!$obj || empty($file) || !file_exists($file) || !is_file($file) || !function_exists("gd_info")) { return false; } $arFile = array("ext" => GetFileExtension($file)); $Params["width"] = intval(@imagesx($obj)); $Params["height"] = intval(@imagesy($obj)); $Params["coefficient"] = floatval($Params["coefficient"]); if (!isset($Params["alpha_level"])) { $Params["alpha_level"] = 100; } $Params["alpha_level"] = intval($Params["alpha_level"]) / 100; $wmWidth = round($Params["width"] * $Params["coefficient"]); $wmHeight = round($Params["height"] * $Params["coefficient"]); $arFileSizeTmp = CFile::GetImageSize($file); if (!in_array($arFileSizeTmp[2], array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_BMP))) { return false; } if ($Params["fill"] == 'resize') { $file_obj_1 = CFile::CreateImage($file, $arFileSizeTmp[2]); $arFile["width"] = intval(imagesx($file_obj_1)); $arFile["height"] = intval(imagesy($file_obj_1)); if ($arFile["width"] > $wmWidth || $arFile["height"] > $wmHeight) { $file_1 = $file . '_new.tmp'; CFile::ResizeImageFile($file, $file_1, array('width' => $wmWidth, 'height' => $wmHeight)); $file_obj = CFile::CreateImage($file_1, $arFileSizeTmp[2]); @imagedestroy($file_obj_1); } else { $file_obj = $file_obj_1; } } else { $file_obj = CFile::CreateImage($file, $arFileSizeTmp[2]); if ($Params["fill"] == 'repeat') { $Params["position"] = array('x' => 'top', 'y' => 'left'); } } if (!$file_obj) { return false; } $arFile["width"] = intval(@imagesx($file_obj)); $arFile["height"] = intval(@imagesy($file_obj)); $wm_pos = array("x" => 2, "y" => 2, "width" => $arFile["width"], "height" => $arFile["height"]); if ($Params["position"]['y'] == 'center') { $wm_pos["y"] = intval(($Params["height"] - $wm_pos["height"]) / 2); } elseif ($Params["position"]['y'] == 'bottom') { $wm_pos["y"] = intval($Params["height"] - $wm_pos["height"]); } if ($Params["position"]['x'] == 'center') { $wm_pos["x"] = intval(($Params["width"] - $wm_pos["width"]) / 2); } elseif ($Params["position"]['x'] == 'right') { $wm_pos["x"] = intval($Params["width"] - $wm_pos["width"]); } if ($wm_pos["y"] < 2) { $wm_pos["y"] = 2; } if ($wm_pos["x"] < 2) { $wm_pos["x"] = 2; } for ($y = 0; $y < $arFile["height"]; $y++) { for ($x = 0; $x < $arFile["width"]; $x++) { $watermark_y = $wm_pos["y"] + $y; while (true) { $watermark_x = $wm_pos["x"] + $x; while (true) { $return_color = NULL; $watermark_alpha = $Params["alpha_level"]; $main_rgb = imagecolorsforindex($obj, imagecolorat($obj, $watermark_x, $watermark_y)); $watermark_rbg = imagecolorsforindex($file_obj, imagecolorat($file_obj, $x, $y)); if ($watermark_rbg['alpha']) { $watermark_alpha = round((127 - $watermark_rbg['alpha']) / 127, 2); $watermark_alpha = $watermark_alpha * $Params["alpha_level"]; } $res = array(); foreach (array('red', 'green', 'blue', 'alpha') as $k) { $res[$k] = round($main_rgb[$k] * (1 - $watermark_alpha) + $watermark_rbg[$k] * $watermark_alpha); } $return_color = imagecolorexactalpha($obj, $res["red"], $res["green"], $res["blue"], $res["alpha"]); if ($return_color == -1) { $return_color = imagecolorallocatealpha($obj, $res["red"], $res["green"], $res["blue"], $res["alpha"]); if ($return_color == -1) { $return_color = imagecolorclosestalpha($obj, $res["red"], $res["green"], $res["blue"], $res["alpha"]); } } imagesetpixel($obj, $watermark_x, $watermark_y, $return_color); $watermark_x += $arFile["width"]; if ($Params["fill"] != 'repeat' || $watermark_x > $Params["width"]) { break; } } $watermark_y += $arFile["height"]; if ($Params["fill"] != 'repeat' || $watermark_y > $Params["height"]) { break; } } } } @imagedestroy($file_obj); return true; }
public function blend($baseImage, $topImage, $mode) { $baseIsTrueColor = imageistruecolor($baseImage); $topIsTrueColor = imageistruecolor($topImage); $baseWidth = imagesx($baseImage); $baseHeight = imagesy($baseImage); $topWidth = imagesx($topImage); $topHeight = imagesy($topImage); $destX = ($baseWidth - $topWidth) / 2; $destY = ($baseHeight - $topHeight) / 2; for ($x = 0; $x < $topWidth; ++$x) { for ($y = 0; $y < $topHeight; ++$y) { $color = imagecolorat($baseImage, $x + $destX, $y + $destY); if ($baseIsTrueColor) { $baseColor = array('red' => $color >> 16 & 0xff, 'green' => $color >> 8 & 0xff, 'blue' => $color & 0xff, 'alpha' => ($color & 0x7f000000) >> 24); } else { $baseColor = imagecolorsforindex($baseImage, $color); } $color = imagecolorat($topImage, $x, $y); if ($topIsTrueColor) { $topColor = array('red' => $color >> 16 & 0xff, 'green' => $color >> 8 & 0xff, 'blue' => $color & 0xff, 'alpha' => ($color & 0x7f000000) >> 24); } else { $topColor = imagecolorsforindex($topImage, $color); } switch ($mode) { case 'dissolve': $topOpacityPercent = round($topColor['alpha'] / 127 * 100); $randomPercent = rand(1, 100); if ($randomPercent <= $topOpacityPercent) { $destColorAlpha = 127.0; } else { $destColorAlpha = 0.0; } $destColor = array('red' => intval($topColor['red']), 'green' => intval($topColor['green']), 'blue' => intval($topColor['blue']), 'alpha' => intval($destColorAlpha)); break; case 'darkerColor': $baseColorHsl = $this->rgbToHsl($baseColor['red'], $baseColor['green'], $baseColor['green']); $topColorHsl = $this->rgbToHsl($topColor['red'], $topColor['green'], $topColor['blue']); if ($baseColorHsl['lightness'] < $topColorHsl['lightness']) { $destColor = array('red' => intval($baseColor['red']), 'green' => intval($baseColor['green']), 'blue' => intval($baseColor['blue']), 'alpha' => intval($baseColor['alpha'])); } else { $destColor = array('red' => intval($topColor['red']), 'green' => intval($topColor['green']), 'blue' => intval($topColor['blue']), 'alpha' => intval($topColor['alpha'])); } break; case 'darken': $destColor = array('red' => intval(min($baseColor['red'], $topColor['red'])), 'green' => intval(min($baseColor['green'], $topColor['green'])), 'blue' => intval(min($baseColor['blue'], $topColor['blue'])), 'alpha' => intval($topColor['alpha'])); break; default: case 'multiply': $destColor = array('red' => intval($baseColor['red'] * ($topColor['red'] / 255.0)), 'green' => intval($baseColor['green'] * ($topColor['green'] / 255.0)), 'blue' => intval($baseColor['blue'] * ($topColor['blue'] / 255.0)), 'alpha' => intval($topColor['alpha'])); break; case 'colorBurn': $destColor = array('red' => intval($baseColor['red'] * ($topColor['red'] / 255.0)), 'green' => intval($baseColor['green'] * ($topColor['green'] / 255.0)), 'blue' => intval($baseColor['blue'] * ($topColor['blue'] / 255.0)), 'alpha' => intval($topColor['alpha'])); break; case 'linearBurn': $destColor = array('red' => intval($baseColor['red'] + $topColor['red'] - 255.0), 'green' => intval($baseColor['green'] + $topColor['green'] - 255.0), 'blue' => intval($baseColor['blue'] + $topColor['blue'] - 255.0), 'alpha' => intval($topColor['alpha'])); break; case 'lighterColor': $baseColorHsl = $this->rgbToHsl($baseColor['red'], $baseColor['green'], $baseColor['green']); $topColorHsl = $this->rgbToHsl($topColor['red'], $topColor['green'], $topColor['blue']); if ($baseColorHsl['lightness'] > $topColorHsl['lightness']) { $destColor = array('red' => intval($baseColor['red']), 'green' => intval($baseColor['green']), 'blue' => intval($baseColor['blue']), 'alpha' => intval($baseColor['alpha'])); } else { $destColor = array('red' => intval($topColor['red']), 'green' => intval($topColor['green']), 'blue' => intval($topColor['blue']), 'alpha' => intval($topColor['alpha'])); } break; case 'lighten': $destColor = array('red' => intval(max($baseColor['red'], $topColor['red'])), 'green' => intval(max($baseColor['green'], $topColor['green'])), 'blue' => intval(max($baseColor['blue'], $topColor['blue'])), 'alpha' => intval($topColor['alpha'])); break; case 'screen': $destColor = array('red' => intval(round($baseColor['red'] + (255.0 - $baseColor['red']) / 100 * ($topColor['red'] / 255.0 * 100))), 'green' => intval(round($baseColor['green'] + (255.0 - $baseColor['green']) / 100 * ($topColor['green'] / 255.0 * 100))), 'blue' => intval(round($baseColor['blue'] + (255.0 - $baseColor['blue']) / 100 * ($topColor['blue'] / 255.0 * 100))), 'alpha' => intval($topColor['alpha'])); break; case 'colorDodge': $destColor = array('red' => intval($baseColor['red'] + $topColor['red'] > 255.0 ? 255.0 : $baseColor['red']), 'green' => intval($baseColor['green'] + $topColor['green'] > 255.0 ? 255.0 : $baseColor['green']), 'blue' => intval($baseColor['blue'] + $topColor['blue'] > 255.0 ? 255.0 : $baseColor['blue']), 'alpha' => intval($topColor['alpha'])); break; case 'linearDodge': $destColor = array('red' => intval($baseColor['red'] + $topColor['red'] > 255.0 ? 255.0 : $baseColor['red'] + $topColor['red']), 'green' => intval($baseColor['green'] + $topColor['green'] > 255.0 ? 255.0 : $baseColor['green'] + $topColor['red']), 'blue' => intval($baseColor['blue'] + $topColor['blue'] > 255.0 ? 255.0 : $baseColor['blue'] + $topColor['blue']), 'alpha' => intval($topColor['alpha'])); break; case 'overlay': $destColor = array('red' => intval($baseColor['red'] > 127.5 ? $topColor['red'] * ((255.0 - $baseColor['red']) / 127.5) + ($baseColor['red'] - (255.0 - $baseColor['red'])) : $topColor['red'] * ($baseColor['red'] / 127.5)), 'green' => intval($baseColor['green'] > 127.5 ? $topColor['green'] * ((255.0 - $baseColor['green']) / 127.5) + ($baseColor['green'] - (255.0 - $baseColor['green'])) : $topColor['green'] * ($baseColor['green'] / 127.5)), 'blue' => intval($baseColor['blue'] > 127.5 ? $topColor['blue'] * ((255.0 - $baseColor['blue']) / 127.5) + ($baseColor['blue'] - (255.0 - $baseColor['blue'])) : $topColor['blue'] * ($baseColor['blue'] / 127.5)), 'alpha' => intval($topColor['alpha'])); break; case 'softLight': $destColor = array('red' => intval(round($topColor['red'] / 255.0 < 0.5 ? (2 * ($baseColor['red'] / 255.0) * ($topColor['red'] / 255.0) + $baseColor['red'] / 255.0 * ($baseColor['red'] / 255.0) * (1 - 2 * ($topColor['red'] / 255.0))) * 255.0 : (2 * ($baseColor['red'] / 255.0) * (1 - $topColor['red'] / 255.0) + sqrt($baseColor['red'] / 255.0) * (2 * ($topColor['red'] / 255.0) - 1)) * 255.0)), 'green' => intval(round($topColor['green'] / 255.0 < 0.5 ? (2 * ($baseColor['green'] / 255.0) * ($topColor['green'] / 255.0) + $baseColor['green'] / 255.0 * ($baseColor['green'] / 255.0) * (1 - 2 * ($topColor['green'] / 255.0))) * 255.0 : (2 * ($baseColor['green'] / 255.0) * (1 - $topColor['green'] / 255.0) + sqrt($baseColor['green'] / 255.0) * (2 * ($topColor['green'] / 255.0) - 1)) * 255.0)), 'blue' => intval(round($topColor['blue'] / 255.0 < 0.5 ? (2 * ($baseColor['blue'] / 255.0) * ($topColor['blue'] / 255.0) + $baseColor['blue'] / 255.0 * ($baseColor['blue'] / 255.0) * (1 - 2 * ($topColor['blue'] / 255.0))) * 255.0 : (2 * ($baseColor['blue'] / 255.0) * (1 - $topColor['blue'] / 255.0) + sqrt($baseColor['blue'] / 255.0) * (2 * ($topColor['blue'] / 255.0) - 1)) * 255.0)), 'alpha' => intval($topColor['alpha'])); break; case 'hardLight': $destColor = array('red' => intval(round($topColor['red'] > 127.5 ? $baseColor['red'] * ((255 - $topColor['red']) / 127.5) + ($topColor['red'] - (255 - $topColor['red'])) : $baseColor['red'] * ($topColor['red'] / 127.5))), 'green' => intval(round($topColor['green'] > 127.5 ? $baseColor['green'] * ((255 - $topColor['green']) / 127.5) + ($topColor['green'] - (255 - $topColor['green'])) : $baseColor['green'] * ($topColor['green'] / 127.5))), 'blue' => intval(round($topColor['blue'] > 127.5 ? $baseColor['blue'] * ((255 - $topColor['blue']) / 127.5) + ($topColor['blue'] - (255 - $topColor['blue'])) : $baseColor['blue'] * ($topColor['blue'] / 127.5))), 'alpha' => intval($topColor['alpha'])); break; case 'vividLight': // hotfix for division by zero bug foreach ($baseColor as $baseKey => $base) { if ($baseKey != 'alpha') { if ($base <= 0) { $baseColor[$baseKey] = 1.0; } elseif ($base >= 255.0) { $baseColor[$baseKey] = 254.0; } } } foreach ($topColor as $topKey => $top) { if ($topKey != 'alpha') { if ($top <= 0) { $topColor[$topKey] = 1.0; } elseif ($top >= 255.0) { $topColor[$topKey] = 254.0; } } } $destColor = array('red' => intval(round($topColor['red'] / 255 <= 0.5 ? (1 - (1 - $baseColor['red'] / 255.0) / (2 * ($topColor['red'] / 255.0))) * 255.0 : $baseColor['red'] / 255.0 / (2 * (1 - $topColor['red'] / 255.0)) * 255.0)), 'green' => intval(round($topColor['green'] / 255 <= 0.5 ? (1 - (1 - $baseColor['green'] / 255.0) / (2 * ($topColor['green'] / 255.0))) * 255.0 : $baseColor['green'] / 255.0 / (2 * (1 - $topColor['green'] / 255.0)) * 255.0)), 'blue' => intval(round($topColor['blue'] / 255 <= 0.5 ? (1 - (1 - $baseColor['blue'] / 255.0) / (2 * ($topColor['blue'] / 255.0))) * 255.0 : $baseColor['blue'] / 255.0 / (2 * (1 - $topColor['blue'] / 255.0)) * 255.0)), 'alpha' => intval($topColor['alpha'])); break; case 'linearLight': $destColor = array('red' => intval(round(($baseColor['red'] / 255 + 2 * ($topColor['red'] / 255) - 1) * 255.0)), 'green' => intval(round(($baseColor['green'] / 255 + 2 * ($topColor['green'] / 255) - 1) * 255.0)), 'blue' => intval(round(($baseColor['blue'] / 255 + 2 * ($topColor['blue'] / 255) - 1) * 255.0)), 'alpha' => intval($topColor['alpha'])); break; case 'pinLight': $destColor = array('red' => intval(round($baseColor['red'] / 255.0 < 2 * ($topColor['red'] / 255.0) - 1 ? (2 * ($topColor['red'] / 255.0) - 1) * 255.0 : 2 * ($topColor['red'] / 255.0) - 1 < $baseColor['red'] / 255.0 && $baseColor['red'] / 255.0 < 2 * ($topColor['red'] / 255.0) ? $baseColor['red'] : 2 * ($topColor['red'] / 255.0) * 255.0)), 'green' => intval(round($baseColor['green'] / 255.0 < 2 * ($topColor['green'] / 255.0) - 1 ? (2 * ($topColor['green'] / 255.0) - 1) * 255.0 : 2 * ($topColor['green'] / 255.0) - 1 < $baseColor['green'] / 255.0 && $baseColor['green'] / 255.0 < 2 * ($topColor['green'] / 255.0) ? $baseColor['green'] : 2 * ($topColor['green'] / 255.0) * 255.0)), 'blue' => intval(round($baseColor['blue'] / 255.0 < 2 * ($topColor['blue'] / 255.0) - 1 ? (2 * ($topColor['blue'] / 255.0) - 1) * 255.0 : 2 * ($topColor['blue'] / 255.0) - 1 < $baseColor['blue'] / 255.0 && $baseColor['blue'] / 255.0 < 2 * ($topColor['blue'] / 255.0) ? $baseColor['blue'] : 2 * ($topColor['blue'] / 255.0) * 255.0)), 'alpha' => intval($topColor['alpha'])); break; case 'hardMix': $destColor = array('red' => intval($baseColor['red'] + $topColor['red'] >= 255.0 ? 255.0 : 0.0), 'green' => intval($baseColor['green'] + $topColor['green'] >= 255.0 ? 255.0 : 0.0), 'blue' => intval($baseColor['blue'] + $topColor['blue'] >= 255.0 ? 255.0 : 0.0), 'alpha' => intval($topColor['alpha'])); break; case 'difference': $destColor = array('red' => intval($topColor['red'] - $baseColor['red'] < 0 ? ($topColor['red'] - $baseColor['red']) * -1 : $topColor['red'] - $baseColor['red']), 'green' => intval($topColor['green'] - $baseColor['green'] < 0 ? ($topColor['green'] - $baseColor['green']) * -1 : $topColor['green'] - $baseColor['green']), 'blue' => intval($topColor['blue'] - $baseColor['blue'] < 0 ? ($topColor['blue'] - $baseColor['blue']) * -1 : $topColor['blue'] - $baseColor['blue']), 'alpha' => intval($topColor['alpha'])); break; case 'exclusion': $destColor = array('red' => intval(round((0.5 - 2.0 * ($baseColor['red'] / 255.0 - 0.5) * ($topColor['red'] / 255.0 - 0.5)) * 255.0)), 'green' => intval(round((0.5 - 2.0 * ($baseColor['green'] / 255.0 - 0.5) * ($topColor['green'] / 255.0 - 0.5)) * 255.0)), 'blue' => intval(round((0.5 - 2.0 * ($baseColor['blue'] / 255.0 - 0.5) * ($topColor['blue'] / 255.0 - 0.5)) * 255.0)), 'alpha' => intval($topColor['alpha'])); break; case 'subtract': $destColor = array('red' => intval($baseColor['red'] - $topColor['red'] < 0.0 ? 0.0 : $baseColor['red'] - $topColor['red']), 'green' => intval($baseColor['green'] - $topColor['green'] < 0.0 ? 0.0 : $baseColor['green'] - $topColor['green']), 'blue' => intval($baseColor['blue'] - $topColor['blue'] < 0.0 ? 0.0 : $baseColor['blue'] - $topColor['blue']), 'alpha' => intval($topColor['alpha'])); break; case 'divide': // hotfix for division by zero bug foreach ($baseColor as $baseKey => $base) { if ($baseKey != 'alpha') { if ($base <= 0) { $baseColor[$baseKey] = 1; } } } foreach ($topColor as $topKey => $top) { if ($topKey != 'alpha') { if ($top <= 0) { $topColor[$topKey] = 1; } } } $destColor = array('red' => intval($baseColor['red'] / $topColor['red'] * 255.0 > 255.0 ? 255.0 : $baseColor['red'] / $topColor['red'] * 255.0), 'green' => intval($baseColor['green'] / $topColor['green'] * 255.0 > 255.0 ? 255.0 : $baseColor['green'] / $topColor['green'] * 255.0), 'blue' => intval($baseColor['blue'] / $topColor['blue'] * 255.0 > 255.0 ? 255.0 : $baseColor['blue'] / $topColor['blue'] * 255.0), 'alpha' => intval($topColor['alpha'])); break; case 'hue': $baseColorHsl = $this->rgbToHsl($baseColor['red'], $baseColor['green'], $baseColor['green']); $topColorHsl = $this->rgbToHsl($topColor['red'], $topColor['green'], $topColor['blue']); $destColorRgb = $this->hslToRgb($topColorHsl['hue'], $baseColorHsl['saturation'], $baseColorHsl['lightness']); $destColor = array('red' => $destColorRgb['red'], 'green' => $destColorRgb['green'], 'blue' => $destColorRgb['blue'], 'alpha' => $topColor['alpha']); break; case 'saturation': $baseColorHsl = $this->rgbToHsl($baseColor['red'], $baseColor['green'], $baseColor['green']); $topColorHsl = $this->rgbToHsl($topColor['red'], $topColor['green'], $topColor['blue']); $destColorRgb = $this->hslToRgb($baseColorHsl['hue'], $topColorHsl['saturation'], $baseColorHsl['lightness']); $destColor = array('red' => $destColorRgb['red'], 'green' => $destColorRgb['green'], 'blue' => $destColorRgb['blue'], 'alpha' => $topColor['alpha']); break; case 'color': $baseColorHsl = $this->rgbToHsl($baseColor['red'], $baseColor['green'], $baseColor['green']); $topColorHsl = $this->rgbToHsl($topColor['red'], $topColor['green'], $topColor['blue']); $destColorRgb = $this->hslToRgb($topColorHsl['hue'], $topColorHsl['saturation'], $baseColorHsl['lightness']); $destColor = array('red' => $destColorRgb['red'], 'green' => $destColorRgb['green'], 'blue' => $destColorRgb['blue'], 'alpha' => $topColor['alpha']); break; case 'luminosity': $baseColorHsl = $this->rgbToHsl($baseColor['red'], $baseColor['green'], $baseColor['green']); $topColorHsl = $this->rgbToHsl($topColor['red'], $topColor['green'], $topColor['blue']); $destColorRgb = $this->hslToRgb($baseColorHsl['hue'], $baseColorHsl['saturation'], $topColorHsl['lightness']); $destColor = array('red' => $destColorRgb['red'], 'green' => $destColorRgb['green'], 'blue' => $destColorRgb['blue'], 'alpha' => $topColor['alpha']); break; } $colorIndex = imagecolorallocatealpha($baseImage, $destColor['red'], $destColor['green'], $destColor['blue'], $destColor['alpha']); if ($colorIndex === false) { $colorIndex = imagecolorclosestalpha($baseImage, $destColor['red'], $destColor['green'], $destColor['blue'], $destColor['alpha']); } imagesetpixel($baseImage, $x + $destX, $y + $destY, $colorIndex); } } return $baseImage; }
print_r(imagecolorsforindex($im, $c)); // with alpha $im = imagecreatetruecolor(5, 5); $c = imagecolorclosestalpha($im, 255, 0, 255, 100); printf("%X\n", $c); imagedestroy($im); $im = imagecreate(5, 5); $c = imagecolorclosestalpha($im, 255, 0, 255, 100); print_r(imagecolorsforindex($im, $c)); imagedestroy($im); $im = imagecreate(5, 5); imagecolorallocatealpha($im, 255, 0, 255, 1); $c = imagecolorclosestalpha($im, 255, 0, 255, 1); print_r(imagecolorsforindex($im, $c)); imagedestroy($im); $im = imagecreate(5, 5); for ($i = 0; $i < 255; $i++) { imagecolorresolvealpha($im, $i, 0, 0, 1); } $c = imagecolorclosestalpha($im, 255, 0, 0, 1); print_r(imagecolorsforindex($im, $c)); $im = imagecreate(5, 5); for ($i = 0; $i < 256; $i++) { if ($i == 246) { imagecolorallocatealpha($im, $i, 10, 10, 1); } else { imagecolorallocatealpha($im, $i, 0, 0, 100); } } $c = imagecolorclosestalpha($im, 255, 10, 10, 1); print_r(imagecolorsforindex($im, $c));
/** * 添加文字 * 参数格式 * array("txt" => $txt, "opacity" => $opacity, "gravity" => $gravity, "font" => array("name" => $font['name'], "size" => $font["size"], "weight" => $font["weight"], "color" => $font["color"])) * @param array $args */ private function _annotate($args) { $font = $args['font']; $rgb = $this->toRGB($font['color']); $color = imagecolorclosestalpha($this->_image, $rgb['r'], $rgb['g'], $rgb['b'], (1 - $args['opacity']) * 100); //设置位置 $fontSize = imagettfbbox($font['size'], 0, $font['name'], $args['txt']); $textWidth = $fontSize[4]; //取出宽 $textHeight = abs($fontSize[7]); //取出高 switch ($args['gravity']) { case SAE_NorthWest: $x = 0; $y = $textHeight; break; case SAE_North: $x = ($this->_width - $textWidth) / 2; $y = $textHeight; break; case SAE_NorthEast: $x = $this->_width - $textWidth; $y = $textHeight; break; case SAE_West: $x = 0; $y = ($this->_height - $textHeight) / 2; break; case SAE_East: $x = $this->_width - $textWidth; $y = ($this->_height - $textHeight) / 2; break; case SAE_SouthWest: $x = 0; $y = $this->_height - $textHeight; break; case SAE_South: $x = ($this->_width - $textWidth) / 2; $y = $this->_height - $textHeight; break; case SAE_SouthEast: $x = $this->_width - $textWidth; $y = $this->_height - $textHeight; break; case SAE_Static: default: $x = ($this->_width - $textWidth) / 2; $y = ($this->_height - $textHeight) / 2; break; } imagettftext($this->_image, $font['size'], 0, $x, $y, $color, $font['name'], $args['txt']); }
$str || ($str = "ÇëÊäÈë×ÖÌå!"); $cnvert = new Chinese("GB2312", "UTF8", $str, dirname(__FILE__) . "/../inc/" . "gbkcode/"); $str = $cnvert->ConvertIT(); $size || ($size = 30); $img_array = getimagesize($image); $font_array = ImageTTFBBox($size, 0, $fnt, $str); $font_wight = intval($font_array[2] - $font_array[0]); $font_height = intval($font_array[3] - $font_array[5]); $x || ($x = intval(($img_array[0] - $font_wight) / 2)); $y || ($y = intval($img_array[1] / 2 + $font_height / 2)); $im = imagecreatefromjpeg($image); if ($c == 'blue') { $color = imagecolorclosestalpha($im, 00, 00, 255, 20); $color2 = imagecolorclosestalpha($im, 00, 00, 00, 98); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } elseif ($c == 'white') { $color = imagecolorclosestalpha($im, 255, 255, 255, 20); $color2 = imagecolorclosestalpha($im, 00, 00, 00, 99); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } elseif ($c == 'red') { $color = imagecolorclosestalpha($im, 255, 00, 00, 20); $color2 = imagecolorclosestalpha($im, 255, 255, 255, 20); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } else { $color = imagecolorclosestalpha($im, 00, 00, 00, 20); $color2 = imagecolorclosestalpha($im, 255, 255, 255, 40); imagettftext($im, $size, 0, $x + 2, $y + 2, $color2, $fnt, $str); } imagettftext($im, $size, 0, $x, $y, $color, $fnt, $str); ImageJPEG($im); ImageDestroy($im);