/** * Copies a rectangular portion of the source image to another rectangle in the destination image * * This function calls imagecopyresampled() if it is available and GD version is 2 at least. * Otherwise it reimplements the same behaviour. See the PHP manual page for more info. * * @link http://php.net/manual/en/function.imagecopyresampled.php * @param resource $dst_img the destination GD image resource * @param resource $src_img the source GD image resource * @param int $dst_x vthe X coordinate of the upper left corner in the destination image * @param int $dst_y the Y coordinate of the upper left corner in the destination image * @param int $src_x the X coordinate of the upper left corner in the source image * @param int $src_y the Y coordinate of the upper left corner in the source image * @param int $dst_w the width of the destination rectangle * @param int $dst_h the height of the destination rectangle * @param int $src_w the width of the source rectangle * @param int $src_h the height of the source rectangle * @return bool tru on success, false otherwise */ 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); } } }
public function generate() { $findColor = Image_Image::hexColorToArrayColor($this->find); $replaceColor = Image_Image::hexColorToArrayColor($this->replace); $index = imagecolorclosest($this->_owner->image, $findColor['red'], $findColor['green'], $findColor['blue']); //find imagecolorset($this->_owner->image, $index, $replaceColor['red'], $replaceColor['green'], $replaceColor['blue']); //replace unset($index); return true; }
public static function compare($max_colors, $filename) { $tally = array(); // size image to something managable (256 x 256) $image_data = getimagesize($filename); // if small image then use its current size if ($image_data[0] < self::$resize_dim && $image_data[1] < self::$resize_dim) { $image = self::createImage($filename, $image_data[2]); $width = $image_data[0]; $height = $image_data[1]; } else { $res = self::createResizedImage($filename, $image_data[0], $image_data[1], $image_data[2]); if ($res == false) { print "[failed on resize]"; return false; } else { $image = $res[0]; $width = $res[1]; $height = $res[2]; } } // create the comparison palette self::createComparisonPalette(); // loop through x axis for ($x = 0; $x < $width; $x++) { // loop through y axis for ($y = 0; $y < $height; $y++) { // compare to find colest match and tally list($red, $green, $blue) = self::getRGBFromPixel($image, $x, $y); $index = imagecolorclosest(self::$comp_palette, $red, $green, $blue); @($tally[$index] = @$tally[$index] + 1); } } // sort the tally results arsort($tally); $ret_array = array(); $i = 0; $threshold = $width * $height * (self::$threshold_filter / 100); // build the return array of the top results foreach ($tally as $index => $count) { // make sure the count is high enough to be considered significant if ($count >= $threshold) { $ret_array[self::$swatch_index[$index]] = $count; $i++; } else { break; } if ($i >= $max_colors) { break; } } return $ret_array; }
public function toPngColor($img) { $color = imagecolorexact($img, $this->red, $this->green, $this->blue); if ($color == -1) { if (imagecolorstotal($img) >= 255) { $color = imagecolorclosest($img, $this->red, $this->green, $this->blue); } else { $color = imagecolorallocate($img, $this->red, $this->green, $this->blue); } } return $color; }
function _get_image_color($im, $r, $g, $b) { $c = imagecolorexact($im, $r, $g, $b); if ($c != -1) { return $c; } $c = imagecolorallocate($im, $r, $g, $b); if ($c != -1) { return $c; } return imagecolorclosest($im, $r, $g, $b); }
/** * Applies the filter to the resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { $dest = $aResource->getResource(); if (imageistruecolor($dest)) { imagetruecolortopalette($dest, false, 256); } foreach ($this->search as $search) { $searchRgb = new Color($search); $index = imagecolorclosest($aResource->getResource(), $searchRgb->getRed(), $searchRgb->getGreen(), $searchRgb->getBlue()); // get White COlor imagecolorset($aResource->getResource(), $index, $this->replace->getRed(), $this->replace->getGreen(), $this->replace->getBlue()); // SET NEW COLOR } $aResource->setResource($dest); }
/** * Insert a color to the palet or return the color if the color exist * * @param image $pic the picture where we work on it * @param int $c1 red part of the color * @param int $c2 green part of the color * @param int $c3 blue part of the color * @return color the color that we want */ public static function createcolor($pic, $c1, $c2, $c3) { //get color from palette $color = imagecolorexact($pic, $c1, $c2, $c3); if ($color == -1) { //color does not exist... //test if we have used up palette if (imagecolorstotal($pic) >= 255) { //palette used up; pick closest assigned color $color = imagecolorclosest($pic, $c1, $c2, $c3); } else { //palette NOT used up; assign new color $color = imagecolorallocate($pic, $c1, $c2, $c3); } } return $color; }
function typo3_distortString($img, $text, $tcolor, $bcolor, $angle, $diffx, $diffy, $xpos, $ypos, $letterSpacing, $bold, $fontSize, $fontFile, $useTTF = 0) { // When no TTF get's used. $charx = 20; $chary = 20; $osx = 5; $osy = 0; $fg = imagecolorallocate($img, $tcolor[0], $tcolor[1], $tcolor[2]); $bg = imagecolorallocate($img, $bcolor[0], $bcolor[1], $bcolor[2]); for ($x = 0; $x < strlen($text); $x++) { $c = substr($text, $x, 1); $da = rand(0, $angle * 2) - $angle; $dx = intval(rand(0, $diffx * 2) - $diffx); $dy = intval(rand(0, $diffy * 2) - $diffy); if ($useTTF) { $ret = imagettftext($img, $fontSize, $da, $xpos + $dx, $ypos + $dy + $fontSize, $fg, $fontFile, $c); if ($bold) { $ret = imagettftext($img, $fontSize, $da, $xpos + $dx + 1, $ypos + $dy + $fontSize, $fg, $fontFile, $c); $ret = imagettftext($img, $fontSize, $da, $xpos + $dx + 1, $ypos + $dy + $fontSize + 1, $fg, $fontFile, $c); $ret = imagettftext($img, $fontSize, $da, $xpos + $dx, $ypos + $dy + $fontSize + 1, $fg, $fontFile, $c); } $xpos += $ret[2] - $ret[0]; } else { $tmpi = imagecreate($charx, $chary); $back = imagecolorallocate($tmpi, $bcolor[0], $bcolor[1], $bcolor[2]); $fcol = imagecolorallocate($tmpi, $tcolor[0], $tcolor[1], $tcolor[2]); imagefill($tmpi, 0, 0, $back); imagestring($tmpi, 5, $osx, $osy, $c, $fcol); $rot = imagerotate($tmpi, $da, $back); $rback = imagecolorclosest($rot, $bcolor[0], $bcolor[1], $bcolor[2]); imagecolortransparent($rot, $rback); imagecopymerge($img, $rot, $xpos + $dx, $ypos + $dy, 0, 0, 20, 20, 100); } $xpos += $letterSpacing; } }
function _a_set_pixel($im, $x, $y, $filled, $fgcolors) { $rgb = imagecolorat($im, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $red = round($r + ($fgcolors['red'] - $r) * $filled); $green = round($g + ($fgcolors['green'] - $g) * $filled); $blue = round($b + ($fgcolors['blue'] - $b) * $filled); imagesetpixel($im, $x, $y, imagecolorclosest($im, $red, $green, $blue)); }
function makeThumbWatermark($width = 128, $height = 128) { $this->fileCheck(); $image_info = $this->getInfo($this->src_image_name); if (!$image_info) { return false; } $src_image_type = $image_info["type"]; $img = $this->createImage($src_image_type, $this->src_image_name); if (!$img) { return false; } $width = $width == 0 ? $image_info["width"] : $width; $height = $height == 0 ? $image_info["height"] : $height; $width = $width > $image_info["width"] ? $image_info["width"] : $width; $height = $height > $image_info["height"] ? $image_info["height"] : $height; $srcW = $image_info["width"]; $srcH = $image_info["height"]; if ($srcH * $width > $srcW * $height) { $width = round($srcW * $height / $srcH); } else { $height = round($srcH * $width / $srcW); } //* $src_image = @imagecreatetruecolor($width, $height); $white = @imagecolorallocate($src_image, 0xff, 0xff, 0xff); @imagecolortransparent($src_image, $white); @imagefilltoborder($src_image, 0, 0, $white, $white); if ($src_image) { ImageCopyResampled($src_image, $img, 0, 0, 0, 0, $width, $height, $image_info["width"], $image_info["height"]); } else { $src_image = imagecreate($width, $height); ImageCopyResized($src_image, $img, 0, 0, 0, 0, $width, $height, $image_info["width"], $image_info["height"]); } $src_image_w = ImageSX($src_image); $src_image_h = ImageSY($src_image); if ($this->wm_image_name) { $wm_image_info = $this->getInfo($this->wm_image_name); if (!$wm_image_info) { return false; } $wm_image_type = $wm_image_info["type"]; $wm_image = $this->createImage($wm_image_type, $this->wm_image_name); $wm_image_w = ImageSX($wm_image); $wm_image_h = ImageSY($wm_image); $temp_wm_image = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos, $wm_image); if ($this->emboss && function_exists("imagefilter")) { imagefilter($wm_image, IMG_FILTER_EMBOSS); $bgcolor = imagecolorclosest($wm_image, 0x7f, 0x7f, 0x7f); imagecolortransparent($wm_image, $bgcolor); } if (function_exists("ImageAlphaBlending") && IMAGETYPE_PNG == $wm_image_info['type']) { ImageAlphaBlending($src_image, true); } $wm_image_x = $temp_wm_image["dest_x"]; $wm_image_y = $temp_wm_image["dest_y"]; if (IMAGETYPE_PNG == $wm_image_info['type']) { imageCopy($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h); } else { imageCopyMerge($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h, $this->wm_image_transition); } } if ($this->wm_text) { $this->wm_text = $this->wm_text; $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos); $wm_text_x = $temp_wm_text["dest_x"]; $wm_text_y = $temp_wm_text["dest_y"]; if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)) { $red = hexdec($color[1]); $green = hexdec($color[2]); $blue = hexdec($color[3]); $wm_text_color = imagecolorallocate($src_image, $red, $green, $blue); } else { $wm_text_color = imagecolorallocate($src_image, 255, 255, 255); } imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color, $this->wm_text_font, $this->wm_text); } if ($this->save_file) { switch ($src_image_type) { case 1: if ($this->gif_enable) { $src_img = ImageGIF($src_image, $this->save_file); } else { $src_img = ImagePNG($src_image, $this->save_file); } break; case 2: $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; case 3: $src_img = ImagePNG($src_image, $this->save_file); break; default: $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; } } else { switch ($src_image_type) { case 1: if ($this->gif_enable) { header("Content-type: image/gif"); $src_img = ImageGIF($src_image); } else { header("Content-type: image/png"); $src_img = ImagePNG($src_image); } break; case 2: header("Content-type: image/jpeg"); $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; case 3: header("Content-type: image/png"); $src_img = ImagePNG($src_image); break; case 6: header("Content-type: image/bmp"); $src_img = imagebmp($src_image); break; default: header("Content-type: image/jpeg"); $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; } } imagedestroy($src_image); imagedestroy($img); return true; }
function imagecreatefrombmp($filename) { global $gd2; $fp = fopen($filename, 'rb'); $errors = error_reporting(0); $header = unpack('vtype/Vsize/Vreserved/Voffset', fread($fp, 14)); $info = unpack('Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vcolorimportant', fread($fp, 40)); if ($header['type'] != 0x4d42) { false; } if ($gd2) { $dst_img = imagecreatetruecolor($info['width'], $info['height']); } else { $dst_img = imagecreate($info['width'], $info['height']); } $palette_size = $header['offset'] - 54; $info['ncolor'] = $palette_size / 4; $palette = array(); $palettedata = fread($fp, $palette_size); $n = 0; for ($j = 0; $j < $palette_size; $j++) { $b = ord($palettedata[$j++]); $g = ord($palettedata[$j++]); $r = ord($palettedata[$j++]); $palette[$n++] = imagecolorallocate($dst_img, $r, $g, $b); } $scan_line_size = $info['bits'] * $info['width'] + 7 >> 3; $scan_line_align = $scan_line_size & 3 ? 4 - ($scan_line_size & 3) : 0; for ($y = 0, $l = $info['height'] - 1; $y < $info['height']; $y++, $l--) { fseek($fp, $header['offset'] + ($scan_line_size + $scan_line_align) * $l); $scan_line = fread($fp, $scan_line_size); if (strlen($scan_line) < $scan_line_size) { continue; } if ($info['bits'] == 32) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $b = ord($scan_line[$j++]); $g = ord($scan_line[$j++]); $r = ord($scan_line[$j++]); $j++; $color = imagecolorexact($dst_img, $r, $g, $b); if ($color == -1) { $color = imagecolorallocate($dst_img, $r, $g, $b); // Gah! Out of colors? Stupid GD 1... try anyhow. if ($color == -1) { $color = imagecolorclosest($dst_img, $r, $g, $b); } } imagesetpixel($dst_img, $x, $y, $color); } } elseif ($info['bits'] == 24) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $b = ord($scan_line[$j++]); $g = ord($scan_line[$j++]); $r = ord($scan_line[$j++]); $color = imagecolorexact($dst_img, $r, $g, $b); if ($color == -1) { $color = imagecolorallocate($dst_img, $r, $g, $b); // Gah! Out of colors? Stupid GD 1... try anyhow. if ($color == -1) { $color = imagecolorclosest($dst_img, $r, $g, $b); } } imagesetpixel($dst_img, $x, $y, $color); } } elseif ($info['bits'] == 16) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $b1 = ord($scan_line[$j++]); $b2 = ord($scan_line[$j++]); $word = $b2 * 256 + $b1; $b = ($word & 31) * 255 / 31; $g = ($word >> 5 & 31) * 255 / 31; $r = ($word >> 10 & 31) * 255 / 31; // Scale the image colors up properly. $color = imagecolorexact($dst_img, $r, $g, $b); if ($color == -1) { $color = imagecolorallocate($dst_img, $r, $g, $b); // Gah! Out of colors? Stupid GD 1... try anyhow. if ($color == -1) { $color = imagecolorclosest($dst_img, $r, $g, $b); } } imagesetpixel($dst_img, $x, $y, $color); } } elseif ($info['bits'] == 8) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { imagesetpixel($dst_img, $x, $y, $palette[ord($scan_line[$j++])]); } } elseif ($info['bits'] == 4) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $byte = ord($scan_line[$j++]); imagesetpixel($dst_img, $x, $y, $palette[(int) ($byte / 16)]); if (++$x < $info['width']) { imagesetpixel($dst_img, $x, $y, $palette[$byte & 15]); } } } else { // Sorry, I'm just not going to do monochrome :P. } } fclose($fp); error_reporting($errors); return $dst_img; }
function imgFolder() { $a = array(18, 24, 38, 24, 43, 29, 76, 29, 76, 70, 18, 70); if (function_exists('imagecreatetruecolor')) { $img = imagecreatetruecolor(94, 94); $black = imagecolorclosest($img, 0, 0, 0); $yellow = imagecolorclosest($img, 250, 240, 0); $white = imagecolorclosest($img, 255, 255, 255); } else { $img = imagecreate(94, 94); $black = imagecolorallocate($img, 0, 0, 0); $yellow = imagecolorallocate($img, 250, 240, 0); $white = imagecolorallocate($img, 255, 255, 255); } imagefilledrectangle($img, 0, 0, 94, 94, $white); if (function_exists('imagesetthickness')) { imagesetthickness($img, 1); } imagefilledpolygon($img, $a, 6, $yellow); imagepolygon($img, $a, 6, $black); imageline($img, 40, 32, 76, 32, $black); imageline($img, 18, 34, 34, 34, $black); imageline($img, 34, 34, 40, 32, $black); return $img; }
$height = $height > MAX_SCENE_HEIGHT ? MAX_SCENE_HEIGHT : $height; $height = $height < MIN_SCENE_HEIGHT ? MIN_SCENE_HEIGHT : $height; $feature_z = MAX_DEPTH - $height * (MAX_DEPTH - MIN_DEPTH) / 256; $sep = (int) ((double) (EYE_SEP * $feature_z) / ($feature_z + OBS_DIST)); $left_px = (int) $x - $sep / 2; $right_px = (int) $x + $sep / 2; if ($left_px >= 0 && $right_px < $img_width) { if (!isset($buffer[$left_px][$y])) { $buffer[$left_px][$y] = $colors[rand(1, 255)]; } $buffer[$right_px][$y] = $buffer[$left_px][$y]; } } for ($x = 0; $x < $img_width; $x++) { if (!isset($buffer[$x][$y])) { $buffer[$x][$y] = $colors[rand(1, 255)]; } } } /* Output PNG File */ for ($y = 0; $y < $img_height; $y++) { for ($x = 0; $x < $img_width; $x++) { imagesetpixel($stereo_img, $x, $y, $buffer[$x][$y]); } } header("Content-type: image/png"); $black = imagecolorclosest($stereo_img, 0, 0, 0); $white = imagecolorclosest($stereo_img, 0xff, 0xff, 0xff); imagefilledrectangle($stereo_img, 0, $img_height - 20, $img_width, $img_height, $black); imagestring($stereo_img, 1, 5, $img_height - 10, "Made at http://www.coggeshall.org/stereogram.php", $white); imagepng($stereo_img);
public function ellipse($bg_colour = "FFFFFF") { $this->bgc = $bg_colour; $this->br = $this->hex2rgb(substr($this->bgc, 0, 2)); $this->bg = $this->hex2rgb(substr($this->bgc, 2, 2)); $this->bb = $this->hex2rgb(substr($this->bgc, 4, 2)); $this->dot = imagecreate(6, 6); $this->dot_base = imagecolorallocate($this->dot, $this->br, $this->bg, $this->bb); $this->zenitha = imagecolorclosest($this->t, $this->br, $this->bg, $this->bb); for ($this->rad = 0; $this->rad < 6.3; $this->rad += 0.005) { $this->xpos = floor($this->q + sin($this->rad) * $this->q) / 2; $this->ypos = floor($this->r + cos($this->rad) * $this->r) / 2; $this->xto = 0; if ($this->xpos >= $this->q / 2) { $this->xto = $this->q; } imagecopymerge($this->t, $this->dot, $this->xpos - 3, $this->ypos - 3, 0, 0, 6, 6, 30); imagecopymerge($this->t, $this->dot, $this->xpos - 2, $this->ypos - 2, 0, 0, 4, 4, 30); imagecopymerge($this->t, $this->dot, $this->xpos - 1, $this->ypos - 1, 0, 0, 2, 2, 30); imageline($this->t, $this->xpos, $this->ypos, $this->xto, $this->ypos, $this->zenitha); } imagedestroy($this->dot); $this->efecto[] = "ellipse"; }
/** * Returns closest color index that matches the given RGB value. Uses * PHP's imagecolorclosest() * * @param mixed $R Red or RGBA array */ function getClosestColor($R, $G = null, $B = null) { if (is_array($R)) { return imagecolorclosest($this->handle, $R['red'], $R['green'], $R['blue']); } else { return imagecolorclosest($this->handle, $R, $G, $B); } }
/** * Watermark - Text Version * * @return bool */ public function text_watermark() { if (!($src_img = $this->image_create_gd())) { return FALSE; } if ($this->wm_use_truetype === TRUE && !file_exists($this->wm_font_path)) { $this->set_error('imglib_missing_font'); return FALSE; } // Fetch source image properties $this->get_image_properties(); // 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 (empty($this->wm_font_size)) { $this->wm_font_size = 17; } if (function_exists('imagettfbbox')) { $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text); $temp = $temp[2] - $temp[0]; $fontwidth = $temp / strlen($this->wm_text); } else { $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; if ($this->wm_use_drop_shadow === FALSE) { $this->wm_shadow_distance = 0; } $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]); $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]); // Set vertical alignment if ($this->wm_vrt_alignment === 'M') { $y_axis += $this->orig_height / 2 + $fontheight / 2; } elseif ($this->wm_vrt_alignment === 'B') { $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - $fontheight / 2; } // Set horizontal alignment if ($this->wm_hor_alignment === 'R') { $x_axis += $this->orig_width - $fontwidth * strlen($this->wm_text) - $this->wm_shadow_distance; } elseif ($this->wm_hor_alignment === 'C') { $x_axis += floor(($this->orig_width - $fontwidth * strlen($this->wm_text)) / 2); } if ($this->wm_use_drop_shadow) { // Offset from text $x_shad = $x_axis + $this->wm_shadow_distance; $y_shad = $y_axis + $this->wm_shadow_distance; /* Set RGB values for shadow * * First character is #, so we don't really need it. * Get the rest of the string and split it into 2-length * hex values: */ $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2); $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2])); // Add the shadow to the source image if ($this->wm_use_truetype) { imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); } else { imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); } } /* Set RGB values for text * * First character is #, so we don't really need it. * Get the rest of the string and split it into 2-length * hex values: */ $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2); $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2])); // Add the text to the source image if ($this->wm_use_truetype) { imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); } else { imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); } // We can preserve transparency for PNG images if ($this->image_type === 3) { imagealphablending($src_img, FALSE); imagesavealpha($src_img, TRUE); } // 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; }
function imagettftexttexture(&$im, &$textureim, $size, $angle, $x, $y, $fontfile, $text) { $width = imagesx($im); // Get the width of the image $height = imagesy($im); // Get the height of the image $buffer = imagecreate($width, $height); // Create the buffer image $tile_w = imagesx($textureim); // Get the width of the texture image $tile_h = imagesy($textureim); // Get the height of the texture image $fits_x = (int) ($im_w / $tile_w); // Find out how many times it fits horizontally $fits_y = (int) ($im_h / $tile_h); // Find out how many times it fits vertically for ($i = 0; $i <= $fits_x; $i++) { // Loop through every time (and another, for extra space) it fits horizontally $x = (int) ($tile_w * $i); // Change the X location based on where in the loop it is for ($i2 = 0; $i2 <= $fits_y; $i2++) { // Loop through every time it fits vertically $y = (int) ($tile_h * $i2); // Change the Y location $copy = imagecopy($im, $textureim, $x, $y, 0, 0, $tile_w, $tile_h); // Copy the image to the X,Y location } } $pink = imagecolorclosest($im, 255, 0, 255); // Create magic pink, a color commonly used for masks $trans = imagecolortransparent($im, $pink); // Make magic pink the transparent color imagettftext($im, $size, $angle, $x, $y, -$pink, $fontfile, $text); // Draw text over magic pink without aliasing imagecopy($buffer, $im, 0, 0, 0, 0, $width, $height); // Copy the main image onto the buffer imagecopy($im, $buffer, 0, 0, 0, 0, $width, $height); // Copy the buffer back onto the main image imagedestroy($buffer); // Destroy the buffer }
/** * @param int red 0-255 * @param int green 0-255 * @param int blue 0-255 * @return int internal color index */ public function getClosestColorIndex($red, $green, $blue) { // TODO issue with gif and limited color rang if (!is_null($this->imageResource)) { return imagecolorclosest($this->imageResource, (int) $red, (int) $green, (int) $blue); } return false; }
/** * It is set only if it doesn't already exist (for forwards compatiblity.) * * - It only supports uncompressed bitmaps. * - It only supports standard windows bitmaps (no os/2 varients) * - Returns an image identifier representing the bitmap image * obtained from the given filename. * * @package Graphics * @param string $filename * @return resource */ function imagecreatefrombmp($filename) { global $gd2; $fp = fopen($filename, 'rb'); $errors = error_reporting(0); // Unpack the general information about the Bitmap Image File, first 14 Bytes $header = unpack('vtype/Vsize/Vreserved/Voffset', fread($fp, 14)); // Upack the DIB header, it stores detailed information about the bitmap image the pixel format, 40 Bytes long $info = unpack('Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vcolorimportant', fread($fp, 40)); // Not a standard bitmap, bail out if ($header['type'] != 0x4d42) { return false; } // Create our image canvas with the given WxH if ($gd2) { $dst_img = imagecreatetruecolor($info['width'], $info['height']); } else { $dst_img = imagecreate($info['width'], $info['height']); } // Color bitCounts 1,4,8 have palette information we use if ($info['bits'] == 1 || $info['bits'] == 4 || $info['bits'] == 8) { $palette_size = $header['offset'] - 54; // Read the palette data $palettedata = fread($fp, $palette_size); // Create the rgb color array $palette = array(); $n = 0; for ($j = 0; $j < $palette_size; $j++) { $b = ord($palettedata[$j++]); $g = ord($palettedata[$j++]); $r = ord($palettedata[$j++]); $palette[$n++] = imagecolorallocate($dst_img, $r, $g, $b); } } $scan_line_size = $info['bits'] * $info['width'] + 7 >> 3; $scan_line_align = $scan_line_size & 3 ? 4 - ($scan_line_size & 3) : 0; for ($y = 0, $l = $info['height'] - 1; $y < $info['height']; $y++, $l--) { fseek($fp, $header['offset'] + ($scan_line_size + $scan_line_align) * $l); $scan_line = fread($fp, $scan_line_size); if (strlen($scan_line) < $scan_line_size) { continue; } // 32 bits per pixel if ($info['bits'] == 32) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $b = ord($scan_line[$j++]); $g = ord($scan_line[$j++]); $r = ord($scan_line[$j++]); $j++; $color = imagecolorexact($dst_img, $r, $g, $b); if ($color == -1) { $color = imagecolorallocate($dst_img, $r, $g, $b); // Gah! Out of colors? Stupid GD 1... try anyhow. if ($color == -1) { $color = imagecolorclosest($dst_img, $r, $g, $b); } } imagesetpixel($dst_img, $x, $y, $color); } } elseif ($info['bits'] == 24) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $b = ord($scan_line[$j++]); $g = ord($scan_line[$j++]); $r = ord($scan_line[$j++]); $color = imagecolorexact($dst_img, $r, $g, $b); if ($color == -1) { $color = imagecolorallocate($dst_img, $r, $g, $b); // Gah! Out of colors? Stupid GD 1... try anyhow. if ($color == -1) { $color = imagecolorclosest($dst_img, $r, $g, $b); } } imagesetpixel($dst_img, $x, $y, $color); } } elseif ($info['bits'] == 16) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $b1 = ord($scan_line[$j++]); $b2 = ord($scan_line[$j++]); $word = $b2 * 256 + $b1; $b = ($word & 31) * 255 / 31; $g = ($word >> 5 & 31) * 255 / 31; $r = ($word >> 10 & 31) * 255 / 31; // Scale the image colors up properly. $color = imagecolorexact($dst_img, $r, $g, $b); if ($color == -1) { $color = imagecolorallocate($dst_img, $r, $g, $b); // Gah! Out of colors? Stupid GD 1... try anyhow. if ($color == -1) { $color = imagecolorclosest($dst_img, $r, $g, $b); } } imagesetpixel($dst_img, $x, $y, $color); } } elseif ($info['bits'] == 8) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { imagesetpixel($dst_img, $x, $y, $palette[ord($scan_line[$j++])]); } } elseif ($info['bits'] == 4) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $byte = ord($scan_line[$j++]); imagesetpixel($dst_img, $x, $y, $palette[(int) ($byte / 16)]); if (++$x < $info['width']) { imagesetpixel($dst_img, $x, $y, $palette[$byte & 15]); } } } elseif ($info['bits'] == 1) { $x = 0; for ($j = 0; $j < $scan_line_size; $x++) { $byte = ord($scan_line[$j++]); imagesetpixel($dst_img, $x, $y, $palette[($byte & 128) != 0]); for ($shift = 1; $shift < 8; $shift++) { if (++$x < $info['width']) { imagesetpixel($dst_img, $x, $y, $palette[($byte << $shift & 128) != 0]); } } } } } fclose($fp); error_reporting($errors); return $dst_img; }
/** * Generates the actual heatmap. */ private function generate_image($data) { require_once 'gd-rg.php'; $time = microtime(1); $this->logg('Started at ' . $time); // Find the maximum value from the given data. $max_data_value = 1; foreach ($data as $row) { if (isset($row[2]) && $max_data_value < $row[2]) { $max_data_value = $row[2]; } } $this->logg('Done sorting data at ' . (microtime(1) - $time)); // Create the heatmap image. $im = imagecreatetruecolor($this->get_config('width'), $this->get_config('height')); $white = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $white); imagealphablending($im, true); imagesavealpha($im, true); // Create a separate spot image for each value to be shown, with different // amounts of black. Having 25 separate shades of colour looks like a decent // number. $spots = array(); for ($i = 0; $i < $this->config['noc']; $i++) { // The gradient lib doesn't like too small values for $alpha_end, so we use // $noc for that, which happens to work well. $alpha_end = $this->map($i, 0, $this->config['noc'] - 1, $this->config['noc'], 255); $temp = new gd_gradient_alpha($this->config['r'], $this->config['r'], 'ellipse', '#000', 0x0, $alpha_end, 0); $spot = $temp->get_image(); imagealphablending($spot, true); imagesavealpha($spot, true); $spots[$i] = $spot; } $this->logg('Created ' . count($spots) . ' spots at ' . (microtime(1) - $time)); // Go through the data, and add appropriate spot images to the heatmap // image. for ($i = 0; $i < count($data); $i++) { $value = isset($data[$i][2]) ? $data[$i][2] : 1; $value = $this->map($value, 1, $max_data_value, 0, $this->config['noc'] - 1); imagecopy($im, $spots[$value], $data[$i][0], $data[$i][1], 0, 0, $this->config['r'], $this->config['r']); } $this->logg('Copied spots to image at ' . (microtime(1) - $time)); imagetruecolortopalette($im, $this->config['dither'], $this->config['noc']); $this->logg('Flattened black at ' . (microtime(1) - $time)); // Get the gradient from an image file $gi = 'gradient-' . $this->config['noc'] . '.png'; if (!file_exists($gi)) { $this->error("Can't find gradient file " . $gi . ". Make one using gradient-source.jpg"); } $gs = imagecreatefrompng($gi); imagetruecolortopalette($gs, TRUE, $this->config['noc']); // Get a list of different gray values in the image, and order them. $grays = array(); for ($i = 0; $i < imagecolorstotal($im); $i++) { $c = imagecolorsforindex($im, $i); $grays[] = str_pad($c['red'] * 65536 + $c['green'] * 256 + $c['blue'], 8, '0', STR_PAD_LEFT) . ':' . $i; } sort($grays); $indexes = array(); foreach ($grays as $gray) { $indexes[] = substr($gray, strpos($gray, ':') + 1); } $this->logg('Created gray indexes at ' . (microtime(1) - $time)); // Replace each shade of gray with the matching rainbow colour. $i = 0; foreach ($indexes as $index) { $fill_index = imagecolorat($gs, $i, 0); $fill_color = imagecolorsforindex($gs, $fill_index); imagecolorset($im, $index, $fill_color['red'], $fill_color['green'], $fill_color['blue']); $i++; } $this->logg('Replaced black with rainbow at ' . (microtime(1) - $time)); // Finally switch from white background to transparent. $closest = imagecolorclosest($im, 255, 255, 255); imagecolortransparent($im, $closest); $this->logg('done at ' . (microtime(1) - $time)); // Debugging text if ($this->config['debug']) { $text_color = imagecolorallocate($im, 0, 0, 0); $y = 5; foreach (explode("\n", $this->debug_log) as $line) { imagestring($im, 3, 250, $y, $line, $text_color); $y = $y + 10; } } $this->im = $im; }
public function make_image() { if (!$this->check_keys()) { $this->error = TEXT_CAPTCHA_KEY_ERROR; return false; } if (!$this->load_fonts()) { $this->error = TEXT_CAPTCHA_NO_FONTS; return false; } if (function_exists('gd_info') && ($text_captcha_gd_info = gd_info())) { if ($text_captcha_gd_info['GD Version'] !== false && $text_captcha_gd_info['FreeType Support'] > 0) { $image = imagecreate($this->image_x, $this->image_y); $this->allocate_colours($image); $this->random_color(224, 255); $color = imagecolorallocate($image, $this->color_red, $this->color_green, $this->color_blue); imagefilledrectangle($image, 0, 0, $this->image_x, $this->image_y, $color); for ($i = 0; $i < $this->noise_level; $i++) { $noise_size = intval(mt_rand((int) ($this->min_char_size / 2.3), (int) ($this->max_char_size / 1.7))); $noise_angle = intval(mt_rand(0, 360)); $noise_x = intval(mt_rand(0, $this->image_x)); $noise_y = intval(mt_rand(0, (int) ($this->image_y - $noise_size / 5))); $this->random_color(160, 224); $noise_color = imagecolorclosest($image, $this->color_red, $this->color_green, $this->color_blue); $noise_text = chr(intval(mt_rand(45, 250))); if (!@imagettftext($image, $noise_size, $noise_angle, $noise_x, $noise_y, $noise_color, $this->random_font(), $noise_text)) { $this->error = TEXT_CAPTCHA_FONT_ERROR; return false; } } for ($i = 0; $i < $this->image_x; $i += (int) ($this->min_char_size / 1.5)) { $this->random_color(160, 224); $line_color = imagecolorclosest($image, $this->color_red, $this->color_green, $this->color_blue); imageline($image, $i, 0, $i, $this->image_y, $line_color); } for ($i = 0; $i < $this->image_y; $i += (int) ($this->min_char_size / 1.8)) { $this->random_color(160, 224); $line_color = imagecolorclosest($image, $this->color_red, $this->color_green, $this->color_blue); imageline($image, 0, $i, $this->image_x, $i, $line_color); } for ($i = 0, $text_x = intval(mt_rand($this->min_char_size, $this->max_char_size)); $i < $this->num_chars; $i++) { $text = mb_strtoupper(mb_substr($this->private_key, $i, 1)); $text_angle = intval(mt_rand($this->max_rotation * -1, $this->max_rotation)); $text_size = intval(mt_rand($this->min_char_size, $this->max_char_size)); $text_y = intval(mt_rand((int) ($text_size * 1.5), (int) ($this->image_y - $text_size / 7))); $this->random_color(0, 127); $color = imagecolorclosest($image, $this->color_red, $this->color_green, $this->color_blue); $this->random_color(0, 127); $shadow = imagecolorclosest($image, $this->color_red, $this->color_green, $this->color_blue); if (!imagettftext($image, $text_size, $text_angle, $text_x + (int) ($text_size / 15), $text_y, $shadow, $this->random_font(), $text)) { $this->error = TEXT_CAPTCHA_FONT_ERROR; return false; } if (!imagettftext($image, $text_size, $text_angle, $text_x, $text_y - (int) ($text_size / 15), $color, $this->get_current_font(), $text)) { $this->error = TEXT_CAPTCHA_FONT_ERROR; return false; } $text_x += (int) ($text_size + $this->min_char_size / 5); } $image_filename = tempnam(sys_get_temp_dir(), 'bhtc'); imagejpeg($image, $image_filename); return $image_filename; } } $this->error = TEXT_CAPTCHA_GD_ERROR; return false; }
function PIE_Contrast($img, $contrast) { if ($contrast != 0) { $width = imagesx($img); $height = imagesy($img); $contrast = (double) (100.0 - $contrast) / 100.0; $contrast *= $contrast; for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { $rgb = imagecolorat($img, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $rf = (double) $r / 255.0; $rf = $rf - 0.5; $rf = $rf * $contrast; $rf = $rf + 0.5; $rf = $rf * 255.0; $bf = (double) $b / 255.0; $bf = $bf - 0.5; $bf = $bf * $contrast; $bf = $bf + 0.5; $bf = $bf * 255.0; $gf = (double) $g / 255.0; $gf = $gf - 0.5; $gf = $gf * $contrast; $gf = $gf + 0.5; $gf = $gf * 255.0; $rf = $rf > 255.0 ? 255.0 : ($rf < 0.0 ? 0.0 : $rf); $gf = $gf > 255.0 ? 255.0 : ($gf < 0.0 ? 0.0 : $gf); $bf = $bf > 255.0 ? 255.0 : ($bf < 0.0 ? 0.0 : $bf); $new_pxl = imagecolorallocate($img, (int) $rf, (int) $gf, (int) $bf); if (!$new_pxl || $new_pxl == -1) { $new_pxl = imagecolorsforindex($img, imagecolorclosest($img, (int) $rf, (int) $gf, (int) $bf)); } @imagesetpixel($img, $x, $y, $new_pxl); } } } }
/** * Create waves in GD images format from WAV (PCM) file * Wave file reading based on a post by "zvoneM" on * http://forums.devshed.com/php-development-5/reading-16-bit-wav-file-318740.html * Completely rewritten the file read loop, kept the header reading intact. * * Waveform drawing from https://github.com/afreiday/php-waveform-png * * Reads width * ACCURACY data points from the file and takes the peak value of accuracy values. * The peak is the highest value if mean is > 127 and the lowest value otherwise. * Data point is the average of ACCURACY points in the data block. */ private function createWaves($wavfilename) { define('ACCURACY', 100); // default optimal accuracy // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Create palette based image // if background == '' we convert it to true color image later then // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $this->waveImg = imagecreate($this->width, $this->height); // fill background of image if ($this->backgroundColor == '') { // try to choose color that does not match // waveColor or progressColor // three colors should be sufficient $colors = array('#FFFFFF', '#000000', '#FF0000'); foreach ($colors as $col) { $tempBackground = $col; if ($tempBackground != strtoupper($this->waveColor) && $tempBackground != strtoupper($this->progressColor)) { break; } } } else { $tempBackground = $this->backgroundColor; } list($r, $g, $b) = JustWave::html2rgb($tempBackground); $transparentColor = imagecolorallocate($this->waveImg, $r, $g, $b); imagefilledrectangle($this->waveImg, 0, 0, $this->width, $this->height, $transparentColor); // generate foreground color list($r, $g, $b) = JustWave::html2rgb($this->waveColor); $waveColor = imagecolorallocate($this->waveImg, $r, $g, $b); // $subColor = imagecolorallocate($this->waveImg, (int)$r * 0.5 , (int)$g * 0.5, (int) $b * 0.5); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Read wave header // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $handle = fopen($wavfilename, 'rb'); $heading[] = fread($handle, 4); $heading[] = bin2hex(fread($handle, 4)); $heading[] = fread($handle, 4); $heading[] = fread($handle, 4); $heading[] = bin2hex(fread($handle, 4)); $heading[] = bin2hex(fread($handle, 2)); $heading[] = bin2hex(fread($handle, 2)); $heading[] = bin2hex(fread($handle, 4)); $heading[] = bin2hex(fread($handle, 4)); $heading[] = bin2hex(fread($handle, 2)); $heading[] = bin2hex(fread($handle, 2)); $heading[] = fread($handle, 4); $heading[] = bin2hex(fread($handle, 4)); if ($heading[5] != '0100') { $this->raiseError('Wave file should be a PCM file'); return false; } $peek = hexdec(substr($heading[10], 0, 2)); $byte = $peek / 8; $channel = hexdec(substr($heading[6], 0, 2)); // point = one data point (pixel), width total // block = one block, there are $accuracy blocks per point // chunk = one data point 8 or 16 bit, mono or stereo $filesize = filesize($wavfilename); $chunksize = $byte * $channel; $file_chunks = ($filesize - 44) / $chunksize; if ($file_chunks < $this->width) { $this->raiseError("Wave file has {$file_chunks} chunks, " . $this->width . ' required'); return false; } if ($file_chunks < $this->width * ACCURACY) { $accuracy = 1; } else { $accuracy = ACCURACY; } $point_chunks = $file_chunks / $this->width; $block_chunks = $file_chunks / ($this->width * $accuracy); $blocks = array(); $points = 0; $current_file_position = 44.0; // float, because chunks/point and clunks/block are floats too. fseek($handle, 44); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Read the data points and draw the image // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - while (!feof($handle)) { // The next file position is the float value rounded to the closest chunk // Read the next block, take the first value (of the first channel) $real_pos_diff = ($current_file_position - 44) % $chunksize; if ($real_pos_diff > $chunksize / 2) { $real_pos_diff -= $chunksize; } fseek($handle, $current_file_position - $real_pos_diff); $chunk = fread($handle, $chunksize); if (feof($handle) && !strlen($chunk)) { break; } $current_file_position += $block_chunks * $chunksize; if ($byte == 1) { $blocks[] = ord($chunk[0]); } else { $blocks[] = ord($chunk[1]) ^ 128; } // 16 bit // Do we have enough blocks for the current point? if (count($blocks) >= $accuracy) { // Calculate the mean and add the peak value to the array of blocks sort($blocks); $mean = count($blocks) % 2 ? $blocks[(count($blocks) - 1) / 2] : ($blocks[count($blocks) / 2] + $blocks[count($blocks) / 2 - 1]) / 2; if ($mean > 127) { $point = array_pop($blocks); } else { $point = array_shift($blocks); } // Draw $lineheight = round($point / 255 * $this->height); imageline($this->waveImg, $points, 0 + ($this->height - $lineheight), $points, $this->height - ($this->height - $lineheight), $waveColor); // update vars $points++; $blocks = array(); } } // close wave file fclose($handle); // final line imageline($this->waveImg, 0, round($this->height / 2), $this->width, round($this->height / 2), $waveColor); if ($this->waveColor != $this->progressColor) { $this->progressImg = imagecreate($this->width, $this->height); imagecopy($this->progressImg, $this->waveImg, 0, 0, 0, 0, $this->width, $this->height); // change waveColor to progressColor $index = imagecolorclosest($this->waveImg, $r, $g, $b); list($r, $g, $b) = JustWave::html2rgb($this->progressColor); imagecolorset($this->progressImg, $index, $r, $g, $b); } // try to save transparency if ($this->backgroundColor == '') { imagealphablending($this->waveImg, false); imagesavealpha($this->waveImg, true); imagealphablending($this->waveImg, true); imagecolortransparent($this->waveImg, $transparentColor); if ($this->waveColor != $this->progressColor) { imagealphablending($this->progressImg, false); imagesavealpha($this->progressImg, true); imagealphablending($this->progressImg, true); imagecolortransparent($this->progressImg, $transparentColor); } } return true; }
} if (!($transparenter_farbwert = imagecolorsforindex($bild, imagecolortransparent($bild)))) { $colorTransparent = imagecolorallocate($dest, 255, 42, 212); // Pinke Farbe imagefill($dest, 0, 0, $colorTransparent); $transparenter_farbwert = array(255, 42, 212); } // Originalbild verkleinern if (!($src = @imagecreatetruecolor($breite + $BLOCKSIZE_X, $hoehe + $BLOCKSIZE_Y))) { $src = imagecreate($breite + $BLOCKSIZE_X, $hoehe + $BLOCKSIZE_Y); } if (!@imagecopyresampled($src, $bild, 0, 0, 0, 0, $breite + $BLOCKSIZE_X, $hoehe + $BLOCKSIZE_Y, $b_x, $b_y)) { imagecopyresized($src, $bild, 0, 0, 0, 0, $breite + $BLOCKSIZE_X, $hoehe + $BLOCKSIZE_Y, $b_x, $b_y); } imagedestroy($bild); while (list(, $v) = each($xy)) { if (!@imagecopyresized($dest, $src, $v['x'], $v['y'], $v['x'], $v['y'], $BLOCKSIZE_X, $BLOCKSIZE_Y, $BLOCKSIZE_X, $BLOCKSIZE_Y)) { imagecopyresized($dest, $src, $v['x'], $v['y'], $v['x'], $v['y'], $BLOCKSIZE_X, $BLOCKSIZE_Y, $BLOCKSIZE_X, $BLOCKSIZE_Y); } } imagetruecolortopalette($dest, false, 256); if (imagecolorstotal($dest) > 1) { imagecolortransparent($dest, imagecolorclosest($dest, $transparenter_farbwert[0], $transparenter_farbwert[1], $transparenter_farbwert[2])); } ob_start(); imagepng($dest); $temp_pic = base64_encode(ob_get_contents()); ob_end_clean(); imagedestroy($dest); imagedestroy($src); return $temp_pic;
/** * Draw a GlowString at the specified location */ function imageGlowString($img, $font, $xx, $yy, $text, $color) { $width = imagefontwidth($font) * strlen($text); $height = imagefontheight($font); $text_image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($text_image, 255, 255, 255); $gray = imagecolorallocate($text_image, 80, 12, 200); imagestring($text_image, $font, 0, 0, $text, $gray); $out_image = imagecreatetruecolor($width + 6, $height + 6); $white = imagecolorallocate($out_image, 255, 255, 255); $black = imagecolorallocate($out_image, 70, 70, 0); imagefill($out_image, $width, $height, $white); $white = imagecolortransparent($out_image, $white); $dist = 1; $numelements = 9; for ($x = -1; $x < $width + 1; ++$x) { for ($y = -1; $y < $height + 1; ++$y) { $newr = 0; $newg = 0; $newb = 0; for ($k = $x - 1; $k <= $x + 1; ++$k) { for ($l = $y - 1; $l <= $y + 1; ++$l) { $colour = imagecolorat($text_image, $k, $l); $newr += $colour >> 16 & 0xff; $newg += $colour >> 8 & 0xff; $newb += $colour & 0xff; } } $newcol = imagecolorclosest($out_image, 255 - $newr / $numelements, 255 - $newg / $numelements, 255 - $newb / $numelements); imagesetpixel($out_image, $x + 3, $y + 3, $newcol); } } imagestring($out_image, $font, 3, 3, $text, $black); imagecopymerge($img, $out_image, $xx - 3, $yy - 3, 0, 0, $width + 6, $height + 6, 90); imagedestroy($text_image); imagedestroy($out_image); }
/** * add colorize effect for given gd resource<br/> * <b>Note :</b> * colorize method work with imagefilter function if exist but if not * it will also work but it will take more time * @param resource $virtual_image gd resource * @param array $color RGB Color as array ex: tulipIP::toRGB('#f00') * @return boolean true on success false otherwise */ public static function colorize($virtual_image, $color) { if (@get_resource_type($virtual_image) != TIP_RT) { self::logger("colorize", " Not A GD Resource"); return false; } if ($color != null && is_array($color)) { if (count($color) == 3) { $red = $color[0]; $green = $color[1]; $blue = $color[2]; } else { return false; } } else { return false; } self::activateAlphaChannel($virtual_image); $is_exist = function_exists('imagefilter'); if ($is_exist == true) { return @imagefilter($virtual_image, IMG_FILTER_COLORIZE, $red, $green, $blue); } else { self::logger("colorize", "GD Lib Does Not Support Colorize Advanced Method Running Slow Mode", TULIP_NOTICE); $width = self::getWidth($virtual_image); $height = self::getHeight($virtual_image); for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $color_index = @imagecolorat($virtual_image, $x, $y); $rgb = @imagecolorsforindex($virtual_image, $color_index); $r = (double) $color[0] + (double) $rgb['red']; $g = (double) $color[1] + (double) $rgb['green']; $b = (double) $color[2] + (double) $rgb['blue']; $r = (int) ($r > 255.0) ? 255.0 : ($r < 0.0 ? 0.0 : $r); $g = (int) ($g > 255.0) ? 255.0 : ($g < 0.0 ? 0.0 : $g); $b = (int) ($b > 255.0) ? 255.0 : ($b < 0.0 ? 0.0 : $b); $new_color = @imagecolorallocate($virtual_image, $r, $g, $b); if (!$new_color || $new_color == -1) { $closest_color = @imagecolorclosest($virtual_image, $r, $g, $b); $new_color = @imagecolorsforindex($virtual_image, $closest_color); } if (!@imagesetpixel($virtual_image, $x, $y, $new_color)) { self::logger("colorize", "Error Setting Pixels"); return false; } } } return true; } }
/** * 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)); $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1); $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2); // 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; $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)); 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); 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); break; } // Add the text to the source image if ($this->wm_use_truetype) { if ($this->wm_use_drop_shadow) { imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); } imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); } 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; }
/** * Create the image. * * @throws Exceptions\DataDoesntExistsException * @throws Exceptions\VersionTooLargeException * @throws Exceptions\ImageSizeTooLargeException * @throws \OverflowException */ public function create() { $image_path = $this->image_path; $path = $this->path; $version_ul = 40; $qrcode_data_string = $this->text; //Previously from $_GET["d"]; $qrcode_error_correct = $this->error_correction; //Previously from $_GET["e"]; $qrcode_module_size = $this->module_size; //Previously from $_GET["s"]; $qrcode_version = $this->version; //Previously from $_GET["v"]; $qrcode_image_type = $this->image_type; //Previously from $_GET["t"]; $qrcode_structureappend_n = $this->structure_append_n; //Previously from $_GET["n"]; $qrcode_structureappend_m = $this->structure_append_m; //Previously from $_GET["m"]; $qrcode_structureappend_parity = $this->structure_append_parity; //Previously from $_GET["p"]; $qrcode_structureappend_originaldata = $this->structure_append_original_data; //Previously from $_GET["o"]; if ($qrcode_module_size > 0) { } else { if ($qrcode_image_type == 'jpeg') { $qrcode_module_size = 8; } else { $qrcode_module_size = 4; } } $data_length = strlen($qrcode_data_string); if ($data_length <= 0) { throw new DataDoesntExistsException('QRCode: Data does not exists.'); } $data_counter = 0; if ($qrcode_structureappend_n > 1 && $qrcode_structureappend_n <= 16 && $qrcode_structureappend_m > 0 && $qrcode_structureappend_m <= 16) { $data_value[0] = 3; $data_bits[0] = 4; $data_value[1] = $qrcode_structureappend_m - 1; $data_bits[1] = 4; $data_value[2] = $qrcode_structureappend_n - 1; $data_bits[2] = 4; $originaldata_length = strlen($qrcode_structureappend_originaldata); if ($originaldata_length > 1) { $qrcode_structureappend_parity = 0; $i = 0; while ($i < $originaldata_length) { $qrcode_structureappend_parity = $qrcode_structureappend_parity ^ ord(substr($qrcode_structureappend_originaldata, $i, 1)); ++$i; } } $data_value[3] = $qrcode_structureappend_parity; $data_bits[3] = 8; $data_counter = 4; } $data_bits[$data_counter] = 4; /* --- determine encode mode */ if (preg_match('/[^0-9]/', $qrcode_data_string) != 0) { if (preg_match("/[^0-9A-Z \$\\*\\%\\+\\.\\/\\:\\-]/", $qrcode_data_string) != 0) { /* --- 8bit byte mode */ $codeword_num_plus = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8); $data_value[$data_counter] = 4; ++$data_counter; $data_value[$data_counter] = $data_length; $data_bits[$data_counter] = 8; /* #version 1-9 */ $codeword_num_counter_value = $data_counter; ++$data_counter; $i = 0; while ($i < $data_length) { $data_value[$data_counter] = ord(substr($qrcode_data_string, $i, 1)); $data_bits[$data_counter] = 8; ++$data_counter; ++$i; } } else { /* ---- alphanumeric mode */ $codeword_num_plus = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4); $data_value[$data_counter] = 2; ++$data_counter; $data_value[$data_counter] = $data_length; $data_bits[$data_counter] = 9; /* #version 1-9 */ $codeword_num_counter_value = $data_counter; $alphanumeric_character_hash = array('0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39, '+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44); $i = 0; ++$data_counter; while ($i < $data_length) { if ($i % 2 == 0) { $data_value[$data_counter] = $alphanumeric_character_hash[substr($qrcode_data_string, $i, 1)]; $data_bits[$data_counter] = 6; } else { $data_value[$data_counter] = $data_value[$data_counter] * 45 + $alphanumeric_character_hash[substr($qrcode_data_string, $i, 1)]; $data_bits[$data_counter] = 11; ++$data_counter; } ++$i; } } } else { /* ---- numeric mode */ $codeword_num_plus = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4); $data_value[$data_counter] = 1; ++$data_counter; $data_value[$data_counter] = $data_length; $data_bits[$data_counter] = 10; /* #version 1-9 */ $codeword_num_counter_value = $data_counter; $i = 0; ++$data_counter; while ($i < $data_length) { if ($i % 3 == 0) { $data_value[$data_counter] = substr($qrcode_data_string, $i, 1); $data_bits[$data_counter] = 4; } else { $data_value[$data_counter] = $data_value[$data_counter] * 10 + substr($qrcode_data_string, $i, 1); if ($i % 3 == 1) { $data_bits[$data_counter] = 7; } else { $data_bits[$data_counter] = 10; ++$data_counter; } } ++$i; } } if (array_key_exists($data_counter, $data_bits) && $data_bits[$data_counter] > 0) { ++$data_counter; } $i = 0; $total_data_bits = 0; while ($i < $data_counter) { $total_data_bits += $data_bits[$i]; ++$i; } $ecc_character_hash = array('L' => '1', 'l' => '1', 'M' => '0', 'm' => '0', 'Q' => '3', 'q' => '3', 'H' => '2', 'h' => '2'); if (!is_numeric($qrcode_error_correct)) { $ec = @$ecc_character_hash[$qrcode_error_correct]; } else { $ec = $qrcode_error_correct; } if (!$ec) { $ec = 0; } $max_data_bits = 0; $max_data_bits_array = array(0, 128, 224, 352, 512, 688, 864, 992, 1232, 1456, 1728, 2032, 2320, 2672, 2920, 3320, 3624, 4056, 4504, 5016, 5352, 5712, 6256, 6880, 7312, 8000, 8496, 9024, 9544, 10136, 10984, 11640, 12328, 13048, 13800, 14496, 15312, 15936, 16816, 17728, 18672, 152, 272, 440, 640, 864, 1088, 1248, 1552, 1856, 2192, 2592, 2960, 3424, 3688, 4184, 4712, 5176, 5768, 6360, 6888, 7456, 8048, 8752, 9392, 10208, 10960, 11744, 12248, 13048, 13880, 14744, 15640, 16568, 17528, 18448, 19472, 20528, 21616, 22496, 23648, 72, 128, 208, 288, 368, 480, 528, 688, 800, 976, 1120, 1264, 1440, 1576, 1784, 2024, 2264, 2504, 2728, 3080, 3248, 3536, 3712, 4112, 4304, 4768, 5024, 5288, 5608, 5960, 6344, 6760, 7208, 7688, 7888, 8432, 8768, 9136, 9776, 10208, 104, 176, 272, 384, 496, 608, 704, 880, 1056, 1232, 1440, 1648, 1952, 2088, 2360, 2600, 2936, 3176, 3560, 3880, 4096, 4544, 4912, 5312, 5744, 6032, 6464, 6968, 7288, 7880, 8264, 8920, 9368, 9848, 10288, 10832, 11408, 12016, 12656, 13328); if (!is_numeric($qrcode_version)) { $qrcode_version = 0; } if (!$qrcode_version) { /* #--- auto version select */ $i = 1 + 40 * $ec; $j = $i + 39; $qrcode_version = 1; while ($i <= $j) { if ($max_data_bits_array[$i] >= $total_data_bits + $codeword_num_plus[$qrcode_version]) { $max_data_bits = $max_data_bits_array[$i]; break; } ++$i; ++$qrcode_version; } } else { $max_data_bits = $max_data_bits_array[$qrcode_version + 40 * $ec]; } if ($qrcode_version > $version_ul) { throw new VersionTooLargeException('QRCode : version too large'); } $total_data_bits += $codeword_num_plus[$qrcode_version]; $data_bits[$codeword_num_counter_value] += $codeword_num_plus[$qrcode_version]; $max_codewords_array = array(0, 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706); $max_codewords = $max_codewords_array[$qrcode_version]; $max_modules_1side = 17 + ($qrcode_version << 2); $matrix_remain_bit = array(0, 0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0); /* ---- read version ECC data file */ $byte_num = $matrix_remain_bit[$qrcode_version] + ($max_codewords << 3); $filename = $path . '/qrv' . $qrcode_version . '_' . $ec . '.dat'; $fp1 = fopen($filename, 'rb'); $matx = fread($fp1, $byte_num); $maty = fread($fp1, $byte_num); $masks = fread($fp1, $byte_num); $fi_x = fread($fp1, 15); $fi_y = fread($fp1, 15); $rs_ecc_codewords = ord(fread($fp1, 1)); $rso = fread($fp1, 128); fclose($fp1); $matrix_x_array = unpack('C*', $matx); $matrix_y_array = unpack('C*', $maty); $mask_array = unpack('C*', $masks); $rs_block_order = unpack('C*', $rso); $format_information_x2 = unpack('C*', $fi_x); $format_information_y2 = unpack('C*', $fi_y); $format_information_x1 = array(0, 1, 2, 3, 4, 5, 7, 8, 8, 8, 8, 8, 8, 8, 8); $format_information_y1 = array(8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 4, 3, 2, 1, 0); $max_data_codewords = $max_data_bits >> 3; $filename = $path . '/rsc' . $rs_ecc_codewords . '.dat'; $fp0 = fopen($filename, 'rb'); $i = 0; $rs_cal_table_array = array(); while ($i < 256) { $rs_cal_table_array[$i] = fread($fp0, $rs_ecc_codewords); ++$i; } fclose($fp0); /* --- set terminator */ if ($total_data_bits <= $max_data_bits - 4) { $data_value[$data_counter] = 0; $data_bits[$data_counter] = 4; } else { if ($total_data_bits < $max_data_bits) { $data_value[$data_counter] = 0; $data_bits[$data_counter] = $max_data_bits - $total_data_bits; } else { if ($total_data_bits > $max_data_bits) { throw new \OverflowException('QRCode : Overflow error'); } } } /* ----divide data by 8bit */ $i = 0; $codewords_counter = 0; $codewords[0] = 0; $remaining_bits = 8; while ($i <= $data_counter) { $buffer = @$data_value[$i]; $buffer_bits = @$data_bits[$i]; $flag = 1; while ($flag) { if ($remaining_bits > $buffer_bits) { $codewords[$codewords_counter] = @$codewords[$codewords_counter] << $buffer_bits | $buffer; $remaining_bits -= $buffer_bits; $flag = 0; } else { $buffer_bits -= $remaining_bits; $codewords[$codewords_counter] = $codewords[$codewords_counter] << $remaining_bits | $buffer >> $buffer_bits; if ($buffer_bits == 0) { $flag = 0; } else { $buffer = $buffer & (1 << $buffer_bits) - 1; $flag = 1; } ++$codewords_counter; if ($codewords_counter < $max_data_codewords - 1) { $codewords[$codewords_counter] = 0; } $remaining_bits = 8; } } ++$i; } if ($remaining_bits != 8) { $codewords[$codewords_counter] = $codewords[$codewords_counter] << $remaining_bits; } else { --$codewords_counter; } /* ---- set padding character */ if ($codewords_counter < $max_data_codewords - 1) { $flag = 1; while ($codewords_counter < $max_data_codewords - 1) { ++$codewords_counter; if ($flag == 1) { $codewords[$codewords_counter] = 236; } else { $codewords[$codewords_counter] = 17; } $flag = $flag * -1; } } /* ---- RS-ECC prepare */ $i = 0; $j = 0; $rs_block_number = 0; $rs_temp[0] = ''; while ($i < $max_data_codewords) { $rs_temp[$rs_block_number] .= chr($codewords[$i]); ++$j; if ($j >= $rs_block_order[$rs_block_number + 1] - $rs_ecc_codewords) { $j = 0; ++$rs_block_number; $rs_temp[$rs_block_number] = ''; } ++$i; } /* # # RS-ECC main # */ $rs_block_number = 0; $rs_block_order_num = count($rs_block_order); while ($rs_block_number < $rs_block_order_num) { $rs_codewords = $rs_block_order[$rs_block_number + 1]; $rs_data_codewords = $rs_codewords - $rs_ecc_codewords; $rstemp = $rs_temp[$rs_block_number] . str_repeat(chr(0), $rs_ecc_codewords); $padding_data = str_repeat(chr(0), $rs_data_codewords); $j = $rs_data_codewords; while ($j > 0) { $first = ord(substr($rstemp, 0, 1)); if ($first) { $left_chr = substr($rstemp, 1); $cal = $rs_cal_table_array[$first] . $padding_data; $rstemp = $left_chr ^ $cal; } else { $rstemp = substr($rstemp, 1); } --$j; } $codewords = array_merge($codewords, unpack('C*', $rstemp)); ++$rs_block_number; } /* ---- flash matrix */ $matrix_content = array(); $i = 0; while ($i < $max_modules_1side) { $j = 0; while ($j < $max_modules_1side) { $matrix_content[$j][$i] = 0; ++$j; } ++$i; } /* --- attach data */ $i = 0; while ($i < $max_codewords) { $codeword_i = $codewords[$i]; $j = 8; while ($j >= 1) { $codeword_bits_number = ($i << 3) + $j; $matrix_content[$matrix_x_array[$codeword_bits_number]][$matrix_y_array[$codeword_bits_number]] = 255 * ($codeword_i & 1) ^ $mask_array[$codeword_bits_number]; $codeword_i = $codeword_i >> 1; --$j; } ++$i; } $matrix_remain = $matrix_remain_bit[$qrcode_version]; while ($matrix_remain) { $remain_bit_temp = $matrix_remain + ($max_codewords << 3); $matrix_content[$matrix_x_array[$remain_bit_temp]][$matrix_y_array[$remain_bit_temp]] = 255 ^ $mask_array[$remain_bit_temp]; --$matrix_remain; } #--- mask select $min_demerit_score = 0; $hor_master = ''; $ver_master = ''; $k = 0; while ($k < $max_modules_1side) { $l = 0; while ($l < $max_modules_1side) { $hor_master = $hor_master . chr($matrix_content[$l][$k]); $ver_master = $ver_master . chr($matrix_content[$k][$l]); ++$l; } ++$k; } $i = 0; $all_matrix = $max_modules_1side * $max_modules_1side; $mask_number = 0; while ($i < 8) { $demerit_n1 = 0; $ptn_temp = array(); $bit = 1 << $i; $bit_r = ~$bit & 255; $bit_mask = str_repeat(chr($bit), $all_matrix); $hor = $hor_master & $bit_mask; $ver = $ver_master & $bit_mask; $ver_shift1 = $ver . str_repeat(chr(170), $max_modules_1side); $ver_shift2 = str_repeat(chr(170), $max_modules_1side) . $ver; $ver_shift1_0 = $ver . str_repeat(chr(0), $max_modules_1side); $ver_shift2_0 = str_repeat(chr(0), $max_modules_1side) . $ver; $ver_or = chunk_split(~($ver_shift1 | $ver_shift2), $max_modules_1side, chr(170)); $ver_and = chunk_split(~($ver_shift1_0 & $ver_shift2_0), $max_modules_1side, chr(170)); $hor = chunk_split(~$hor, $max_modules_1side, chr(170)); $ver = chunk_split(~$ver, $max_modules_1side, chr(170)); $hor = $hor . chr(170) . $ver; $n1_search = '/' . str_repeat(chr(255), 5) . '+|' . str_repeat(chr($bit_r), 5) . '+/'; $n3_search = chr($bit_r) . chr(255) . chr($bit_r) . chr($bit_r) . chr($bit_r) . chr(255) . chr($bit_r); $demerit_n3 = substr_count($hor, $n3_search) * 40; $demerit_n4 = floor(abs((100 * (substr_count($ver, chr($bit_r)) / $byte_num) - 50) / 5)) * 10; $n2_search1 = '/' . chr($bit_r) . chr($bit_r) . '+/'; $n2_search2 = '/' . chr(255) . chr(255) . '+/'; $demerit_n2 = 0; preg_match_all($n2_search1, $ver_and, $ptn_temp); foreach ($ptn_temp[0] as $str_temp) { $demerit_n2 += strlen($str_temp) - 1; } $ptn_temp = array(); preg_match_all($n2_search2, $ver_or, $ptn_temp); foreach ($ptn_temp[0] as $str_temp) { $demerit_n2 += strlen($str_temp) - 1; } $demerit_n2 *= 3; $ptn_temp = array(); preg_match_all($n1_search, $hor, $ptn_temp); foreach ($ptn_temp[0] as $str_temp) { $demerit_n1 += strlen($str_temp) - 2; } $demerit_score = $demerit_n1 + $demerit_n2 + $demerit_n3 + $demerit_n4; if ($demerit_score <= $min_demerit_score || $i == 0) { $mask_number = $i; $min_demerit_score = $demerit_score; } ++$i; } $mask_content = 1 << $mask_number; # --- format information $format_information_value = $ec << 3 | $mask_number; $format_information_array = array('101010000010010', '101000100100101', '101111001111100', '101101101001011', '100010111111001', '100000011001110', '100111110010111', '100101010100000', '111011111000100', '111001011110011', '111110110101010', '111100010011101', '110011000101111', '110001100011000', '110110001000001', '110100101110110', '001011010001001', '001001110111110', '001110011100111', '001100111010000', '000011101100010', '000001001010101', '000110100001100', '000100000111011', '011010101011111', '011000001101000', '011111100110001', '011101000000110', '010010010110100', '010000110000011', '010111011011010', '010101111101101'); $i = 0; while ($i < 15) { $content = substr($format_information_array[$format_information_value], $i, 1); $matrix_content[$format_information_x1[$i]][$format_information_y1[$i]] = $content * 255; $matrix_content[$format_information_x2[$i + 1]][$format_information_y2[$i + 1]] = $content * 255; ++$i; } $mib = $max_modules_1side + 8; if ($this->size == 0) { $this->size = $mib * $qrcode_module_size; if ($this->size > 1480) { throw new ImageSizeTooLargeException('QRCode : Image size too large'); } } $image_width = $this->size + $this->padding * 2; $image_height = $this->size + $this->padding * 2; if (!empty($this->label)) { $font_box = imagettfbbox($this->label_font_size, 0, $this->label_font_path, $this->label); $label_width = (int) $font_box[2] - (int) $font_box[0]; $label_height = (int) $font_box[0] - (int) $font_box[7]; $image_height += $label_height + $this->padding; } $output_image = imagecreate($image_width, $image_height); imagecolorallocate($output_image, 255, 255, 255); if (!empty($this->label)) { $font_x = floor($image_width - $label_width) / 2; $font_y = $image_height - $this->padding; $color = imagecolorallocate($output_image, 0, 0, 0); imagettftext($output_image, $this->label_font_size, 0, $font_x, $font_y, $color, $this->label_font_path, $this->label); } $image_path = $image_path . '/qrv' . $qrcode_version . '.png'; $base_image = imagecreatefrompng($image_path); $col[1] = imagecolorallocate($base_image, 0, 0, 0); $col[0] = imagecolorallocate($base_image, 255, 255, 255); $i = 4; $mxe = 4 + $max_modules_1side; $ii = 0; while ($i < $mxe) { $j = 4; $jj = 0; while ($j < $mxe) { if ($matrix_content[$ii][$jj] & $mask_content) { imagesetpixel($base_image, $i, $j, $col[1]); } ++$j; ++$jj; } ++$i; ++$ii; } imagecopyresampled($output_image, $base_image, $this->padding, $this->padding, 4, 4, $this->size, $this->size, $mib - 8, $mib - 8); $imagecolorset_function = new ReflectionFunction('imagecolorset'); $allow_alpha = $imagecolorset_function->getNumberOfParameters() == 6; if ($this->color_background != null) { $index = imagecolorclosest($output_image, 255, 255, 255); if ($allow_alpha) { imagecolorset($output_image, $index, $this->color_background['r'], $this->color_background['g'], $this->color_background['b'], $this->color_background['a']); } else { imagecolorset($output_image, $index, $this->color_background['r'], $this->color_background['g'], $this->color_background['b']); } } if ($this->color_foreground != null) { $index = imagecolorclosest($output_image, 0, 0, 0); if ($allow_alpha) { imagecolorset($output_image, $index, $this->color_foreground['r'], $this->color_foreground['g'], $this->color_foreground['b'], $this->color_foreground['a']); } else { imagecolorset($output_image, $index, $this->color_foreground['r'], $this->color_foreground['g'], $this->color_foreground['b']); } } $this->image = $output_image; }
/** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * If the function is called with a null or empty argument, then it will return the content of the picture * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Optional path location of the uploaded file, with an ending slash * @return string Optional content of the image */ function process($server_path = null) { $this->error = ''; $this->processed = true; $return_mode = false; $return_content = null; // clean up dst variables $this->file_dst_path = ''; $this->file_dst_pathname = ''; $this->file_dst_name = ''; $this->file_dst_name_body = ''; $this->file_dst_name_ext = ''; if (!$this->uploaded) { $this->error = $this->translate('file_not_uploaded'); $this->processed = false; } if ($this->processed) { if (empty($server_path) || is_null($server_path)) { $this->log .= '<b>process file and return the content</b><br />'; $return_mode = true; } else { if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') { $server_path = $server_path . '\\'; } } else { if (substr($server_path, -1, 1) != '/') { $server_path = $server_path . '/'; } } $this->log .= '<b>process file to ' . $server_path . '</b><br />'; } } if ($this->processed) { // checks file max size if ($this->file_src_size > $this->file_max_size) { $this->processed = false; $this->error = $this->translate('file_too_big'); } else { $this->log .= '- file size OK<br />'; } } if ($this->processed) { // if we have an image without extension, set it if ($this->file_is_image && !$this->file_src_name_ext_) { $this->file_src_name_ext = $this->file_src_name_ext_ = $this->image_src_type; } // turn dangerous scripts into text files if ($this->no_script) { if ((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf' || strpos($this->file_src_mime, 'javascript') !== false) && substr($this->file_src_name, -4) != '.txt' || preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $this->file_src_name) || empty($this->file_src_name_ext_)) { $this->file_src_mime = 'text/plain'; $this->log .= '- script ' . $this->file_src_name . ' renamed as ' . $this->file_src_name . '.txt!<br />'; $this->file_src_name_ext = $this->file_src_name_ext_ . (empty($this->file_src_name_ext_) ? 'txt' : '.txt'); } } if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = $this->translate('no_mime'); } else { if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) { list($m1, $m2) = explode('/', $this->file_src_mime); $allowed = false; // check wether the mime type is allowed foreach ($this->allowed as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = true; break; } } // check wether the mime type is forbidden foreach ($this->forbidden as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = false; break; } } if (!$allowed) { $this->processed = false; $this->error = $this->translate('incorrect_file'); } else { $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />'; } } else { $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />'; } } // if the file is an image, we can check on its dimensions // these checks are not available if open_basedir restrictions are in place if ($this->file_is_image) { if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) { $ratio = $this->image_src_x / $this->image_src_y; if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) { $this->processed = false; $this->error = $this->translate('image_too_wide'); } if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) { $this->processed = false; $this->error = $this->translate('image_too_narrow'); } if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) { $this->processed = false; $this->error = $this->translate('image_too_high'); } if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) { $this->processed = false; $this->error = $this->translate('image_too_short'); } if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_high'); } if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_low'); } if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) { $this->processed = false; $this->error = $this->translate('too_many_pixels'); } if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) { $this->processed = false; $this->error = $this->translate('not_enough_pixels'); } } else { $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />'; } } } if ($this->processed) { $this->file_dst_path = $server_path; // repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext; if ($this->file_overwrite) { $this->file_auto_rename = false; } if ($this->image_convert != '') { // if we convert as an image $this->file_dst_name_ext = $this->image_convert; $this->log .= '- new file name ext : ' . $this->image_convert . '<br />'; } if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />'; } if ($this->file_name_body_add != '') { // append a string to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />'; } if ($this->file_name_body_pre != '') { // prepend a string to the name $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body; $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_', '_'), $this->file_dst_name_body); $this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body); $this->log .= '- file name safe format<br />'; } $this->log .= '- destination variables<br />'; if (empty($this->file_dst_path) || is_null($this->file_dst_path)) { $this->log .= ' file_dst_path : n/a<br />'; } else { $this->log .= ' file_dst_path : ' . $this->file_dst_path . '<br />'; } $this->log .= ' file_dst_name_body : ' . $this->file_dst_name_body . '<br />'; $this->log .= ' file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />'; // do we do some image manipulation? $image_manipulation = $this->file_is_image && ($this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || $this->image_unsharp || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_precrop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height); if ($image_manipulation) { if ($this->image_convert == '') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- image operation, keep extension<br />'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- image operation, change extension for conversion type<br />'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- no image operation, keep extension<br />'; } if (!$return_mode) { if (!$this->file_auto_rename) { $this->log .= '- no auto_rename if same filename exists<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- checking for auto_rename<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt > 1) { $this->log .= ' auto_rename to ' . $this->file_dst_name . '<br />'; } } $this->log .= '- destination file details<br />'; $this->log .= ' file_dst_name : ' . $this->file_dst_name . '<br />'; $this->log .= ' file_dst_pathname : ' . $this->file_dst_pathname . '<br />'; if ($this->file_overwrite) { $this->log .= '- no overwrite checking<br />'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('already_exists', array($this->file_dst_name)); } else { $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />'; } } } } if ($this->processed) { // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists if (!empty($this->file_src_temp)) { $this->log .= '- use the temp file instead of the original file since it is a second process<br />'; $this->file_src_pathname = $this->file_src_temp; if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('temp_file_missing'); } // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file() } else { if (!$this->no_upload_check) { if (!is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists() } else { if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } } } // checks if the destination directory exists, and attempt to create it if (!$return_mode) { if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:'; if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir'); } else { $this->log .= ' success<br />'; } } else { $this->error = $this->translate('destination_dir_missing'); } } if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = $this->translate('destination_path_not_dir'); } // checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:'; if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { $this->log .= ' success<br />'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { @fclose($f); } } } else { $this->processed = false; $this->error = $this->translate('destination_path_write'); } } else { if ($this->processed) { @fclose($f); } @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); } // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction) // then we create a temp file that will be used as the source file in subsequent processes // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists) if (!$this->no_upload_check && empty($this->file_src_temp) && !@file_exists($this->file_src_pathname)) { $this->log .= '- attempting to use a temp file:'; $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->file_src_temp = $this->file_src_pathname; $this->log .= ' file created<br />'; $this->log .= ' temp file is: ' . $this->file_src_temp . '<br />'; } else { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('temp_file'); } } } } if ($this->processed) { // we do a quick check to ensure the file is really an image // we can do this only now, as it would have failed before in case of open_basedir if ($image_manipulation && !@getimagesize($this->file_src_pathname)) { $this->log .= '- the file is not an image!<br />'; $image_manipulation = false; } if ($image_manipulation) { // checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = $this->translate('source_not_readable'); } else { @fclose($f); } // we now do all the image manipulations $this->log .= '- image resizing or conversion wanted<br />'; if ($this->gdversion()) { switch ($this->image_src_type) { case 'jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('JPEG')); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('JPEG')); } else { $this->log .= '- source image is JPEG<br />'; } } break; case 'png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('PNG')); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('PNG')); } else { $this->log .= '- source image is PNG<br />'; } } break; case 'gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('GIF')); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('GIF')); } else { $this->log .= '- source image is GIF<br />'; } } break; case 'bmp': if (!method_exists($this, 'imagecreatefrombmp')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('BMP')); } else { $image_src = @$this->imagecreatefrombmp($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('BMP')); } else { $this->log .= '- source image is BMP<br />'; } } break; default: $this->processed = false; $this->error = $this->translate('source_invalid'); } } else { $this->processed = false; $this->error = $this->translate('gd_missing'); } if ($this->processed && $image_src) { // we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= '- setting destination file type to ' . $this->file_src_name_ext . '<br />'; $this->image_convert = $this->file_src_name_ext; } if (!in_array($this->image_convert, $this->image_supported)) { $this->image_convert = 'jpg'; } // we set the default color to be the background color if we don't output in a transparent format if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color) && empty($this->image_background_color)) { $this->image_background_color = $this->image_default_color; } if (!empty($this->image_background_color)) { $this->image_default_color = $this->image_background_color; } if (empty($this->image_default_color)) { $this->image_default_color = '#FFFFFF'; } $this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $gd_version = $this->gdversion(); $ratio_crop = null; if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif' $this->log .= '- image is detected as having a palette<br />'; $this->image_is_palette = true; $this->image_transparent_color = imagecolortransparent($image_src); if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) { $this->image_is_transparent = true; $this->log .= ' palette image is detected as transparent<br />'; } // if the image has a palette (GIF), we convert it to true color, preserving transparency $this->log .= ' convert palette image to true color<br />'; $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y); imagealphablending($true_color, false); imagesavealpha($true_color, true); for ($x = 0; $x < $this->image_src_x; $x++) { for ($y = 0; $y < $this->image_src_y; $y++) { if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) { imagesetpixel($true_color, $x, $y, 127 << 24); } else { $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y)); imagesetpixel($true_color, $x, $y, $rgb['alpha'] << 24 | $rgb['red'] << 16 | $rgb['green'] << 8 | $rgb['blue']); } } } $image_src = $this->imagetransfer($true_color, $image_src); imagealphablending($image_src, false); imagesavealpha($image_src, true); $this->image_is_palette = false; } $image_dst =& $image_src; // pre-crop image, before resizing if (!empty($this->image_precrop)) { if (is_array($this->image_precrop)) { $vars = $this->image_precrop; } else { $vars = explode(' ', $this->image_precrop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_src_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_src_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_src_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_src_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_src_x = $this->image_src_x - $cl - $cr; $this->image_src_y = $this->image_src_y - $ct - $cb; if ($this->image_src_x < 1) { $this->image_src_x = 1; } if ($this->image_src_y < 1) { $this->image_src_y = 1; } $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y) if ($this->image_resize) { $this->log .= '- resizing...<br />'; if ($this->image_ratio_x) { $this->log .= ' calculate x size<br />'; $this->image_dst_x = round($this->image_src_x * $this->image_y / $this->image_src_y); $this->image_dst_y = $this->image_y; } else { if ($this->image_ratio_y) { $this->log .= ' calculate y size<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = round($this->image_src_y * $this->image_x / $this->image_src_x); } else { if (is_numeric($this->image_ratio_pixels)) { $this->log .= ' calculate x/y size to match a number of pixels<br />'; $pixels = $this->image_src_y * $this->image_src_x; $diff = sqrt($this->image_ratio_pixels / $pixels); $this->image_dst_x = round($this->image_src_x * $diff); $this->image_dst_y = round($this->image_src_y * $diff); } else { if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) { $this->log .= ' check x/y sizes<br />'; if (!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out || $this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y) || $this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; if ($this->image_ratio_crop) { if (!is_string($this->image_ratio_crop)) { $this->image_ratio_crop = ''; } $this->image_ratio_crop = strtolower($this->image_ratio_crop); if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_crop, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_crop, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_crop_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_crop, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_crop, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_crop_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_ratio_fill) { if (!is_string($this->image_ratio_fill)) { $this->image_ratio_fill = ''; } $this->image_ratio_fill = strtolower($this->image_ratio_fill); if ($this->image_src_x / $this->image_x < $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_fill, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_fill, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_fill_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_fill, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_fill, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_fill_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); } else { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); } } } } else { $this->log .= ' doesn\'t calculate x/y sizes<br />'; $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } } else { $this->log .= ' use plain sizes<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; } } } } if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); if ($gd_version >= 2) { $res = imagecopyresampled($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } else { $res = imagecopyresized($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } $this->log .= ' resized image object created<br />'; $this->log .= ' image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />'; $this->log .= ' image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />'; // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } else { $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } // crop image (and also crops if image_ratio_crop is used) if (!empty($this->image_crop) || !is_null($ratio_crop)) { if (is_array($this->image_crop)) { $vars = $this->image_crop; } else { $vars = explode(' ', $this->image_crop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; // we adjust the cropping if we use image_ratio_crop if (!is_null($ratio_crop)) { if (array_key_exists('t', $ratio_crop)) { $ct += $ratio_crop['t']; } if (array_key_exists('r', $ratio_crop)) { $cr += $ratio_crop['r']; } if (array_key_exists('b', $ratio_crop)) { $cb += $ratio_crop['b']; } if (array_key_exists('l', $ratio_crop)) { $cl += $ratio_crop['l']; } } $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_dst_x = $this->image_dst_x - $cl - $cr; $this->image_dst_y = $this->image_dst_y - $ct - $cb; if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct - 1, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl - 1, $this->image_dst_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // flip image if ($gd_version >= 2 && !empty($this->image_flip)) { $this->image_flip = strtolower($this->image_flip); $this->log .= '- flip image : ' . $this->image_flip . '<br />'; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if (strpos($this->image_flip, 'v') !== false) { imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1); } } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // rotate image if ($gd_version >= 2 && is_numeric($this->image_rotate)) { if (!in_array($this->image_rotate, array(0, 90, 180, 270))) { $this->image_rotate = 0; } if ($this->image_rotate != 0) { if ($this->image_rotate == 90 || $this->image_rotate == 270) { $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x); } else { $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); } $this->log .= '- rotate image : ' . $this->image_rotate . '<br />'; for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($this->image_rotate == 90) { imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 180) { imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 270) { imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1); } } } } } if ($this->image_rotate == 90 || $this->image_rotate == 270) { $t = $this->image_dst_y; $this->image_dst_y = $this->image_dst_x; $this->image_dst_x = $t; } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } } // unsharp mask if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) { // Unsharp Mask for PHP - version 2.1.1 // Unsharp mask algorithm by Torstein Hønsi 2003-07. // Used with permission // Modified to support alpha transparency if ($this->image_unsharp_amount > 500) { $this->image_unsharp_amount = 500; } $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016; if ($this->image_unsharp_radius > 50) { $this->image_unsharp_radius = 50; } $this->image_unsharp_radius = $this->image_unsharp_radius * 2; if ($this->image_unsharp_threshold > 255) { $this->image_unsharp_threshold = 255; } $this->image_unsharp_radius = abs(round($this->image_unsharp_radius)); if ($this->image_unsharp_radius != 0) { $this->image_dst_x = imagesx($image_dst); $this->image_dst_y = imagesy($image_dst); $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); if (function_exists('imageconvolution')) { // PHP >= 5.1 $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)); imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imageconvolution($blur, $matrix, 16, 0); } else { for ($i = 0; $i < $this->image_unsharp_radius; $i++) { imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y); // left $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // right $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // center imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333); // up $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25); // down } } $p_new = array(); if ($this->image_unsharp_threshold > 0) { for ($x = 0; $x < $this->image_dst_x - 1; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red'])) : $p_orig['red']; $p_new['green'] = abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green'])) : $p_orig['green']; $p_new['blue'] = abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue'])) : $p_orig['blue']; if ($p_orig['red'] != $p_new['red'] || $p_orig['green'] != $p_new['green'] || $p_orig['blue'] != $p_new['blue']) { $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } else { for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red']; if ($p_new['red'] > 255) { $p_new['red'] = 255; } elseif ($p_new['red'] < 0) { $p_new['red'] = 0; } $p_new['green'] = $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green']; if ($p_new['green'] > 255) { $p_new['green'] = 255; } elseif ($p_new['green'] < 0) { $p_new['green'] = 0; } $p_new['blue'] = $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue']; if ($p_new['blue'] > 255) { $p_new['blue'] = 255; } elseif ($p_new['blue'] < 0) { $p_new['blue'] = 0; } $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } imagedestroy($canvas); imagedestroy($blur); } } // add color overlay if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && $this->image_overlay_percent > 0 && !empty($this->image_overlay_color))) { $this->log .= '- apply color overlay<br />'; list($red, $green, $blue) = $this->getcolors($this->image_overlay_color); $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color); $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent); imagedestroy($filter); } // add brightness, contrast and tint, turns to greyscale and inverts colors if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold) || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) { $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />'; if (!empty($this->image_tint_color)) { list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color); } imagealphablending($image_dst, true); for ($y = 0; $y < $this->image_dst_y; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { if ($this->image_greyscale) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = $g = $b = round(0.2125 * $pixel['red'] + 0.7154 * $pixel['green'] + 0.0721 * $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_threshold)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $c = round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3 - 127; $r = $g = $b = $c > $this->image_threshold ? 255 : 0; $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_brightness)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round($pixel['red'] + $this->image_brightness * 2), 255), 0); $g = max(min(round($pixel['green'] + $this->image_brightness * 2), 255), 0); $b = max(min(round($pixel['blue'] + $this->image_brightness * 2), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_contrast)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0); $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0); $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_tint_color)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = min(round($tint_red * $pixel['red'] / 169), 255); $g = min(round($tint_green * $pixel['green'] / 169), 255); $b = min(round($tint_blue * $pixel['blue'] / 169), 255); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_negative)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = round(255 - $pixel['red']); $g = round(255 - $pixel['green']); $b = round(255 - $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } // adds a border if ($gd_version >= 2 && !empty($this->image_border)) { if (is_array($this->image_border)) { $vars = $this->image_border; $this->log .= '- add border : ' . implode(' ', $this->image_border) . '<br />'; } else { $this->log .= '- add border : ' . $this->image_border . '<br />'; $vars = explode(' ', $this->image_border); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->image_dst_x = $this->image_dst_x + $cl + $cr; $this->image_dst_y = $this->image_dst_y + $ct + $cb; if (!empty($this->image_border_color)) { list($red, $green, $blue) = $this->getcolors($this->image_border_color); } // we now create an image, that we fill with the border color $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0); imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background); // we then copy the source image into the new image, without merging so that only the border is actually kept imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct); // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add frame border if (is_numeric($this->image_frame)) { if (is_array($this->image_frame_colors)) { $vars = $this->image_frame_colors; $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />'; } else { $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />'; $vars = explode(' ', $this->image_frame_colors); } $nb = sizeof($vars); $this->image_dst_x = $this->image_dst_x + $nb * 2; $this->image_dst_y = $this->image_dst_y + $nb * 2; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - $nb * 2, $this->image_dst_y - $nb * 2); for ($i = 0; $i < $nb; $i++) { list($red, $green, $blue) = $this->getcolors($vars[$i]); $c = imagecolorallocate($tmp, $red, $green, $blue); if ($this->image_frame == 1) { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } else { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add bevel border if ($this->image_bevel > 0) { if (empty($this->image_bevel_color1)) { $this->image_bevel_color1 = '#FFFFFF'; } if (empty($this->image_bevel_color2)) { $this->image_bevel_color2 = '#000000'; } list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1); list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2); $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagealphablending($tmp, true); for ($i = 0; $i < $this->image_bevel; $i++) { $alpha = round($i / $this->image_bevel * 127); $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha); $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha); imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c1); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i, $this->image_dst_x - $i - 1, $i, $c2); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c2); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c1); } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add watermark image if ($this->image_watermark != '' && file_exists($this->image_watermark)) { $this->log .= '- add watermark<br />'; $this->image_watermark_position = strtolower($this->image_watermark_position); $watermark_info = getimagesize($this->image_watermark); $watermark_type = array_key_exists(2, $watermark_info) ? $watermark_info[2] : null; // 1 = GIF, 2 = JPG, 3 = PNG $watermark_checked = false; if ($watermark_type == IMAGETYPE_GIF) { if (!function_exists('imagecreatefromgif')) { $this->error = $this->translate('watermark_no_create_support', array('GIF')); } else { $filter = @imagecreatefromgif($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('GIF')); } else { $this->log .= ' watermark source image is GIF<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_JPEG) { if (!function_exists('imagecreatefromjpeg')) { $this->error = $this->translate('watermark_no_create_support', array('JPEG')); } else { $filter = @imagecreatefromjpeg($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('JPEG')); } else { $this->log .= ' watermark source image is JPEG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_PNG) { if (!function_exists('imagecreatefrompng')) { $this->error = $this->translate('watermark_no_create_support', array('PNG')); } else { $filter = @imagecreatefrompng($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('PNG')); } else { $this->log .= ' watermark source image is PNG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_BMP) { if (!method_exists($this, 'imagecreatefrombmp')) { $this->error = $this->translate('watermark_no_create_support', array('BMP')); } else { $filter = @$this->imagecreatefrombmp($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('BMP')); } else { $this->log .= ' watermark source image is BMP<br />'; $watermark_checked = true; } } } else { $this->error = $this->translate('watermark_invalid'); } } } } if ($watermark_checked) { $watermark_dst_width = $watermark_src_width = imagesx($filter); $watermark_dst_height = $watermark_src_height = imagesy($filter); // if watermark is too large/tall, resize it first if (!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y) || !$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y) { $canvas_width = $this->image_dst_x - abs($this->image_watermark_x); $canvas_height = $this->image_dst_y - abs($this->image_watermark_y); if ($watermark_src_width / $canvas_width > $watermark_src_height / $canvas_height) { $watermark_dst_width = $canvas_width; $watermark_dst_height = intval($watermark_src_height * ($canvas_width / $watermark_src_width)); } else { $watermark_dst_height = $canvas_height; $watermark_dst_width = intval($watermark_src_width * ($canvas_height / $watermark_src_height)); } $this->log .= ' watermark resized from ' . $watermark_src_width . 'x' . $watermark_src_height . ' to ' . $watermark_dst_width . 'x' . $watermark_dst_height . '<br />'; } // determine watermark position $watermark_x = 0; $watermark_y = 0; if (is_numeric($this->image_watermark_x)) { if ($this->image_watermark_x < 0) { $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x; } else { $watermark_x = $this->image_watermark_x; } } else { if (strpos($this->image_watermark_position, 'r') !== false) { $watermark_x = $this->image_dst_x - $watermark_dst_width; } else { if (strpos($this->image_watermark_position, 'l') !== false) { $watermark_x = 0; } else { $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2; } } } if (is_numeric($this->image_watermark_y)) { if ($this->image_watermark_y < 0) { $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y; } else { $watermark_y = $this->image_watermark_y; } } else { if (strpos($this->image_watermark_position, 'b') !== false) { $watermark_y = $this->image_dst_y - $watermark_dst_height; } else { if (strpos($this->image_watermark_position, 't') !== false) { $watermark_y = 0; } else { $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2; } } } imagealphablending($image_dst, true); imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height); } else { $this->error = $this->translate('watermark_invalid'); } } // add text if (!empty($this->image_text)) { $this->log .= '- add text<br />'; // calculate sizes in human readable format $src_size = $this->file_src_size / 1024; $src_size_mb = number_format($src_size / 1024, 1, ".", " "); $src_size_kb = number_format($src_size, 1, ".", " "); $src_size_human = $src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb"; $this->image_text = str_replace(array('[src_name]', '[src_name_body]', '[src_name_ext]', '[src_pathname]', '[src_mime]', '[src_size]', '[src_size_kb]', '[src_size_mb]', '[src_size_human]', '[src_x]', '[src_y]', '[src_pixels]', '[src_type]', '[src_bits]', '[dst_path]', '[dst_name_body]', '[dst_name_ext]', '[dst_name]', '[dst_pathname]', '[dst_x]', '[dst_y]', '[date]', '[time]', '[host]', '[server]', '[ip]', '[gd_version]'), array($this->file_src_name, $this->file_src_name_body, $this->file_src_name_ext, $this->file_src_pathname, $this->file_src_mime, $this->file_src_size, $src_size_kb, $src_size_mb, $src_size_human, $this->image_src_x, $this->image_src_y, $this->image_src_pixels, $this->image_src_type, $this->image_src_bits, $this->file_dst_path, $this->file_dst_name_body, $this->file_dst_name_ext, $this->file_dst_name, $this->file_dst_pathname, $this->image_dst_x, $this->image_dst_y, date('Y-m-d'), date('H:i:s'), isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a', isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a', isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a', $this->gdversion(true)), $this->image_text); if (!is_numeric($this->image_text_padding)) { $this->image_text_padding = 0; } if (!is_numeric($this->image_text_line_spacing)) { $this->image_text_line_spacing = 0; } if (!is_numeric($this->image_text_padding_x)) { $this->image_text_padding_x = $this->image_text_padding; } if (!is_numeric($this->image_text_padding_y)) { $this->image_text_padding_y = $this->image_text_padding; } $this->image_text_position = strtolower($this->image_text_position); $this->image_text_direction = strtolower($this->image_text_direction); $this->image_text_alignment = strtolower($this->image_text_alignment); // if the font is a string, we assume that we might want to load a font if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') { $this->log .= ' try to load font ' . $this->image_text_font . '... '; if ($this->image_text_font = @imageloadfont($this->image_text_font)) { $this->log .= 'success<br />'; } else { $this->log .= 'error<br />'; $this->image_text_font = 5; } } $text = explode("\n", $this->image_text); $char_width = imagefontwidth($this->image_text_font); $char_height = imagefontheight($this->image_text_font); $text_height = 0; $text_width = 0; $line_height = 0; $line_width = 0; foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { $h = $char_width * strlen($v); if ($h > $text_height) { $text_height = $h; } $line_width = $char_height; $text_width += $line_width + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } else { $w = $char_width * strlen($v); if ($w > $text_width) { $text_width = $w; } $line_height = $char_height; $text_height += $line_height + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } } $text_width += 2 * $this->image_text_padding_x; $text_height += 2 * $this->image_text_padding_y; $text_x = 0; $text_y = 0; if (is_numeric($this->image_text_x)) { if ($this->image_text_x < 0) { $text_x = $this->image_dst_x - $text_width + $this->image_text_x; } else { $text_x = $this->image_text_x; } } else { if (strpos($this->image_text_position, 'r') !== false) { $text_x = $this->image_dst_x - $text_width; } else { if (strpos($this->image_text_position, 'l') !== false) { $text_x = 0; } else { $text_x = ($this->image_dst_x - $text_width) / 2; } } } if (is_numeric($this->image_text_y)) { if ($this->image_text_y < 0) { $text_y = $this->image_dst_y - $text_height + $this->image_text_y; } else { $text_y = $this->image_text_y; } } else { if (strpos($this->image_text_position, 'b') !== false) { $text_y = $this->image_dst_y - $text_height; } else { if (strpos($this->image_text_position, 't') !== false) { $text_y = 0; } else { $text_y = ($this->image_dst_y - $text_height) / 2; } } } // add a background, maybe transparent if (!empty($this->image_text_background)) { list($red, $green, $blue) = $this->getcolors($this->image_text_background); if ($gd_version >= 2 && is_numeric($this->image_text_background_percent) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) { $filter = imagecreatetruecolor($text_width, $text_height); $background_color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color); $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent); imagedestroy($filter); } else { $background_color = imagecolorallocate($image_dst, $red, $green, $blue); imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color); } } $text_x += $this->image_text_padding_x; $text_y += $this->image_text_padding_y; $t_width = $text_width - 2 * $this->image_text_padding_x; $t_height = $text_height - 2 * $this->image_text_padding_y; list($red, $green, $blue) = $this->getcolors($this->image_text_color); // add the text, maybe transparent if ($gd_version >= 2 && is_numeric($this->image_text_percent) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) { if ($t_width < 0) { $t_width = 0; } if ($t_height < 0) { $t_height = 0; } $filter = $this->imagecreatenew($t_width, $t_height, false, true); $text_color = imagecolorallocate($filter, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($filter, $this->image_text_font, $this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2), $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent); imagedestroy($filter); } else { $text_color = imageColorAllocate($image_dst, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $text_y + $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } } } // add a reflection if ($this->image_reflection_height) { $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />'; // we decode image_reflection_height, which can be a integer, a string in pixels or percentage $image_reflection_height = $this->image_reflection_height; if (strpos($image_reflection_height, '%') > 0) { $image_reflection_height = $this->image_dst_y * str_replace('%', '', $image_reflection_height / 100); } if (strpos($image_reflection_height, 'px') > 0) { $image_reflection_height = str_replace('px', '', $image_reflection_height); } $image_reflection_height = (int) $image_reflection_height; if ($image_reflection_height > $this->image_dst_y) { $image_reflection_height = $this->image_dst_y; } if (empty($this->image_reflection_opacity)) { $this->image_reflection_opacity = 60; } // create the new destination image $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true); $transparency = $this->image_reflection_opacity; // copy the original image imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)); // we have to make sure the extra bit is the right color, or transparent if ($image_reflection_height + $this->image_reflection_space > 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fill in from the edge of the extra bit imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill); } // copy the reflection for ($y = 0; $y < $image_reflection_height; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { $pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space)); $pixel_o = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0))); $alpha_o = 1 - $pixel_o['alpha'] / 127; $alpha_b = 1 - $pixel_b['alpha'] / 127; $opacity = $alpha_o * $transparency / 100; if ($opacity > 0) { $red = round(($pixel_o['red'] * $opacity + $pixel_b['red'] * $alpha_b) / ($alpha_b + $opacity)); $green = round(($pixel_o['green'] * $opacity + $pixel_b['green'] * $alpha_b) / ($alpha_b + $opacity)); $blue = round(($pixel_o['blue'] * $opacity + $pixel_b['blue'] * $alpha_b) / ($alpha_b + $opacity)); $alpha = $opacity + $alpha_b; if ($alpha > 1) { $alpha = 1; } $alpha = round((1 - $alpha) * 127); $color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha); imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color); } } if ($transparency > 0) { $transparency = $transparency - $this->image_reflection_opacity / $image_reflection_height; } } // copy the resulting image into the destination image $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space; $image_dst = $this->imagetransfer($tmp, $image_dst); } // reduce the JPEG image to a set desired size if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) { // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net $this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />'; // calculate size of each image. 75%, 50%, and 25% quality ob_start(); imagejpeg($image_dst, '', 75); $buffer = ob_get_contents(); ob_end_clean(); $size75 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 50); $buffer = ob_get_contents(); ob_end_clean(); $size50 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 25); $buffer = ob_get_contents(); ob_end_clean(); $size25 = strlen($buffer); // calculate gradient of size reduction by quality $mgrad1 = 25 / ($size50 - $size25); $mgrad2 = 25 / ($size75 - $size50); $mgrad3 = 50 / ($size75 - $size25); $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3; // result of approx. quality factor for expected size $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50); if ($q_factor < 1) { $this->jpeg_quality = 1; } elseif ($q_factor > 100) { $this->jpeg_quality = 100; } else { $this->jpeg_quality = $q_factor; } $this->log .= ' JPEG quality factor set to ' . $this->jpeg_quality . '<br />'; } // converts image from true color, and fix transparency if needed $this->log .= '- converting...<br />'; switch ($this->image_convert) { case 'gif': // if the image is true color, we convert it to a palette if (imageistruecolor($image_dst)) { $this->log .= ' true color to palette<br />'; // creates a black and white mask $mask = array(array()); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $mask[$x][$y] = $pixel['alpha']; } } list($red, $green, $blue) = $this->getcolors($this->image_default_color); // first, we merge the image with the background color, so we know which colors we will have for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($mask[$x][$y] > 0) { // we have some transparency. we combine the color with the default color $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $alpha = $mask[$x][$y] / 127; $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha); $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha); $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha); $color = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']); imagesetpixel($image_dst, $x, $y, $color); } } } // transforms the true color image into palette, with its merged default color if (empty($this->image_background_color)) { imagetruecolortopalette($image_dst, true, 255); $transparency = imagecolorallocate($image_dst, 254, 1, 253); imagecolortransparent($image_dst, $transparency); // make the transparent areas transparent for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { // we test wether we have enough opacity to justify keeping the color if ($mask[$x][$y] > 120) { imagesetpixel($image_dst, $x, $y, $transparency); } } } } unset($mask); } break; case 'jpg': case 'bmp': // if the image doesn't support any transparency, then we merge it with the default color $this->log .= ' fills in transparency with default color<br />'; list($red, $green, $blue) = $this->getcolors($this->image_default_color); $transparency = imagecolorallocate($image_dst, $red, $green, $blue); // make the transaparent areas transparent for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { // we test wether we have some transparency, in which case we will merge the colors if (imageistruecolor($image_dst)) { $rgba = imagecolorat($image_dst, $x, $y); $pixel = array('red' => $rgba >> 16 & 0xff, 'green' => $rgba >> 8 & 0xff, 'blue' => $rgba & 0xff, 'alpha' => ($rgba & 0x7f000000) >> 24); } else { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); } if ($pixel['alpha'] == 127) { // we have full transparency. we make the pixel transparent imagesetpixel($image_dst, $x, $y, $transparency); } else { if ($pixel['alpha'] > 0) { // we have some transparency. we combine the color with the default color $alpha = $pixel['alpha'] / 127; $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha); $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha); $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha); $color = imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']); imagesetpixel($image_dst, $x, $y, $color); } } } } break; default: break; } // outputs image $this->log .= '- saving image...<br />'; switch ($this->image_convert) { case 'jpeg': case 'jpg': if (!$return_mode) { $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality); } else { ob_start(); $result = @imagejpeg($image_dst, '', $this->jpeg_quality); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('JPEG')); } else { $this->log .= ' JPEG image created<br />'; } break; case 'png': imagealphablending($image_dst, false); imagesavealpha($image_dst, true); if (!$return_mode) { $result = @imagepng($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagepng($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('PNG')); } else { $this->log .= ' PNG image created<br />'; } break; case 'gif': if (!$return_mode) { $result = @imagegif($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagegif($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('GIF')); } else { $this->log .= ' GIF image created<br />'; } break; case 'bmp': if (!$return_mode) { $result = $this->imagebmp($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = $this->imagebmp($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('BMP')); } else { $this->log .= ' BMP image created<br />'; } break; default: $this->processed = false; $this->error = $this->translate('no_conversion_type'); } if ($this->processed) { if (is_resource($image_src)) { imagedestroy($image_src); } if (is_resource($image_dst)) { imagedestroy($image_dst); } $this->log .= ' image objects destroyed<br />'; } } } else { $this->log .= '- no image processing wanted<br />'; if (!$return_mode) { // copy the file to its final destination. we don't use move_uploaded_file here // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file if (!copy($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('copy_failed'); } } else { // returns the file, so that its content can be received by the caller $return_content = @file_get_contents($this->file_src_pathname); if ($return_content === FALSE) { $this->processed = false; $this->error = $this->translate('reading_failed'); } } } } if ($this->processed) { $this->log .= '- <b>process OK</b><br />'; } else { $this->log .= '- <b>error</b>: ' . $this->error . '<br />'; } // we reinit all the vars $this->init(); // we may return the image content if ($return_mode) { return $return_content; } }
/** * The actual rendering of the image file. * Basically sets the dimensions, the background color, the traverses the array of GIFBUILDER objects and finally setting the transparent color if defined. * Creates a GDlib resource in $this->im and works on that * Called by gifBuild() * * @return void * @access private * @see gifBuild() */ public function make() { // Get trivial data $XY = $this->XY; // Reset internal properties $this->saveAlphaLayer = false; // Gif-start $this->im = imagecreatetruecolor($XY[0], $XY[1]); $this->w = $XY[0]; $this->h = $XY[1]; // Transparent layer as background if set and requirements are met if (!empty($this->setup['backColor']) && $this->setup['backColor'] === 'transparent' && $this->png_truecolor && !$this->setup['reduceColors'] && (empty($this->setup['format']) || $this->setup['format'] === 'png')) { // Set transparency properties imagesavealpha($this->im, true); // Fill with a transparent background $transparentColor = imagecolorallocatealpha($this->im, 0, 0, 0, 127); imagefill($this->im, 0, 0, $transparentColor); // Set internal properties to keep the transparency over the rendering process $this->saveAlphaLayer = true; // Force PNG in case no format is set $this->setup['format'] = 'png'; $BGcols = array(); } else { // Fill the background with the given color $BGcols = $this->convertColor($this->setup['backColor']); $Bcolor = imagecolorallocate($this->im, $BGcols[0], $BGcols[1], $BGcols[2]); imagefilledrectangle($this->im, 0, 0, $XY[0], $XY[1], $Bcolor); } // Traverse the GIFBUILDER objects an render each one: if (is_array($this->setup)) { $sKeyArray = ArrayUtility::filterAndSortByNumericKeys($this->setup); foreach ($sKeyArray as $theKey) { $theValue = $this->setup[$theKey]; if ((int) $theKey && ($conf = $this->setup[$theKey . '.'])) { // apply stdWrap to all properties, except for TEXT objects // all properties of the TEXT sub-object have already been stdWrap-ped // before in ->checkTextObj() if ($theValue !== 'TEXT') { $isStdWrapped = array(); foreach ($conf as $key => $value) { $parameter = rtrim($key, '.'); if (!$isStdWrapped[$parameter] && isset($conf[$parameter . '.'])) { $conf[$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter . '.']); $isStdWrapped[$parameter] = 1; } } } switch ($theValue) { case 'IMAGE': if ($conf['mask']) { $this->maskImageOntoImage($this->im, $conf, $this->workArea); } else { $this->copyImageOntoImage($this->im, $conf, $this->workArea); } break; case 'TEXT': if (!$conf['hide']) { if (is_array($conf['shadow.'])) { $isStdWrapped = array(); foreach ($conf['shadow.'] as $key => $value) { $parameter = rtrim($key, '.'); if (!$isStdWrapped[$parameter] && isset($conf[$parameter . '.'])) { $conf['shadow.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter . '.']); $isStdWrapped[$parameter] = 1; } } $this->makeShadow($this->im, $conf['shadow.'], $this->workArea, $conf); } if (is_array($conf['emboss.'])) { $isStdWrapped = array(); foreach ($conf['emboss.'] as $key => $value) { $parameter = rtrim($key, '.'); if (!$isStdWrapped[$parameter] && isset($conf[$parameter . '.'])) { $conf['emboss.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter . '.']); $isStdWrapped[$parameter] = 1; } } $this->makeEmboss($this->im, $conf['emboss.'], $this->workArea, $conf); } if (is_array($conf['outline.'])) { $isStdWrapped = array(); foreach ($conf['outline.'] as $key => $value) { $parameter = rtrim($key, '.'); if (!$isStdWrapped[$parameter] && isset($conf[$parameter . '.'])) { $conf['outline.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter . '.']); $isStdWrapped[$parameter] = 1; } } $this->makeOutline($this->im, $conf['outline.'], $this->workArea, $conf); } $conf['imgMap'] = 1; $this->makeText($this->im, $conf, $this->workArea); } break; case 'OUTLINE': if ($this->setup[$conf['textObjNum']] == 'TEXT' && ($txtConf = $this->checkTextObj($this->setup[$conf['textObjNum'] . '.']))) { $this->makeOutline($this->im, $conf, $this->workArea, $txtConf); } break; case 'EMBOSS': if ($this->setup[$conf['textObjNum']] == 'TEXT' && ($txtConf = $this->checkTextObj($this->setup[$conf['textObjNum'] . '.']))) { $this->makeEmboss($this->im, $conf, $this->workArea, $txtConf); } break; case 'SHADOW': if ($this->setup[$conf['textObjNum']] == 'TEXT' && ($txtConf = $this->checkTextObj($this->setup[$conf['textObjNum'] . '.']))) { $this->makeShadow($this->im, $conf, $this->workArea, $txtConf); } break; case 'BOX': $this->makeBox($this->im, $conf, $this->workArea); break; case 'EFFECT': $this->makeEffect($this->im, $conf); break; case 'ADJUST': $this->adjust($this->im, $conf); break; case 'CROP': $this->crop($this->im, $conf); break; case 'SCALE': $this->scale($this->im, $conf); break; case 'WORKAREA': if ($conf['set']) { // this sets the workArea $this->setWorkArea($conf['set']); } if (isset($conf['clear'])) { // This sets the current to the default; $this->workArea = $this->defaultWorkArea; } break; case 'ELLIPSE': $this->makeEllipse($this->im, $conf, $this->workArea); break; } } } } // Preserve alpha transparency if (!$this->saveAlphaLayer) { if ($this->setup['transparentBackground']) { // Auto transparent background is set $Bcolor = imagecolorclosest($this->im, $BGcols[0], $BGcols[1], $BGcols[2]); imagecolortransparent($this->im, $Bcolor); } elseif (is_array($this->setup['transparentColor_array'])) { // Multiple transparent colors are set. This is done via the trick that all transparent colors get // converted to one color and then this one gets set as transparent as png/gif can just have one // transparent color. $Tcolor = $this->unifyColors($this->im, $this->setup['transparentColor_array'], (int) $this->setup['transparentColor.']['closest']); if ($Tcolor >= 0) { imagecolortransparent($this->im, $Tcolor); } } } }