Exemple #1
0
 function init()
 {
     session_write_close();
     parent::init();
     $options = $this->options;
     $font_path = $this->getFontPath();
     $options['desired_width'] = round($options['desired_width']);
     // $text = $this->wrap($options['font_size'],$options['rotation_angle'],$font_path,$options['text'],$options['desired_width']);
     // $width_height = $this->getTextBoxWidthHeight($options['text'],$font_path);
     $options['halign'] = $options['alignment_center'] == true ? 'center' : ($options['alignment_right'] == 'right' ? 'right' : 'left');
     /*
     	Calculating height of the of the box 
     */
     $im = imagecreatetruecolor($options['desired_width'], 10);
     imagesavealpha($im, true);
     $backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);
     imagefill($im, 0, 0, $backgroundColor);
     $this->phpimage = $im;
     $box = new \GDText\Box($this->phpimage);
     $box->setFontFace($font_path);
     $rgb_color_array = $this->hex2rgb($options['text_color']);
     $box->setFontColor(new \GDText\Color($rgb_color_array[0], $rgb_color_array[1], $rgb_color_array[2]));
     $box->setFontSize($options['font_size']);
     $box->setBox(0, 0, $options['desired_width'], 0);
     $box->setTextAlign($options['halign'], 'top');
     if ($options['underline']) {
         $box->setUnderline();
     }
     // $h = $box->draw($options['text']);
     $this->new_height = $h = $box->getBoxHeight($options['text']);
     //CREATING DEFAULT IMAGES
     $im = imagecreatetruecolor($options['desired_width'], $h);
     imagesavealpha($im, true);
     $backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);
     imagefill($im, 0, 0, $backgroundColor);
     $this->phpimage = $im;
     $box = new \GDText\Box($this->phpimage);
     $box->setFontFace($font_path);
     $rgb_color_array = $this->hex2rgb($options['text_color']);
     $box->setFontColor(new \GDText\Color($rgb_color_array[0], $rgb_color_array[1], $rgb_color_array[2]));
     // $box->setTextShadow(new \GDText\Color(0, 0, 0, 50), 2, 2);
     $box->setFontSize($options['font_size']);
     // $box->enableDebug();
     $box->setBox(0, 0, $options['desired_width'], $h);
     $box->setTextAlign($options['halign'], 'top');
     if ($options['underline']) {
         $box->setUnderline();
     }
     $box->draw($options['text']);
     if ($options['rotation_angle']) {
         $this->phpimage = imagerotate($this->phpimage, $options['rotation_angle'], imageColorAllocateAlpha($im, 255, 255, 255, 127));
         imagealphablending($this->phpimage, false);
         imagesavealpha($this->phpimage, true);
     }
 }
Exemple #2
0
/**
 * Create an avatar with initials
 * @param string $char Character that be print in image
 * @param string $name Name of image
 * @param number $s Size in pixels
 * @param string $format Output format
 */
function generate_avatar($char, $name, $s = 150, $format = "png")
{
    require_once CINDA_DIR . 'vendors/GDText/Box.php';
    require_once CINDA_DIR . 'vendors/GDText/Color.php';
    $char = strtoupper($char);
    if (2 < strlen($char)) {
        $char = substr($char, 0, 2);
    }
    $filename = $name . "." . $format;
    if (!file_exists(wp_upload_dir()['basedir'] . "/avatars/")) {
        mkdir(wp_upload_dir()['basedir'] . "/avatars/", 0775);
    }
    $private_uri = wp_upload_dir()['basedir'] . "/avatars/" . $filename;
    // Create image
    $img = @imagecreatetruecolor($s, $s);
    // Background color
    $red = (int) rand(128, 256);
    $green = (int) rand(128, 256);
    $blue = (int) rand(128, 256);
    // Create color
    $color = imagecolorallocate($img, $red, $green, $blue);
    // Asign color to background
    imagefill($img, 0, 0, $color);
    // Text Box (using GDText)
    $font_family = CINDA_DIR . 'assets/fonts/Oswald-Bold.ttf';
    $font_size = (int) ($s / 2);
    $textbox = new GDText\Box($img);
    $textbox->setFontSize($font_size);
    $textbox->setFontFace($font_family);
    $textbox->setFontColor(new GDText\Color(250, 250, 250));
    // black
    $textbox->setBox(0, 0, imagesx($img), imagesy($img));
    // now we have to align the text horizontally and vertically inside the textbox
    // the texbox covers whole image, so text will be centered relatively to it
    $textbox->setTextAlign('center', 'center');
    // it accepts multiline text
    $textbox->draw($char);
    // Save image
    if ("png" === $format) {
        if (imagepng($img, $private_uri)) {
            return $filename;
        }
    } else {
        if ("jpg" === $format) {
            if (imagejpeg($img, $private_uri, 90)) {
                return $filename;
            }
        }
    }
    imagedestroy($img);
    return false;
}