Exemple #1
0
 /**
  * {@inheritDoc}
  */
 public function watermarkText($wm_txt_text, $wm_txt_font_path, $wm_txt_start_x = 0, $wm_txt_start_y = 0, $wm_txt_font_size = 10, $wm_txt_font_color = 'transwhitetext', $wm_txt_font_alpha = 60)
 {
     if (false === $this->isClassSetup()) {
         return false;
     }
     // setup source image object
     if (false === $this->setupSourceImageObject()) {
         return false;
     }
     // check previous step contain errors?
     if ($this->isPreviousError() === true) {
         return false;
     }
     if (!is_file($wm_txt_font_path)) {
         $this->status = false;
         $this->status_msg = 'Unable to load font file.';
         return false;
     }
     $wm_txt_font_path = realpath($wm_txt_font_path);
     // find text width and height
     // +10 will be -5 padding on watermark text area.
     $ImagickDraw = new \ImagickDraw();
     $ImagickDraw->setFont($wm_txt_font_path);
     $ImagickDraw->setFontSize($wm_txt_font_size);
     $ImagickDraw->setGravity(\Imagick::GRAVITY_NORTHWEST);
     // set new resolution for font due to it is smaller than GD if it was not set.
     $ImagickDraw->setresolution(96, 96);
     $type_space = $this->Imagick->queryFontMetrics($ImagickDraw, $wm_txt_text, false);
     if (is_array($type_space) && array_key_exists('textWidth', $type_space) && array_key_exists('textHeight', $type_space)) {
         $wm_txt_height = $type_space['textHeight'];
         $wm_txt_width = $type_space['textWidth'];
     }
     unset($type_space);
     // if start x or y is number, convert to integer value
     if (is_numeric($wm_txt_start_x)) {
         $wm_txt_start_x = intval($wm_txt_start_x);
     }
     if (is_numeric($wm_txt_start_y)) {
         $wm_txt_start_y = intval($wm_txt_start_y);
     }
     // if start x or y is NOT number, find the real position of start x or y from word left, center, right, top, middle, bottom
     if (!is_numeric($wm_txt_start_x) || !is_numeric($wm_txt_start_y)) {
         if (!is_numeric($wm_txt_start_x)) {
             switch (strtolower($wm_txt_start_x)) {
                 case 'center':
                     $image_width = $this->Imagick->getImageWidth();
                     $watermark_width = $wm_txt_width;
                     $wm_txt_start_x = $this->calculateStartXOfCenter($watermark_width, $image_width);
                     unset($image_width, $watermark_width);
                     break;
                 case 'right':
                     $image_width = $this->Imagick->getImageWidth();
                     $wm_txt_start_x = intval($image_width - $wm_txt_width) - 5;
                     // minus 5 because Imagick is different from GD.
                     unset($image_width);
                     break;
                 case 'left':
                 default:
                     $wm_txt_start_x = 10;
                     // Imagick is different from GD, so increase it.
                     break;
             }
         }
         if (!is_numeric($wm_txt_start_y)) {
             switch (strtolower($wm_txt_start_y)) {
                 case 'middle':
                     $image_height = $this->Imagick->getImageHeight();
                     $watermark_height = $wm_txt_height;
                     $wm_txt_start_y = $this->calculateStartXOfCenter($watermark_height, $image_height);
                     unset($image_height, $watermark_height);
                     break;
                 case 'bottom':
                     $image_height = $this->Imagick->getImageHeight();
                     if ($image_height - ($wm_txt_height + 5) > '0') {
                         $wm_txt_start_y = intval($image_height - ($wm_txt_height + 5));
                     } else {
                         $wm_txt_start_y = intval($image_height - $wm_txt_height);
                     }
                     unset($image_height);
                     break;
                 case 'top':
                 default:
                     $wm_txt_start_y = 10;
                     // Imagick is different from GD, so increase it.
                     break;
             }
         }
     }
     // begins watermark text --------------------------------------------------------------------------------------------
     // set color
     $black = new \ImagickPixel('black');
     $white = new \ImagickPixel('white');
     $transwhite = new \ImagickPixel('rgba(255, 255, 255, 0)');
     // set color transparent white
     $transwhitetext = new \ImagickPixel('rgba(255, 255, 255, ' . $this->convertAlpha127ToRgba($wm_txt_font_alpha) . ')');
     if (!isset(${$wm_txt_font_color})) {
         $wm_txt_font_color = 'transwhitetext';
     }
     // fill font color
     $ImagickDraw->setFillColor(${$wm_txt_font_color});
     // write text on image
     if ($this->source_image_frames > 1) {
         // if source image is animated gif
         $this->Imagick = $this->Imagick->coalesceImages();
         if (is_object($this->Imagick)) {
             $i = 1;
             foreach ($this->Imagick as $Frame) {
                 $Frame->annotateImage($ImagickDraw, $wm_txt_start_x, $wm_txt_start_y, 0, $wm_txt_text);
                 $Frame->setImagePage(0, 0, 0, 0);
                 if ($i == 1) {
                     $this->ImagickFirstFrame = $Frame->getImage();
                 }
                 $i++;
             }
             unset($Frame, $i);
         }
     } else {
         $this->Imagick->annotateImage($ImagickDraw, $wm_txt_start_x, $wm_txt_start_y, 0, $wm_txt_text);
         if ($this->source_image_type == '1') {
             // if source image is gif, set image page to prevent sizing error.
             $this->Imagick->setImagePage(0, 0, 0, 0);
         }
         $this->ImagickFirstFrame = null;
     }
     // end watermark text -----------------------------------------------------------------------------------------------
     $ImagickDraw->clear();
     $black->destroy();
     $transwhite->destroy();
     $transwhitetext->destroy();
     $white->destroy();
     unset($black, $ImagickDraw, $transwhite, $transwhitetext, $white, $wm_txt_height, $wm_txt_width);
     $this->destination_image_height = $this->Imagick->getImageHeight();
     $this->destination_image_width = $this->Imagick->getImageWidth();
     $this->source_image_height = $this->destination_image_height;
     $this->source_image_width = $this->destination_image_width;
     $this->status = true;
     $this->status_msg = null;
     return true;
 }
Exemple #2
0
<?php

$im = new Imagick();
$im->clear();
$im->destroy();
$im = new ImagickDraw();
$im->clear();
$im->destroy();
$im = new ImagickPixel();
$im->clear();
$im->destroy();
$magick = new Imagick('magick:rose');
$im = new ImagickPixelIterator($magick);
$im->clear();
$im->destroy();
echo 'success';