function applyFilter(Canvas $canvas) { $himage = $canvas->getImage(); $iw = imagesx($himage); $ih = imagesy($himage); switch ($this->placement) { case WatermarkImageFilter::POS_RELATIVE: $dx = $this->x >= 0 ? $this->x : $iw - $this->width + $this->x + 1; $dy = $this->y >= 0 ? $this->y : $ih - $this->height + $this->y + 1; break; case WatermarkImageFilter::POS_ABSOLUTE: $dx = $this->x; $dy = $this->y; break; case WatermarkImageFilter::POS_CENTERED: $dx = $iw / 2 + $this->x; $dy = $ih / 2 + $this->y; break; } imagecopymerge_alpha($himage, $this->hwatermark, $dx, $dy, 0, 0, $this->width, $this->height, 0); }
function drawField($team) { $field = imagecreatefromjpeg("img/field.jpg"); foreach ($team->players as $player) { $img = drawPlayer($player->number, $player->name); $width = imagesx($img); $height = imagesy($img); $posX = $player->x - ($width - 26) / 2; $posY = $player->y - 10; imageAlphaBlending($field, true); // копировать сюда будем вместе с настройками imageSaveAlpha($field, true); // сохраняем imageCopy($field, $img, $posX, $posY, 0, 0, $width, $height); //копируем картинку с формой в пустой бокс } $copyright = drawCaption("http://www.ezheloko.ru/tactic", 12, 0); imagecopymerge_alpha($field, $copyright, 240, imagesY($field) - 25, 0, 0, imagesX($copyright), imagesY($copyright), 30); $name = generateName(); $name = "formations/" . $name . ".png"; imagePng($field, $name); return $name; }
protected function processImageAndWriteToCache($localImage) { $sData = getimagesize($localImage); $origType = $sData[2]; $mimeType = $sData['mime']; $this->debug(3, "Mime type of image is {$mimeType}"); if (!preg_match('/^image\\/(?:gif|jpg|jpeg|png)$/i', $mimeType)) { return $this->error("The image being resized is not a valid gif, jpg or png."); } if (!function_exists('imagecreatetruecolor')) { return $this->error('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library'); } if (function_exists('imagefilter') && defined('IMG_FILTER_NEGATE')) { $imageFilters = array(1 => array(IMG_FILTER_NEGATE, 0), 2 => array(IMG_FILTER_GRAYSCALE, 0), 3 => array(IMG_FILTER_BRIGHTNESS, 1), 4 => array(IMG_FILTER_CONTRAST, 1), 5 => array(IMG_FILTER_COLORIZE, 4), 6 => array(IMG_FILTER_EDGEDETECT, 0), 7 => array(IMG_FILTER_EMBOSS, 0), 8 => array(IMG_FILTER_GAUSSIAN_BLUR, 0), 9 => array(IMG_FILTER_SELECTIVE_BLUR, 0), 10 => array(IMG_FILTER_MEAN_REMOVAL, 0), 11 => array(IMG_FILTER_SMOOTH, 0)); } // get standard input properties $new_width = (int) abs($this->param('w', 0)); $new_height = (int) abs($this->param('h', 0)); $zoom_crop = (int) $this->param('zc', DEFAULT_ZC); $quality = (int) abs($this->param('q', DEFAULT_Q)); $align = $this->cropTop ? 't' : $this->param('a', 'c'); $filters = $this->param('f', DEFAULT_F); $sharpen = (bool) $this->param('s', DEFAULT_S); $canvas_color = $this->param('cc', DEFAULT_CC); //$watermark_image_details = $this->param('wmi', 0); $canvas_trans = (bool) $this->param('ct', '1'); // set default width and height if neither are set already if ($new_width == 0 && $new_height == 0) { $new_width = 100; $new_height = 100; } // ensure size limits can not be abused $new_width = min($new_width, MAX_WIDTH); $new_height = min($new_height, MAX_HEIGHT); // set memory limit to be able to have enough space to resize larger images $this->setMemoryLimit(); // open the existing image $image = $this->openImage($mimeType, $localImage); if ($image === false) { return $this->error('Unable to open image.'); } // Get original width and height $width = imagesx($image); $height = imagesy($image); $origin_x = 0; $origin_y = 0; // generate new w/h if not provided if ($new_width && !$new_height) { $new_height = floor($height * ($new_width / $width)); } else { if ($new_height && !$new_width) { $new_width = floor($width * ($new_height / $height)); } } // scale down and add borders if ($zoom_crop == 3) { $final_height = $height * ($new_width / $width); if ($final_height > $new_height) { $new_width = $width * ($new_height / $height); } else { $new_height = $final_height; } } // create a new true color image $canvas = imagecreatetruecolor($new_width, $new_height); imagealphablending($canvas, false); if (strlen($canvas_color) == 3) { //if is 3-char notation, edit string into 6-char notation $canvas_color = str_repeat(substr($canvas_color, 0, 1), 2) . str_repeat(substr($canvas_color, 1, 1), 2) . str_repeat(substr($canvas_color, 2, 1), 2); } else { if (strlen($canvas_color) != 6) { $canvas_color = DEFAULT_CC; // on error return default canvas color } } $canvas_color_R = hexdec(substr($canvas_color, 0, 2)); $canvas_color_G = hexdec(substr($canvas_color, 2, 2)); $canvas_color_B = hexdec(substr($canvas_color, 4, 2)); // Create a new transparent color for image // If is a png and PNG_IS_TRANSPARENT is false then remove the alpha transparency // (and if is set a canvas color show it in the background) if (preg_match('/^image\\/png$/i', $mimeType) && !PNG_IS_TRANSPARENT && $canvas_trans) { $color = imagecolorallocatealpha($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 127); } else { $color = imagecolorallocatealpha($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 0); } // Completely fill the background of the new image with allocated color. imagefill($canvas, 0, 0, $color); // scale down and add borders if ($zoom_crop == 2) { $final_height = $height * ($new_width / $width); if ($final_height > $new_height) { $origin_x = $new_width / 2; $new_width = $width * ($new_height / $height); $origin_x = round($origin_x - $new_width / 2); } else { $origin_y = $new_height / 2; $new_height = $final_height; $origin_y = round($origin_y - $new_height / 2); } } // Restore transparency blending imagesavealpha($canvas, true); if ($zoom_crop > 0) { $src_x = $src_y = 0; $src_w = $width; $src_h = $height; $cmp_x = $width / $new_width; $cmp_y = $height / $new_height; // calculate x or y coordinate and width or height of source if ($cmp_x > $cmp_y) { $src_w = round($width / $cmp_x * $cmp_y); $src_x = round(($width - $width / $cmp_x * $cmp_y) / 2); } else { if ($cmp_y > $cmp_x) { $src_h = round($height / $cmp_y * $cmp_x); $src_y = round(($height - $height / $cmp_y * $cmp_x) / 2); } } // positional cropping! if ($align) { if (strpos($align, 't') !== false) { $src_y = 0; } if (strpos($align, 'b') !== false) { $src_y = $height - $src_h; } if (strpos($align, 'l') !== false) { $src_x = 0; } if (strpos($align, 'r') !== false) { $src_x = $width - $src_w; } } imagecopyresampled($canvas, $image, $origin_x, $origin_y, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h); } else { // copy and resize part of an image with resampling imagecopyresampled($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } if ($filters != '' && function_exists('imagefilter') && defined('IMG_FILTER_NEGATE')) { // apply filters to image $filterList = explode('|', $filters); foreach ($filterList as $fl) { $filterSettings = explode(',', $fl); if (isset($imageFilters[$filterSettings[0]])) { for ($i = 0; $i < 4; $i++) { if (!isset($filterSettings[$i])) { $filterSettings[$i] = null; } else { $filterSettings[$i] = (int) $filterSettings[$i]; } } switch ($imageFilters[$filterSettings[0]][1]) { case 1: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1]); break; case 2: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2]); break; case 3: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3]); break; case 4: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3], $filterSettings[4]); break; default: imagefilter($canvas, $imageFilters[$filterSettings[0]][0]); break; } } } } // watermark image if (WATERMARK_IMAGE_ENABLED && preg_match('/^image\\/(?:jpg|jpeg)$/i', $mimeType)) { $watermark_position = 'c'; $watermark_opacity = 40; $watermark_image = null; if ($new_width > 200 && $new_height > 160) { $watermark_image = dirname($this->localImage) . DIRECTORY_SEPARATOR . 'marcadagua.png'; $watermark_margin = 15; } elseif ($new_width > 70 && $new_height > 70) { $watermark_image = dirname($this->localImage) . DIRECTORY_SEPARATOR . 'marcadagua-small.png'; $watermark_margin = 5; } //if ($watermark_image_details && WATERMARK_IMAGE_ENABLED) { if (!is_null($watermark_image) && file_exists($watermark_image)) { //@list( $watermark_image, $watermark_position, $watermark_opacity ) = explode('|', $watermark_image_details); function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) { $cut = imagecreatetruecolor($src_w, $src_h); imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); } $watermark_extension = pathinfo($watermark_image, PATHINFO_EXTENSION); if ($watermark_extension != 'png') { die('Watermark image must be ".png" please convert your watermark image to png.'); } //$watermark_image = dirname(__FILE__) . DIRECTORY_SEPARATOR . $watermark_image; //$watermark_image = LOCAL_FILE_BASE_DIRECTORY . DIRECTORY_SEPARATOR . $watermark_image; if (!file_exists($watermark_image)) { die('Can not found watermark image.' . $watermark_image); } $watermark_image = imagecreatefrompng($watermark_image); $watermark_width = imagesx($watermark_image); $watermark_height = imagesy($watermark_image); /* $maxwm_width = floor($new_width / 2); $maxwm_height = floor($new_height / 3); if ($watermark_height > $maxwm_height) { $watermark_height = $maxwm_height; $watermark_width = floor(($new_width * $watermark_height) / $new_height); } if ($watermark_width > $maxwm_width) { $watermark_width = $maxwm_width; $watermark_height = floor(($new_height * $watermark_width) / $new_width); } */ $watermark_positionx = $new_width - ($watermark_width + $watermark_margin); $watermark_positiony = $new_height - ($watermark_height + $watermark_margin); /* if(strtolower($watermark_position) == 'c'){ $watermark_positionx = $new_width/2 - $watermark_width/2; $watermark_positiony = $new_height/2 - $watermark_height/2; }else{ @list($watermark_positionx, $watermark_positiony ) = explode('x', $watermark_position); if(is_numeric($watermark_positionx) === false or is_numeric($watermark_positiony) === false){ die('Watermark position could be \'C\' (center) or \'25x25\''); } } */ $watermark_opacity = intval($watermark_opacity); if ($watermark_opacity === 0) { $watermark_opacity = 80; } imagealphablending($canvas, true); imagecopymerge_alpha($canvas, $watermark_image, $watermark_positionx, $watermark_positiony, 0, 0, $watermark_width, $watermark_height, $watermark_opacity); imagealphablending($canvas, false); imagesavealpha($canvas, true); } } // sharpen image if ($sharpen && function_exists('imageconvolution')) { $sharpenMatrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)); $divisor = 8; $offset = 0; imageconvolution($canvas, $sharpenMatrix, $divisor, $offset); } //Straight from Wordpress core code. Reduces filesize by up to 70% for PNG's if ((IMAGETYPE_PNG == $origType || IMAGETYPE_GIF == $origType) && function_exists('imageistruecolor') && !imageistruecolor($image) && imagecolortransparent($image) > 0) { imagetruecolortopalette($canvas, false, imagecolorstotal($image)); } $imgType = ""; $tempfile = tempnam($this->cacheDirectory, 'timthumb_tmpimg_'); if (preg_match('/^image\\/(?:jpg|jpeg)$/i', $mimeType)) { $imgType = 'jpg'; imagejpeg($canvas, $tempfile, $quality); } else { if (preg_match('/^image\\/png$/i', $mimeType)) { $imgType = 'png'; imagepng($canvas, $tempfile, floor($quality * 0.09)); } else { if (preg_match('/^image\\/gif$/i', $mimeType)) { $imgType = 'gif'; imagegif($canvas, $tempfile); } else { return $this->sanityFail("Could not match mime type after verifying it previously."); } } } if ($imgType == 'png' && OPTIPNG_ENABLED && OPTIPNG_PATH && @is_file(OPTIPNG_PATH)) { $exec = OPTIPNG_PATH; $this->debug(3, "optipng'ing {$tempfile}"); $presize = filesize($tempfile); $out = `{$exec} -o1 {$tempfile}`; //you can use up to -o7 but it really slows things down clearstatcache(); $aftersize = filesize($tempfile); $sizeDrop = $presize - $aftersize; if ($sizeDrop > 0) { $this->debug(1, "optipng reduced size by {$sizeDrop}"); } else { if ($sizeDrop < 0) { $this->debug(1, "optipng increased size! Difference was: {$sizeDrop}"); } else { $this->debug(1, "optipng did not change image size."); } } } else { if ($imgType == 'png' && PNGCRUSH_ENABLED && PNGCRUSH_PATH && @is_file(PNGCRUSH_PATH)) { $exec = PNGCRUSH_PATH; $tempfile2 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_'); $this->debug(3, "pngcrush'ing {$tempfile} to {$tempfile2}"); $out = `{$exec} {$tempfile} {$tempfile2}`; $todel = ""; if (is_file($tempfile2)) { $sizeDrop = filesize($tempfile) - filesize($tempfile2); if ($sizeDrop > 0) { $this->debug(1, "pngcrush was succesful and gave a {$sizeDrop} byte size reduction"); $todel = $tempfile; $tempfile = $tempfile2; } else { $this->debug(1, "pngcrush did not reduce file size. Difference was {$sizeDrop} bytes."); $todel = $tempfile2; } } else { $this->debug(3, "pngcrush failed with output: {$out}"); $todel = $tempfile2; } @unlink($todel); } } $this->debug(3, "Rewriting image with security header."); $tempfile4 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_'); $context = stream_context_create(); $fp = fopen($tempfile, 'r', 0, $context); file_put_contents($tempfile4, $this->filePrependSecurityBlock . $imgType . ' ?' . '>'); //6 extra bytes, first 3 being image type file_put_contents($tempfile4, $fp, FILE_APPEND); fclose($fp); @unlink($tempfile); $this->debug(3, "Locking and replacing cache file."); $lockFile = $this->cachefile . '.lock'; $fh = fopen($lockFile, 'w'); if (!$fh) { return $this->error("Could not open the lockfile for writing an image."); } if (flock($fh, LOCK_EX)) { @unlink($this->cachefile); //rename generally overwrites, but doing this in case of platform specific quirks. File might not exist yet. rename($tempfile4, $this->cachefile); flock($fh, LOCK_UN); fclose($fh); @unlink($lockFile); } else { fclose($fh); @unlink($lockFile); @unlink($tempfile4); return $this->error("Could not get a lock for writing."); } $this->debug(3, "Done image replace with security header. Cleaning up and running cleanCache()"); imagedestroy($canvas); imagedestroy($image); return true; }
function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000",$transparent=100,$quality=100) { if(substr($waterImage,0,1) == '/'){ $waterImage = substr($waterImage,1); } $isWaterImage = FALSE; $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。"; if(!empty($waterImage) &&file_exists($waterImage)) { $isWaterImage = TRUE; $water_info = getimagesize($waterImage); $water_w = $water_info[0]; $water_h = $water_info[1]; switch($water_info[2]) { case 1: $water_im = imagecreatefromgif($waterImage); break; case 2: $water_im = imagecreatefromjpeg($waterImage); break; case 3: $water_im = imagecreatefrompng($waterImage); break; default:die($formatMsg); } } if(!empty($groundImage) &&file_exists($groundImage)) { $ground_info = getimagesize($groundImage); $ground_w = $ground_info[0]; $ground_h = $ground_info[1]; switch($ground_info[2]) { case 1: $ground_im = imagecreatefromgif($groundImage); break; case 2: $ground_im = imagecreatefromjpeg($groundImage); break; case 3: $ground_im = imagecreatefrompng($groundImage); break; default:die($formatMsg); } }else { return; } if($isWaterImage) { $w = $water_w; $h = $water_h; $label = "图片的"; }else { $temp = @imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterText); $w = $temp[2] -$temp[6]; $h = $temp[3] -$temp[7]; unset($temp); $label = "文字区域"; } if( ($ground_w<=$w) ||($ground_h<=$h) ) { return; } switch($waterPos) { case 0: $posX = rand(0,($ground_w -$w)); $posY = rand(0,($ground_h -$h)); break; case 1: $posX = 0; $posY = 0; break; case 2: $posX = ($ground_w -$w) / 2; $posY = 0; break; case 3: $posX = $ground_w -$w; $posY = 0; break; case 4: $posX = 0; $posY = ($ground_h -$h) / 2; break; case 5: $posX = ($ground_w -$w) / 2; $posY = ($ground_h -$h) / 2; break; case 6: $posX = $ground_w -$w; $posY = ($ground_h -$h) / 2; break; case 7: $posX = 0; $posY = $ground_h -$h; break; case 8: $posX = ($ground_w -$w) / 2; $posY = $ground_h -$h; break; case 9: $posX = $ground_w -$w; $posY = $ground_h -$h; break; default: $posX = rand(0,($ground_w -$w)); $posY = rand(0,($ground_h -$h)); break; } if($posX<20) $posX=20; if($posY<20) $posY=20; if($posX>$ground_w -$w-20) $posX=$ground_w -$w-20; if($posY>$ground_h -$h-20) $posY=$ground_h -$h-20; imagesavealpha($ground_im,true); imagealphablending($ground_im,false); imagesavealpha($water_im,true); imagealphablending($water_im,false); if($isWaterImage) { imagecopymerge_alpha($ground_im,$water_im,$posX,$posY,0,0,$water_w,$water_h,$transparent); }else { if( !empty($textColor) &&(strlen($textColor)==7) ) { $R = hexdec(substr($textColor,1,2)); $G = hexdec(substr($textColor,3,2)); $B = hexdec(substr($textColor,5)); }else { die("水印文字颜色格式不正确!"); } imagestring ( $ground_im,$textFont,$posX,$posY,$waterText,imagecolorallocate($ground_im,$R,$G,$B)); } @unlink($groundImage); switch($ground_info[2]) { case 1: imagegif($ground_im,$groundImage); break; case 2: imagejpeg($ground_im,$groundImage,$quality); break; case 3: imagepng($ground_im,$groundImage); break; default:die('未知格式'); } if(isset($water_info)) unset($water_info); if(isset($water_im)) imagedestroy($water_im); unset($ground_info); imagedestroy($ground_im); }
/** * @param array $options * 'watermark' => waImage|string $watermark * 'opacity' => float|int 0..1 * 'align' => self::ALIGN_* const * 'font_file' => null|string If null - will be used some default font. Note: use when watermark option is text * 'font_size' => float Size of font. Note: use when watermark option is text * 'font_color' => string Hex-formatted of color (without #). Note: use when watermark option is text * 'text_orientation' => self::ORIENTATION_* const. Note: use when watermark option is text * @return mixed */ protected function _watermark($options) { // export options to php-vars foreach ($options as $name => $value) { ${$name} = $value; } $opacity = min(max($opacity, 0), 1); imagealphablending($this->image, true); if ($watermark instanceof waImage) { $width = $watermark->width; $height = $watermark->height; $offset = $this->calcWatermarkOffset($width, $height, $align); $type = $watermark->type; $watermark = $this->createGDImageResourse($watermark->file, $type); imagecopymerge_alpha($this->image, $watermark, $offset[0], $offset[1], 0, 0, $width, $height, $opacity * 100); imagedestroy($watermark); } else { $text = (string) $watermark; if (!$text) { return; } $font_color = array('r' => '0x' . substr($font_color, 0, 2), 'g' => '0x' . substr($font_color, 2, 2), 'b' => '0x' . substr($font_color, 4, 2), 'a' => floor((1 - $opacity) * 127)); if ($font_file && file_exists($font_file)) { $metrics = imagettfbbox($font_size, 0, $font_file, $text); if ($metrics) { $width = $metrics[2] - $metrics[0]; $height = $metrics[1] - $metrics[7]; if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align); $offset = $this->watermarkOffsetFix($offset, $width, $height, $text_orientation); $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); imagettftext($this->image, $font_size, $text_orientation == self::ORIENTATION_VERTICAL ? 90 : 0, $offset[0], $offset[1], $color, $font_file, $text); } else { throw new waException(_ws("Can't read font file {$font_file}")); } } else { $font = floor(5 * $font_size / 12); if ($font < 1) { $font = 1; } else { if ($font > 5) { $font = 5; } } $width = imagefontwidth($font) * strlen($text); $height = imagefontheight($font); if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align); if ($text_orientation == self::ORIENTATION_VERTICAL) { imagestring_rotate($this->image, $font, 90, $offset[0], $offset[1], $text, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); } else { $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); imagestring($this->image, $font, $offset[0], $offset[1], $text, $color); } } } }
/*反転する*/ $flip = $d['flip']; if ($flip === "both") { imageflip($tempimage, IMG_FLIP_BOTH); } else { if ($flip === "horizontal") { imageflip($tempimage, IMG_FLIP_HORIZONTAL); } else { if ($flip === "vertical") { imageflip($tempimage, IMG_FLIP_VERTICAL); } } } /*画像のコピー、メモリのクリア*/ // imagecopy($im, $tempimage, $d['position'][0], $d['position'][1], 0, 0, $fixheight, $fixwidth); imagecopymerge_alpha($im, $tempimage, $d['position'][0], $d['position'][1], 0, 0, $fixheight, $fixwidth, $d['opacity']); imagedestroy($tempimage); } else { if ($type === "text") { /*画像を生成する*/ $tempimage = imagecreatefrompng($d['filename']); /*画像のコピー、メモリのクリア*/ imagecopy($im, $tempimage, $d['position'][0], $d['position'][1], 0, 0, $d['size'][0], $d['size'][1]); imagedestroy($tempimage); } else { if ($type === "draw") { /*画像を生成する*/ $tempimage = imagecreatefrompng($d['filename']); /*画像のコピー、メモリのクリア*/ imagecopy($im, $tempimage, $d['position'][0], $d['position'][1], 0, 0, $d['size'][0], $d['size'][1]); imagedestroy($tempimage);
/** * @brief Draw the canvas onto another canvas. * * @param Canvas $dest The destination canvas * @param integer $x The left coordinate of the destination canvas * @param integer $y The top coordinate of the destination canvas * @param integer $width The width of the drawing * @param integer $height The height of the drawing */ function draw(Canvas $dest, $x = null, $y = null, $width = null, $height = null, $alpha = false) { $this->checkMeta(); $dstimage = $dest->getImage(); if (!$x) { $x = 0; } if (!$y) { $y = 0; } if (!$width) { $width = $this->width; } if (!$height) { $height = $this->height; } if (!$alpha) { imagecopy($dstimage, $this->himage, $x, $y, 0, 0, $width, $height); } else { imagecopymerge_alpha($dstimage, $this->himage, $x, $y, 0, 0, $width, $height, 0); } }
echo json_encode(array("stat" => "error", "msg" => "Invalid image data")); die; } unset($imageData); if (file_exists("boards/0.png")) { $baseImage = imagecreatefrompng("boards/0.png"); } else { $baseImage = imagecreatetruecolor(500, 500); $color = imagecolorallocate($baseImage, 255, 255, 255); imagefilledrectangle($baseImage, 0, 0, 500, 500, $color); } if ($baseImage === false) { echo json_encode(array("stat" => "error", "msg" => "Unable to load base image")); die; } imagealphablending($baseImage, true); imagesavealpha($baseImage, true); imagealphablending($newImage, true); imagesavealpha($newImage, true); if (!imagecopymerge_alpha($baseImage, $newImage, 0, 0, 0, 0, 500, 500, 100)) { echo json_encode(array("stat" => "error", "msg" => "Unable to merge images")); die; } if (!imagepng($baseImage, "boards/0.png")) { echo json_encode(array("stat" => "error", "msg" => "Unable to save image")); die; } imagedestroy($newImage); imagedestroy($baseImage); echo json_encode(array("stat" => "ok")); }
} imagestring($image, 2, 15, 22, 'Position ', $font_color); if (is_numeric($rank)) { imagestring($image, 3, 70, 22, number_format($rank), $font_color); $start_pos_x = 71 + imagefontwidth(3) * strlen(number_format($rank)) + 7; } else { imagestring($image, 3, 70, 22, $rank, $font_color); $start_pos_x = 71 + imagefontwidth(3) * strlen($rank) + 7; } $ranktext = 'of ' . $pl_count['count'] . ' players with ' . $playerdata['skill'] . ' ('; imagestring($image, 2, $start_pos_x, 22, $ranktext, $font_color); $start_pos_x += imagefontwidth(2) * strlen($ranktext); if ($trend) { imagecopy($image, $trend, $start_pos_x, 26, 0, 0, 7, 7); $start_header_name += 22; imagedestroy($trend); $start_pos_x += 10; } imagestring($image, 2, $start_pos_x, 22, $skill_change . ') points', $font_color); imagestring($image, 2, 15, 34, 'Frags: ' . $playerdata['kills'] . ' kills : ' . $playerdata['deaths'] . ' deaths (' . $playerdata['kpd'] . '), ' . $playerdata['headshots'] . ' headshots (' . $playerdata['hpk'] . '%)', $font_color); imagestring($image, 2, 15, 45, 'Activity: ' . $playerdata['lastevent'] . ' (' . $playerdata['activity'] . '%), Time: ' . $con_time . ' hours', $font_color); imagestring($image, 2, 15, 56, 'Statistics: ', $font_color); imagestring($image, 2, 85, 56, $g_options['siteurl'], $link_color); $watermark = imagecreatefrompng(IMAGE_PATH . '/watermark.png'); imagecopymerge_alpha($image, $watermark, 334, 58, 0, 0, 60, 12, 50); $mod_date = date('D, d M Y H:i:s \\G\\M\\T', time()); Header('Last-Modified:' . $mod_date); imagepng($image); imagedestroy($image); imagedestroy($watermark); }
function imagefusion($src_a, $src_b, $out) { $a = $src_a; $b = $src_b; $a = imagecreatefrompng($a); $b = imagecreatefrompng($b); imagecopymerge_alpha($a, $b, 0, 0, 0, 0, imagesx($b), imagesy($b), 100); imagesavealpha($a, true); imagepng($a, $out); imagedestroy($a); imagedestroy($b); }
function resize($original, $w, $h, $crop, $cropratio = 0, $wm = 0, $catid = '') { require dirname(__FILE__) . DS . 'config.datsogallery.php'; jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); $cro = str_replace(':', 'x', $cropratio); $dirname = "datsogallery_catid-{$catid}_{$w}x{$h}_{$cro}"; $types = array(1 => 'gif', 'jpeg', 'png'); if (!JFolder::exists(JPATH_SITE . DS . 'cache' . DS . $dirname)) { JFolder::create(JPATH_SITE . DS . 'cache' . DS . $dirname); $content = '<html><body bgcolor="#ffffff"></body></html>'; JFile::write(JPATH_SITE . DS . 'cache' . DS . $dirname . DS . 'index.html', $content); } if (!JFile::exists(JPATH_SITE . $ad_pathoriginals . DS . 'blank.jpg')) { JFile::copy(JPATH_COMPONENT_SITE . DS . 'images' . DS . 'blank.jpg', JPATH_SITE . $ad_pathoriginals . DS . 'blank.jpg'); } $path = JPath::clean(JPATH_SITE . $ad_pathoriginals . DS . $original); dgFileCheck($path); if (!file_exists($path) || is_dir($path) || !($size = getimagesize($path))) { return; } $width = $size[0]; $height = $size[1]; $mw = $w; $mh = $h; $x = 0; $y = 0; if ($crop == '1') { $cr = explode(':', $cropratio); if (count($cr) == 2) { $rc = $width / $height; $crc = (double) $cr[0] / (double) $cr[1]; if ($rc < $crc) { $oh = $height; $height = $width / $crc; $y = ($oh - $height) / 2; } else { if ($rc > $crc) { $ow = $width; $width = $height * $crc; $x = ($ow - $width) / 2; } } } } $xr = $mw / $width; $yr = $mh / $height; if ($xr * $height < $mh) { $th = ceil($xr * $height); $tw = $mw; } else { $tw = ceil($yr * $width); $th = $mh; } $relfile = JURI::root(true) . '/cache/' . $dirname . '/' . basename($original); $cachefile = JPATH_SITE . DS . 'cache' . DS . $dirname . DS . basename($original); if (file_exists($cachefile)) { $cachesize = getimagesize($cachefile); $cached = $cachesize[0] == $tw && $cachesize[1] == $th; if (filemtime($cachefile) < filemtime($path)) { $cached = false; } } else { $cached = false; } if (!$cached && ($size[0] >= $w || $size[1] >= $h)) { $resize = $size[0] >= $w || $size[1] >= $h; } elseif (!$cached && ($size[0] <= $w || $size[1] <= $h)) { $resize = true; } else { $resize = false; } if ($resize) { @increasememory($original); $image = call_user_func('imagecreatefrom' . $types[$size[2]], $path); $temp = $size[0] <= $w || $size[1] <= $h ? imagecreatetruecolor($width, $height) : imagecreatetruecolor($tw, $th); if (function_exists('imagecreatetruecolor') && $temp) { if (in_array($types[$size[2]], array('gif', 'png'))) { $color = 'F5F5F5'; $background = imagecolorallocate($temp, hexdec($color[0] . $color[1]), hexdec($color[2] . $color[3]), hexdec($color[4] . $color[5])); imagefillalpha($temp, $background); } if ($resize && ($size[0] <= $w || $size[1] <= $h)) { if (in_array($types[$size[2]], array('gif', 'png'))) { imagecopyresampled($temp, $image, 0, 0, 0, 0, $size[0], $size[1], $width, $height); } else { fastimagecopyresampled($temp, $image, 0, 0, 0, 0, $size[0], $size[1], $width, $height); } } else { if (in_array($types[$size[2]], array('gif', 'png'))) { imagecopyresampled($temp, $image, 0, 0, $x, $y, $tw, $th, $width, $height); } else { fastimagecopyresampled($temp, $image, 0, 0, $x, $y, $tw, $th, $width, $height); } } imagedestroy($image); $sharpness = findsharp($width, $tw); $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1)); $divisor = $sharpness; $offset = 0; imageconvolution($temp, $sharpenMatrix, $divisor, $offset); if ($wm) { $watermarkPNGFile = JPATH_SITE . DS . 'components' . DS . 'com_datsogallery' . DS . 'images' . DS . 'watermark.png'; $watermarkMargin = 5; $watermark = imagecreatefrompng($watermarkPNGFile); $watermarkWidth = imagesx($watermark); $watermarkHeight = imagesy($watermark); $imageWidth = imagesx($temp); $imageHeight = imagesy($temp); switch ($ad_wmpos) { case 'topleft': $placeWatermarkX = $watermarkMargin; $placeWatermarkY = $watermarkMargin; break; case 'topright': $placeWatermarkX = $imageWidth - $watermarkWidth - $watermarkMargin; $placeWatermarkY = $watermarkMargin; break; case 'bottomleft': $placeWatermarkX = $watermarkMargin; $placeWatermarkY = $imageHeight - $watermarkHeight - $watermarkMargin; break; case 'bottomright': $placeWatermarkX = $imageWidth - $watermarkWidth - $watermarkMargin; $placeWatermarkY = $imageHeight - $watermarkHeight - $watermarkMargin; break; case 'center': $placeWatermarkX = ($imageWidth - $watermarkWidth) / 2 - $watermarkMargin; $placeWatermarkY = ($imageHeight - $watermarkHeight) / 2 - $watermarkMargin; break; } imagecopymerge_alpha($temp, $watermark, $placeWatermarkX, $placeWatermarkY, 0, 0, $watermarkWidth, $watermarkHeight, 0); } } if ($types[$size[2]] == 'jpeg') { dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname, 0777); call_user_func('image' . $types[$size[2]], $temp, $cachefile, $ad_thumbquality); dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname); } else { dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname, 0777); call_user_func('image' . $types[$size[2]], $temp, $cachefile); dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname); } imagedestroy($temp); } return $relfile; exit; }
/** * process * Does the thing * * @param string * @return mixed */ public function process() { if ($this->valid_data()) { $this->extension = $this->get_true_extension($this->mime); if ($this->extension == 'bmp') { require_once 'class.imageconvert.php'; $this->ImageConvert = new ImageConvert($this->working, $this->extension, $this->img_upload_path . 'temp_' . generateRandomString(256)); unset($this->working); unset($this->extension); $this->working = $this->ImageConvert->out; $this->extension = 'png'; } switch ($this->storage) { case 'direct': $this->img_upload_path = __CHV_PATH_IMAGES__; break; case 'datefolder': case 'datefolders': default: // Sets the date folder YYYY/MM/DD $datefolder = $this->img_upload_path . date('Y/m/d/'); $old_umask = umask(0); if (!file_exists($datefolder) && !@mkdir($datefolder, 0755, true)) { $this->error = "Unable to create upload folder"; return false; } umask($old_umask); $this->img_upload_path = $datefolder; break; } $image_filename = $this->nameFile($this->img_upload_path, $this->extension, chevereto_config('file_naming'), $this->original_file_name); // Prepare and formats the temp image $formated_temp = $this->working . '.' . $this->extension; rename($this->working, $formated_temp); unset($this->working); $this->working = $formated_temp; // Call the resize class require_once 'class.imageresize.php'; // Thumb $thumb_filename = str_replace($this->extension, 'th.' . $this->extension, $image_filename); $this->ThumbResize = new ImageResize($this->working, $thumb_filename, $this->thumb_width, $this->thumb_height, true); // Fixed width but fluid height? Replace the line above with this: // $this->ThumbResize = new ImageResize($this->working, $thumb_filename, $this->thumb_width); if (check_value($this->ThumbResize->error)) { $this->error = $this->ThumbResize->error . " (thumb)"; return false; } // Resize? if (check_value($this->resize_width)) { $this->ImageResize = new ImageResize($this->working, $this->working, $this->resize_width); if (check_value($this->ImageResize->error)) { $this->error = $this->ImageResize->error; return false; } } if (!check_value($this->error)) { // Apply the watermark ? if (!is_animated_image($this->working) && conditional_config('watermark_enable') and chevereto_config('watermark_opacity') > 0) { switch ($this->extension) { case 'gif': $src = imagecreatefromgif($this->working); break; case 'png': $src = imagecreatefrompng($this->working); break; case 'jpg': $src = imagecreatefromjpeg($this->working); break; } $src_width = imagesx($src); $src_height = imagesy($src); $watermark_src = imagecreatefrompng(__CHV_WATERMARK_FILE__); $watermark_width = imagesx($watermark_src); $watermark_height = imagesy($watermark_src); // Calculate the position switch (chevereto_config('watermark_x_position')) { case 'left': $watermark_x = chevereto_config('watermark_margin'); break; case 'center': $watermark_x = $src_width / 2 - $watermark_width / 2; break; case 'right': $watermark_x = $src_width - $watermark_width - chevereto_config('watermark_margin'); break; } switch (chevereto_config('watermark_y_position')) { case 'top': $watermark_y = chevereto_config('watermark_margin'); break; case 'center': $watermark_y = $src_height / 2 - $watermark_height / 2; break; case 'bottom': $watermark_y = $src_height - $watermark_height - chevereto_config('watermark_margin'); break; } // Watermark has the same or greater size of the image ? // --> Center the watermark if ($watermark_width == $src_width && $watermark_height == $src_height) { $watermark_x = $src_width / 2 - $watermark_width / 2; $watermark_y = $src_height / 2 - $watermark_height / 2; } // Watermark is too big ? // --> Fit the watermark on the image if ($watermark_width > $src_width || $watermark_height > $src_height) { // Watermark is wider than the image if ($watermark_width > $src_width) { $watermark_new_width = $src_width; $watermark_new_height = $src_width * $watermark_height / $watermark_width; if ($watermark_new_height > $src_height) { $watermark_new_width = $src_height * $watermark_width / $watermark_height; $watermark_new_height = $src_height; } } else { $watermark_new_width = $src_height * $watermark_width / $watermark_height; $watermark_new_height = $src_height; } $watermark_temp = $this->img_upload_path . 'temp_watermark_' . generateRandomString(64) . '.png'; $WatermarkResize = new ImageResize(__CHV_WATERMARK_FILE__, $watermark_temp, $watermark_new_width); if (!check_value($WatermarkResize->error)) { $watermark_width = $watermark_new_width; $watermark_height = $watermark_new_height; $watermark_src = imagecreatefrompng($watermark_temp); $watermark_x = $src_width / 2 - $watermark_width / 2; $watermark_y = $src_height / 2 - $watermark_height / 2; } } // Apply and save the watermark imagecopymerge_alpha($src, $watermark_src, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height, chevereto_config('watermark_opacity'), $this->extension); switch ($this->extension) { case 'gif': imagegif($src, $this->working); break; case 'png': imagepng($src, $this->working); break; case 'jpg': imagejpeg($src, $this->working, 96); break; } imagedestroy($src); @unlink($watermark_temp); } // Move the temp to the final path... $uploaded = rename($this->working, $image_filename); // Change the CHMOD of the file (for some php enviroments) @chmod($image_filename, 0644); @chmod($thumb_filename, 0644); if ($uploaded) { $info = get_info($image_filename); $file_path = absolute_to_relative($image_filename); $thumb_path = absolute_to_relative($thumb_filename); $image_url = absolute_to_url($image_filename); $name = str_replace('.' . $this->extension, '', str_replace($this->img_upload_path, '', $image_filename)); $this->image_info = array('image_name' => $name, 'image_filename' => $name . "." . $this->extension, 'image_type' => $this->extension, 'image_path' => $file_path, 'image_url' => $image_url, 'image_width' => $info['width'], 'image_height' => $info['height'], 'image_attr' => 'width="' . $info['width'] . '" height="' . $info['height'] . '"', 'image_bytes' => $info['bytes'], 'image_size' => $info['size'], 'image_thumb_url' => absolute_to_url($thumb_filename), 'image_thumb_path' => $thumb_path, 'image_thumb_width' => $this->thumb_width, 'image_thumb_height' => $this->thumb_height); switch ($this->storage) { case 'direct': $this->image_info['storage_id'] = 2; break; case 'datefolder': case 'datefolders': $this->image_info['storage_id'] = NULL; break; } // Shorthand the dB object $dB = $this->dB; if ($dB->dead) { $this->error = $dB->error; return false; } if ($dB->insert_file($this->image_info)) { $image_delete_hash = $dB->image_delete_hash; $this->image_info['image_id'] = $dB->last_insert_id(); $this->image_info['image_id_public'] = encodeID($this->image_info['image_id']); $this->image_info['image_viewer'] = __CHV_BASE_URL__ . __CHV_VIRTUALFOLDER_IMAGE__ . '/' . $this->image_info['image_id_public']; $this->image_info['image_shorturl'] = __CHV_BASE_URL__ . $this->image_info['image_id_public']; $this->image_info['image_delete_hash'] = $image_delete_hash; $this->image_info['image_delete_url'] = __CHV_BASE_URL__ . 'delete/image/' . $this->image_info['image_id_public'] . '/' . $image_delete_hash; $this->image_info['image_delete_confirm_url'] = __CHV_BASE_URL__ . 'delete-confirm/image/' . $this->image_info['image_id_public'] . '/' . $image_delete_hash; $this->image_info['image_date'] = date('Y-m-d H:i:s', time()); return true; } else { unlink($image_filename); unlink($thumb_filename); $this->error = $dB->error; return false; } } else { unlink($this->working); $this->error = 'error uploading'; return false; } } else { unlink($this->working); return false; } } else { // Invalid data return false; } }
function generate_image_gd($image_list) { // functions with alpha channel support require 'imagecopymerge_alpha.php'; require 'image_resize.php'; $body = array_shift($image_list); $body = imagecreatefrompng($body); $body = image_resize($body, self::IMAGE_WIDTH, self::IMAGE_HEIGHT); foreach ($image_list as $image_file) { $image = imagecreatefrompng($image_file); $image = image_resize($image, self::IMAGE_WIDTH, self::IMAGE_HEIGHT); imagecopymerge_alpha($body, $image, 0, 0, 0, 0, imagesx($image), imagesy($image), 100); imagedestroy($image); } list($width, $height) = $this->get_width_height(); $body = image_resize($body, $width, $height); imagesavealpha($body, true); return $body; }
function watermark($options) { if (!empty($options['watermark'])) { $this->watermarkFile = $options['watermark']; } if (!empty($options['watermark-ratio'])) { $this->watermarkRatio = $options['watermark-ratio']; } $watermark = imagecreatefrompng($this->watermarkFile); imagealphablending($watermark, true); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $watermark_resized_width = $this->imgWidth * $this->watermarkRatio; $watermark_resized_height = $this->imgHeight * $this->watermarkRatio; $watermark_resized = imagecreatetruecolor($watermark_resized_width, $watermark_resized_height); imagealphablending($watermark_resized, false); imagesavealpha($watermark_resized, true); imagecopyresampled($watermark_resized, $watermark, 0, 0, 0, 0, $watermark_resized_width, $watermark_resized_height, $watermark_width, $watermark_height); imagedestroy($watermark); // copy the image to watermark it or resize it if (!empty($options['width']) or !empty($options['height'])) { $options = $this->resize($options); $image_watermarked = $this->imgResized; } else { $image_watermarked = $this->imgResource; $options['width'] = $this->imgWidth; $options['height'] = $this->imgHeight; } $dest_x = ($options['width']->imgWidth - $watermark_resized_width) / 2; $dest_y = ($options['height'] - $watermark_resized_height) / 2; imagecopymerge_alpha($image_watermarked, $watermark_resized, $dest_x, $dest_y, 0, 0, $watermark_resized_width, $watermark_resized_height); imagedestroy($watermark_resized); if (!$this->alphaBlend($image_watermarked)) { return false; } $options['file'] = $options['file'] . '.' . $this->imgExt; switch ($this->imgType) { case 'image/gif': if (!imagegif($image_watermarked, $p['file'])) { $this->error = 'The GIF could not be created.'; return false; } break; case 'image/pjpeg': case 'image/jpeg': case 'image/jpg': if (!imagejpeg($image_watermarked, $options['file'], $this->jpegQuality)) { $this->error = 'The JPEG could not be created.'; return false; } break; case 'image/png': case 'image/x-png': if (!imagepng($image_watermarked, $options['file'])) { $this->error = 'The PNG could not be created.'; return false; } break; } imagedestroy($image_watermarked); return $options['file']; }
// Le format du fichier est correct $user = $_SESSION['Auth']; $user_id = $db->quote($user['id']); $db->query("INSERT INTO images SET user_id={$user_id}"); $image_id = $db->lastInsertId(); $image_name = $user['username'] . '_' . $image_id . '.' . $extension; move_uploaded_file($image['tmp_name'], IMAGES . '/' . $image_name); if ($extension == 'jpg') { $im = imagecreatefromjpeg(IMAGES . '/' . $image_name); } else { if ($extension == 'png') { $im = imagecreatefrompng(IMAGES . '/' . $image_name); } } $alpha = imagecreatefrompng(IMAGES . '/alpha/' . $_POST['alpha'] . '.png'); imagecopymerge_alpha($im, $alpha, 0, 0, 0, 0, imagesx($alpha), imagesy($alpha), 100); imagepng($im, IMAGES . '/' . $image_name); // free memory imagedestroy($im); $image_name = $db->quote($image_name); $db->query("UPDATE images SET name={$image_name} WHERE id={$image_id}"); } header('Location:' . WEBROOT . 'admin/my_creations.php'); die; } ?> <div> <video id="video"></video> <button id="startbutton">Prendre une photo</button> <canvas style="display: none" id="canvas"></canvas>
/** * @param array $options * 'watermark' => waImage|string $watermark * 'opacity' => float|int 0..1 * 'align' => self::ALIGN_* const * 'font_file' => null|string If null - will be used some default font. Note: use when watermark option is text * 'font_size' => float Size of font. Note: use when watermark option is text * 'font_color' => string Hex-formatted of color (without #). Note: use when watermark option is text * 'text_orientation' => self::ORIENTATION_* const. Note: use when watermark option is text * @throws waException * @return mixed */ protected function _watermark($options) { $watermark = false; $opacity = 0.5; $align = self::ALIGN_BOTTOM_RIGHT; $font_file = null; $font_size = 12; $font_color = '888888'; $text_orientation = self::ORIENTATION_HORIZONTAL; extract($options, EXTR_IF_EXISTS); $opacity = min(max($opacity, 0), 1); imagealphablending($this->image, true); if ($watermark instanceof waImage) { $type = $watermark->type; $gd_watermark = $this->createGDImageResourse($watermark->file, $type); $width = ifset($options['width'], $watermark->width); $height = ifset($options['height'], $watermark->height); $offset = $this->calcWatermarkOffset($width, $height, $align); if ($width != $watermark->width || $height != $watermark->height) { $watermark_resized = $this->_create($width, $height); if (function_exists('imagecopyresampled')) { imagecopyresampled($watermark_resized, $gd_watermark, 0, 0, 0, 0, $width, $height, $watermark->width, $watermark->height); } else { imagecopyresized($watermark_resized, $gd_watermark, 0, 0, 0, 0, $width, $height, $watermark->width, $watermark->height); } imagedestroy($gd_watermark); $gd_watermark = $watermark_resized; } imagecopymerge_alpha($this->image, $gd_watermark, $offset[0], $offset[1], 0, 0, $width, $height, $opacity * 100); imagedestroy($gd_watermark); } else { $text = (string) $watermark; if (!$text) { return; } $margin = round($font_size / 3.6); $font_color = array('r' => '0x' . substr($font_color, 0, 2), 'g' => '0x' . substr($font_color, 2, 2), 'b' => '0x' . substr($font_color, 4, 2), 'a' => floor((1 - $opacity) * 127)); if ($align == self::ALIGN_CENTER) { $rotation = (int) ifempty($options['rotation'], 0); $text_orientation = self::ORIENTATION_HORIZONTAL; } else { if ($text_orientation == self::ORIENTATION_VERTICAL) { $rotation = 90; } else { $rotation = 0; } } if (!empty($font_file) && file_exists($font_file)) { $gd_info = gd_info(); $gd_version = preg_replace('/[^0-9\\.]/', '', $gd_info['GD Version']); if (!empty($gd_info['FreeType Support']) && version_compare($gd_version, '2.0.1', '>=')) { // Free Type $free_type = true; } else { // True Type $free_type = false; // GD1 use pixels, GD2 use points if (version_compare($gd_version, '2.0', '<')) { $font_size = 24 * $font_size / 18; // 24px = 18pt } } if ($free_type) { $metrics = imageftbbox($font_size, 0, $font_file, $text); } else { $metrics = imagettfbbox($font_size, 0, $font_file, $text); } if ($metrics) { $width = $metrics[2] - $metrics[0]; $height = $metrics[1] - $metrics[7]; if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align, $margin); $offset = $this->watermarkOffsetFix($offset, $width, $height, $align, $text_orientation, $align == self::ALIGN_CENTER ? $rotation : 0); $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); if ($free_type) { imagefttext($this->image, $font_size, $rotation, $offset[0], $offset[1], $color, $font_file, $text); } else { imagettftext($this->image, $font_size, $rotation, $offset[0], $offset[1], $color, $font_file, $text); } } else { throw new waException(_ws("Can't read font file {$font_file}")); } } else { $font = floor(5 * $font_size / 12); if ($font < 1) { $font = 1; } else { if ($font > 5) { $font = 5; } } $width = imagefontwidth($font) * strlen($text); $height = imagefontheight($font); if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkOffset($width, $height, $align, $margin); if ($rotation != 0) { imagestring_rotate($this->image, $font, $rotation, $offset[0], $offset[1], $text, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); } else { $color = imagecolorallocatealpha($this->image, $font_color['r'], $font_color['g'], $font_color['b'], $font_color['a']); imagestring($this->image, $font, $offset[0], $offset[1], $text, $color); } } } }
function create_rt_icon_with_bg($rectype_id, $color_new) { //}, $bg_color ) { if (substr($rectype_id, 0, 4) == 'term') { $rectype_id = substr($rectype_id, 4); $path = HEURIST_TERM_ICON_DIR; } else { $path = HEURIST_ICON_DIR; } $alpha = 0; if (substr($rectype_id, -5, 5) == "m.png") { //for mapping $rectype_id = substr($rectype_id, 0, -5); $bg_color = array(200, 200, 200); //gray $filename2 = $path . $rectype_id . "m.png"; $alpha = 127; //0-127 } else { if (substr($rectype_id, -5, 5) == "s.png") { //for selection $rectype_id = substr($rectype_id, 0, -5); $bg_color = array(190, 228, 248); //#bee4f8 $filename2 = $path . $rectype_id . "s.png"; $alpha = 10; } else { $rectype_id = substr($rectype_id, 0, -4); $filename2 = $path . $rectype_id . ".png"; $bg_color = array(200, 200, 200); //gray $alpha = 127; //0-127 /*if(file_exists($filename2)){ download_file($filename2); return; }*/ } } $filename = $path . $rectype_id . ".png"; //original if (!file_exists($filename)) { //if term icon does not exist - take default icon $filename = HEURIST_ICON_DIR . "3.png"; } if (file_exists($filename)) { if ($color_new == null) { download_file($filename); return; } /*if($alpha==127){ download_file($filename); }*/ $img = imagecreatetruecolor(25, 25); //truecolor //imagealphablending($img, false); //imagesavealpha($img, false); // fill the background color $bg = imagecolorallocate($img, 200, 200, 200); // make the background transparent imagecolortransparent($img, $bg); //draw transparent rectangle imagefilledrectangle($img, 0, 0, 25, 25, $bg); // draw circle //$col_ellipse = imagecolorallocatealpha($img, $bg_color[0], $bg_color[1], $bg_color[2], $alpha); $col_ellipse = imagecolorallocate($img, $bg_color[0], $bg_color[1], $bg_color[2]); imagefilledellipse($img, 12, 12, 24, 24, $col_ellipse); // load icon $img_icon = @imagecreatefrompng($filename); $color_old = array(54, 100, 139); $color_new = !$color_new ? array(255, 0, 0) : $color_new; /* RGB of your inside color */ $rgb = $color_new; //array(0,0,255); /* Negative values, don't edit */ $rgb = array($color_old[0] - $rgb[0], $color_old[1] - $rgb[1], $color_old[2] - $rgb[2]); imagefilter($img_icon, IMG_FILTER_NEGATE); imagefilter($img_icon, IMG_FILTER_COLORIZE, $rgb[0], $rgb[1], $rgb[2]); imagefilter($img_icon, IMG_FILTER_NEGATE); //imagealphablending( $im, false ); //imagesavealpha( $im, true ); /*if($alpha==127){ imagecopy($img, $img_icon, 4, 4, 0, 0, 16, 16); //keep bg of icon - transparent hole }*/ // merge icon $imageInfo = getimagesize($filename); if (is_array($imageInfo) && $imageInfo[0] == 24 && $imageInfo[1] == 24) { imagecopymerge_alpha($img, $img_icon, 0, 0, 0, 0, 24, 24, 70); //mix background to dark } else { imagecopymerge_alpha($img, $img_icon, 4, 4, 0, 0, 16, 16, 70); //mix background to dark } /*$bg = imagecolorallocate($img_icon, 255, 255, 255); // make the background transparent imagecolortransparent($img_icon, $bg);*/ /*$cut = imagecreatetruecolor(16, 16); //imagecreatetruecolor imagealphablending($cut, false); //imagesavealpha($cut, true); imagecopy($cut, $img_icon, 0, 0, 0, 0, 16, 16); imagecopymerge($img, $cut, 4, 4, 0, 0, 16, 16, 70);*/ //imagealphablending($img_icon, true); //imagesavealpha($img_icon, true); //imagecopy($img, $img_icon, 4, 4, 0, 0, 16, 16); //imagecopymerge($img, $img_icon, 4, 4, 0, 0, 16, 16, 70); //imagealphablending(, false); // output header("Content-type: image/png"); imagepng($img); //imagepng($img, $filename2); //save to file //readfile($filename2); imagedestroy($img); imagedestroy($img_icon); } }