$Image1->readImage($input_file);
    $timers['open'] += microtime(true) - $time_open_start;
    $time_get_info_start = microtime(true);
    $width = $Image1->getImageWidth();
    $height = $Image1->getImageHeight();
    $timers['get_info'] += microtime(true) - $time_get_info_start;
    $time_get_info_start = microtime(true);
    $new_width = ceil($width / TestSettings::DOWNSCALE_FACTOR);
    $new_height = ceil($height / TestSettings::DOWNSCALE_FACTOR);
    $timers['get_info'] += microtime(true) - $time_get_info_start;
    $time_scale_start = microtime(true);
    $Image1->scaleImage($new_width, $new_height);
    $timers['scale'] += microtime(true) - $time_scale_start;
    $time_rotate_start = microtime(true);
    $WhiteColor = new GmagickPixel('white');
    $Image1->rotateImage($WhiteColor, TestSettings::ROTATE_ANGLE);
    $timers['rotate'] += microtime(true) - $time_rotate_start;
    $time_new_start = microtime(true);
    $Image2 = new Gmagick();
    $Image2->newImage(TestSettings::OUTPUT_IMAGE_WIDTH, TestSettings::OUTPUT_IMAGE_HEIGHT, 'white');
    $timers['new'] += microtime(true) - $time_new_start;
    $time_paste_start = microtime(true);
    $Image2->compositeImage($Image1, 1, TestSettings::PASTE_X, TestSettings::PASTE_Y);
    $timers['paste'] += microtime(true) - $time_paste_start;
    $time_save_start = microtime(true);
    $Image2->setImageFormat('JPEG');
    $Image2->setCompressionQuality(TestSettings::OUTPUT_JPEG_QUALITY);
    file_put_contents($output_file, $Image2);
    $timers['save'] += microtime(true) - $time_save_start;
}
print_r(['gmagick' => $timers]);
Пример #2
0
 public function resizeFit($width, $height, $background = false)
 {
     //
     if (!$width) {
         $width = 1;
     }
     if (!$height) {
         $height = 1;
     }
     try {
         $this->image->scaleImage($width, $height, true);
         $w = $this->image->getImageWidth();
         $h = $this->image->getImageHeight();
     } catch (Exception $e) {
         return false;
     }
     if ($background === false) {
         $this->width = $w;
         $this->height = $h;
         return true;
     } else {
         try {
             $this->image->setImageBackgroundColor($background);
             $x = round(($width - $w) / 2);
             $y = round(($height - $h) / 2);
             $img = new Gmagick();
             $img->newImage($width, $height, $background);
             $img->compositeImage($this->image, 1, $x, $y);
         } catch (Exception $e) {
             return false;
         }
         $this->image = $img;
         $this->width = $width;
         $this->height = $height;
         return true;
     }
 }
Пример #3
0
 public function process($args)
 {
     if (isset($args[0]) and is_numeric($args[0])) {
         $rotation = $args[0];
     } else {
         $rotation = mt_rand(-5, 5);
     }
     if (isset($args[1])) {
         $signature = $args[1];
     }
     if (isset($args[2])) {
         $role = $args[2];
     }
     /* -------------------------- */
     $frame_path = DOCROOT . "/staticfiles/img/polaroids/polaroid_frame.png";
     if (!isset($frame_path) or !file_exists($frame_path)) {
         return;
     }
     // Load the frame and crop the requested image
     $frame = new Gmagick();
     $frame->readImage($frame_path);
     $w = $frame->getimagewidth();
     $h = $frame->getimageheight();
     $this->crop(285, 294);
     $x = 28;
     $y = 31;
     $this->image->borderImage("transparent", $x, $y);
     // Have to add a border as the x displacement in compositeImage() is broken!
     $frame->compositeImage($this->image, Gmagick::COMPOSITE_OVER, 0, 0);
     // Some comp styles seem to throw errors!
     $this->image = $frame;
     // Add the signature if we have been asked for one
     if (isset($signature)) {
         $sig_path = DOCROOT . "/staticfiles/img/polaroids/" . $signature . "_sig.png";
         if (file_exists($sig_path)) {
             $sig = new Gmagick();
             $sig->readImage($sig_path);
             $sw = $sig->getimagewidth();
             $sh = $sig->getimageheight();
             $x = ($this->image->getimagewidth() - $sig->getimagewidth()) / 2;
             $y = 330;
             // Have to add a border as the x displacement in compositeImage() is broken!
             $sig->borderImage("transparent", $x, $y);
             $this->image->compositeImage($sig, Gmagick::COMPOSITE_OVER, 0, 0);
             // Some comp styles seem to throw errors!
         }
     }
     // Add the role if we've been asked for one
     if (isset($role)) {
         $font_size = 17;
         $draw = new GmagickDraw();
         $draw->setFontSize($font_size);
         $draw->setFont(DOCROOT . "/staticfiles/img/polaroids/monaco.ttf");
         $draw->setFillColor('#666');
         $text_width = $font_size * strlen($role) * 0.77;
         // Seems to be about a 0.77 ration for monaco between width and height
         $x = ($this->image->getimagewidth() - $text_width) / 2;
         $this->image->annotateimage($draw, $x, 395, 0, $role);
     }
     // Rotate the image
     if ($rotation != 0) {
         $frame->magnifyimage();
         $frame->magnifyimage();
         $this->image->rotateimage('transparent', $rotation);
         $frame->minifyimage();
         $frame->minifyimage();
     }
 }
Пример #4
0
 /** 
  * 生成水印
  * @param string $groundImage 
  */
 function waterImage($groundImage = '')
 {
     try {
         //获取背景图的高,宽
         if ($groundImage && is_file($groundImage)) {
             $bg = new Gmagick();
             $bg->readImage($groundImage);
             $bgHeight = $bg->getImageHeight();
             $bgWidth = $bg->getImageWidth();
         }
         //获取水印图的高,宽
         if ($this->waterImage && is_file($this->waterImage)) {
             $water = new Gmagick($this->waterImage);
             $waterHeight = $water->getImageHeight();
             $waterWidth = $water->getImageWidth();
         }
         //如果背景图的高宽小于水印图的高宽则不加水印
         if ($bgHeight < $waterHeight || $bgWidth < $waterWidth) {
             return false;
         } else {
             $isWaterImg = TRUE;
         }
         //加水印
         if ($isWaterImg) {
             $dw = new GmagickDraw();
             //加图片水印
             if (is_file($this->waterImage)) {
                 //水印位置随机
                 $waterPos = $this->getWaterPos($bgWidth, $bgHeight, $waterWidth, $waterHeight);
                 $bg->compositeImage($water, 1, $waterPos['x'], $waterPos['y']);
                 if (!$bg->writeImage($groundImage)) {
                     return FALSE;
                 }
             } else {
                 //加文字水印
                 $waterTextInfo = array('textFont' => '15', 'textColor' => '#FF0000', 'textAlpha' => 1, 'textInfo' => 'www.okooo.com');
                 $dw->setFontSize($waterTextInfo['textFont']);
                 //$dw->setFillColor($waterTextInfo['textColor']);
                 $dw->setFillOpacity(1);
                 $x = abs(130 - $bgWidth);
                 $y = abs(15 - $bgHeight);
                 $dw->annotate($x, $y, $waterTextInfo['textInfo']);
                 $dw->setTextEncoding('UTF-8');
                 $bg->drawImage($dw);
                 if (!$bg->writeImage($groundImage)) {
                     return FALSE;
                 }
             }
         }
     } catch (Exception $e) {
         Logger::getLogger('dataengine.lottery.snapshot')->apps('exception')->info(json_encode($e->getMessage()));
     }
 }