Пример #1
0
 /**
  * Destroys allocated gmagick resources
  */
 public function __destruct()
 {
     if (null !== $this->gmagick && $this->gmagick instanceof \Gmagick) {
         $this->gmagick->clear();
         $this->gmagick->destroy();
     }
 }
 private function drawRectangleAndReplaceExistImg($image, $height, $width)
 {
     // прямоугольник рисуем снизу изображения высотой 8% от высоты изображения
     $heightRectagle = $height * 0.08;
     $gmagicDraw = new GmagickDraw();
     $gmagicDraw->setfillcolor("#fff");
     $gmagicDraw->rectangle(0, $height - $heightRectagle, $width, $height);
     $gImage = new Gmagick();
     $gImage->readImage($image);
     $gImage->drawimage($gmagicDraw);
     $gImage->writeimage($image);
     $gImage->destroy();
 }
Пример #3
0
 function thumbnail($upfiledir, $src, $tName, $tw = '', $th = '', $scale = true, $tDir = "thumb")
 {
     global $iCMS;
     $R = array();
     $tw = empty($tw) ? $iCMS->config['thumbwidth'] : (int) $tw;
     $th = empty($th) ? $iCMS->config['thumbhight'] : (int) $th;
     if ($tw && $th) {
         list($width, $height, $type) = @getimagesize($src);
         if ($width < 1 && $height < 1) {
             $R['width'] = $tw;
             $R['height'] = $th;
             $R['src'] = $src;
             return $R;
         }
         if ($width > $tw || $height > $th) {
             $R['src'] = $upfiledir . $tDir . "/" . $tName . '_' . $tw . 'x' . $th . '.' . substr(strrchr($src, "."), 1);
             if (in_array('Gmagick', get_declared_classes())) {
                 $image = new Gmagick();
                 $image->readImage($src);
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight()));
                 $image->resizeImage($im['w'], $im['h'], null, 1);
                 $image->cropImage($tw, $th, 0, 0);
                 //$image->thumbnailImage($gm_w,$gm_h);
                 FS::mkdir($upfiledir . $tDir);
                 $image->writeImage($R['src']);
                 $image->destroy();
             } else {
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $width, "h" => $height), $scale);
                 $R['width'] = $im['w'];
                 $R['height'] = $im['h'];
                 $res = self::imagecreate($type, $src);
                 if ($res) {
                     $thumb = imagecreatetruecolor($im['w'], $im['h']);
                     imagecopyresampled($thumb, $res, 0, 0, 0, 0, $im['w'], $im['h'], $width, $height);
                     //PHP_VERSION != '4.3.2' && self::UnsharpMask($thumb);
                     FS::mkdir($upfiledir . $tDir);
                     self::image($thumb, $type, $R['src']);
                 } else {
                     $R['src'] = $src;
                 }
             }
         } else {
             $R['width'] = $width;
             $R['height'] = $height;
             $R['src'] = $src;
         }
         return $R;
     }
 }
Пример #4
0
 function encode_gmagick($ps_filepath, $ps_output_path, $pa_options)
 {
     if (!($magick = $this->mimetype2magick[$pa_options["output_mimetype"]])) {
         $this->error = "Invalid output format";
         return false;
     }
     #
     # Open image
     #
     try {
         $h = new Gmagick($ps_filepath);
         $h->setimageindex(0);
         // force use of first image in multi-page TIFF
     } catch (Exception $e) {
         $this->error = "Couldn't open image {$ps_filepath}";
         return false;
     }
     if (function_exists('exif_read_data')) {
         if (is_array($va_exif = @exif_read_data($ps_filepath, 'EXIF', true, false))) {
             if (isset($va_exif['IFD0']['Orientation'])) {
                 $vn_orientation = $va_exif['IFD0']['Orientation'];
                 switch ($vn_orientation) {
                     case 3:
                         $h->rotateimage("#FFFFFF", 180);
                         break;
                     case 6:
                         $h->rotateimage("#FFFFFF", 90);
                         break;
                     case 8:
                         $h->rotateimage("#FFFFFF", -90);
                         break;
                 }
             }
         }
     }
     $h->setimagetype(Gmagick::IMGTYPE_TRUECOLOR);
     if (!$h->setimagecolorspace(Gmagick::COLORSPACE_RGB)) {
         $this->error = "Error during RGB colorspace transformation operation";
         return false;
     }
     $va_tmp = $h->getimagegeometry();
     $image_width = $va_tmp['width'];
     $image_height = $va_tmp['height'];
     if ($image_width < 10 || $image_height < 10) {
         $this->error = "Image is too small to be output as Tilepic; minimum dimensions are 10x10 pixels";
         return false;
     }
     if ($pa_options["scale_factor"] != 1) {
         $image_width *= $pa_options["scale_factor"];
         $image_height *= $pa_options["scale_factor"];
         if (!$h->resizeimage($image_width, $image_height, Gmagick::FILTER_CUBIC, $pa_options["antialiasing"])) {
             $this->error = "Couldn't scale image";
             return false;
         }
     }
     #
     # How many layers to make?
     #
     if (!$pa_options["layers"]) {
         $sw = $image_width * $pa_options["layer_ratio"];
         $sh = $image_height * $pa_options["layer_ratio"];
         $pa_options["layers"] = 1;
         while ($sw >= $pa_options["tile_width"] || $sh >= $pa_options["tile_height"]) {
             $sw = ceil($sw / $pa_options["layer_ratio"]);
             $sh = ceil($sh / $pa_options["layer_ratio"]);
             $pa_options["layers"]++;
         }
     }
     #
     # Cut image into tiles
     #
     $tiles = 0;
     $layer_list = array();
     $base_width = $image_width;
     $base_height = $image_height;
     if ($this->debug) {
         print "BASE {$base_width} x {$base_height} \n";
     }
     for ($l = $pa_options["layers"]; $l >= 1; $l--) {
         $x = $y = 0;
         $wx = $pa_options["tile_width"];
         $wy = $pa_options["tile_height"];
         if ($this->debug) {
             print "LAYER={$l}\n";
         }
         if ($l < $pa_options["layers"]) {
             $image_width = ceil($image_width / $pa_options["layer_ratio"]);
             $image_height = ceil($image_height / $pa_options["layer_ratio"]);
             if ($this->debug) {
                 print "RESIZE layer {$l} TO {$image_width} x {$image_height} \n";
             }
             if (!$h->resizeimage($image_width, $image_height, Gmagick::FILTER_CUBIC, $pa_options["antialiasing"])) {
                 $this->error = "Couldn't scale image";
                 return false;
             }
         }
         $i = 0;
         $layer_list[] = array();
         while ($y < $image_height) {
             $slice = clone $h;
             try {
                 $slice->cropimage($wx, $wy, $x, $y);
                 $slice->setcompressionquality($pa_options["quality"]);
             } catch (Exception $e) {
                 $this->error = "Couldn't create tile";
                 return false;
             }
             if (!$slice->setimageformat($magick)) {
                 $this->error = "Tile conversion failed: {$reason}; {$description}";
                 return false;
             }
             # --- remove color profile (saves lots of space)
             $layer_list[sizeof($layer_list) - 1][] = $slice->getImageBlob();
             $slice->destroy();
             $x += $pa_options["tile_width"];
             if ($x >= $image_width) {
                 $y += $pa_options["tile_height"];
                 $x = 0;
             }
             $i++;
             $tiles++;
         }
         if ($this->debug) {
             print "OUTPUT {$tiles} TILES FOR LAYER {$l} : {$image_width} x {$image_height}\n";
         }
     }
     $h->destroy();
     #
     # Write Tilepic format file
     #
     if ($this->debug) {
         print "WRITING FILE...";
     }
     if ($fh = fopen($ps_output_path . ".tpc", "w")) {
         # --- attribute list
         $attribute_list = "";
         $attributes = 0;
         if (isset($pa_options["attributes"]) && is_array($pa_options["attributes"])) {
             $pa_options["attributes"]["mimeType"] = $pa_options["output_mimetype"];
         } else {
             $pa_options["attributes"] = array("mimeType" => $pa_options["output_mimetype"]);
         }
         foreach ($pa_options["attributes"] as $k => $v) {
             $attribute_list .= "{$k}={$v}";
             $attributes++;
         }
         if ($this->debug) {
             print "header OK;";
         }
         # --- header
         if (!fwrite($fh, "TPC\n")) {
             $this->error = "Could not write Tilepic signature";
             return false;
         }
         if (!fwrite($fh, pack("NNNNNNnnNN", 40, $base_width, $base_height, $pa_options["tile_width"], $pa_options["tile_height"], $tiles, $pa_options["layers"], $pa_options["layer_ratio"], strlen($attribute_list), $attributes))) {
             $this->error = "Could not write Tilepic header";
             return false;
         }
         # --- offset table
         $offset = 44 + $tiles * 4;
         for ($i = sizeof($layer_list) - 1; $i >= 0; $i--) {
             for ($j = 0; $j < sizeof($layer_list[$i]); $j++) {
                 if (!fwrite($fh, pack("N", $offset))) {
                     $this->error = "Could not write Tilepic offset table";
                     return false;
                 }
                 $offset += strlen($layer_list[$i][$j]);
             }
         }
         if ($this->debug) {
             print "offset table OK;";
         }
         if (!fwrite($fh, pack("N", $offset))) {
             $this->error = "Could not finish writing Tilepic offset table";
             return false;
         }
         # --- tiles
         for ($i = sizeof($layer_list) - 1; $i >= 0; $i--) {
             for ($j = 0; $j < sizeof($layer_list[$i]); $j++) {
                 if (!fwrite($fh, $layer_list[$i][$j])) {
                     $this->error = "Could not write Tilepic tile data";
                     return false;
                 }
             }
         }
         if ($this->debug) {
             print "tiles OK;";
         }
         unset($layer_list);
         # --- attributes
         if (!fwrite($fh, $attribute_list)) {
             $this->error = "Could not write Tilepic attributes";
             return false;
         }
         if ($this->debug) {
             print "attributes OK\n";
         }
         fclose($fh);
         return $pa_options;
     } else {
         $this->error = "Couldn't open output file {$ps_output_path}\n";
         return false;
     }
 }
Пример #5
0
                copy($target_path, $Thumb_Path);
                // initialize object
                $image = new Gmagick();
                $image->readImage($target_path);
                $old_width = $image->getImageWidth();
                $old_height = $image->getImageHeight();
                $new_width = 200;
                $new_height = $old_height / $old_width * $new_width;
                exec("gm mogrify -resize {$new_width}x{$new_height} -quality 90 " . APP_INSTALLPATH . UPLOAD_PATH . "thumbs/{$name}");
                //$image->thumbnailImage($new_width, $new_height);
                //$thumbFile = dirname($file) . '/' . basename($target_path, '.JPG') . '.thumb.jpg';
                //$image->writeImage($Thumb_Path);
                /*$thumb_size = $image->getsize();
                		$MF->updateDiskSpace($thumb_size);*/
                // free resource handle
                $image->destroy();
            } catch (Exception $e) {
                die($e->getMessage());
            }
        }
        $file_success = execute_sqlInsert('tbl_chatFiles', array('fileName' => $file_name, 'fileRandomName' => $name, 'fileExt' => $type, 'fileSize' => $size, 'fileCode' => $UFID, 'fileType' => $file_type));
        $fileId = mysql_insert_id();
        if ($fileId) {
            $newmsg = 'has uploaded a file <a href="chatfiledownload.php?fc=' . $fileId . '">' . $file_name . '</a>';
            $success = execute_sqlInsert('tbl_ChatRooms', array('saidBy_username' => $CURRENT_USER, 'saidBy_empl_id' => $_SESSION["empl_id"], 'message_base64' => '', 'message_plain_mysqlescaped' => $newmsg, 'chatRoom' => $rid, 'msgType' => 'F', 'fileId' => $fileId));
            register_LastPingAt();
        }
        $result = 1;
    }
    //}
}
Пример #6
0
 /**
  * Apply transformations on image
  *
  * @param string $source Source image
  * @param array  $params Transformations and parameters
  * @param string $store  Temporary store on disk
  *
  * @return string
  */
 public function transform($source, $params, $store = null)
 {
     try {
         $image = new \Gmagick();
         $image->readImage($source);
         if (isset($params['negate'])) {
             $this->canNegate();
         }
         if (isset($params['rotate'])) {
             $image->rotateImage('#000', $params['rotate']['angle']);
         }
         if (isset($params['crop'])) {
             $image->cropImage($params['crop']['width'], $params['crop']['height'], $params['crop']['x'], $params['crop']['y']);
         }
         if (isset($params['contrast'])) {
             $this->canContrast();
         }
         if (isset($params['brightness'])) {
             $value = (int) $params['brightness'];
             $brightness = null;
             if ($value <= 0) {
                 $brightness = $value + 100;
             } else {
                 $brightness = $value * 3 + 100;
             }
             $image->modulateImage($brightness, 100, 100);
         }
         $ret = null;
         if ($store !== null) {
             $ret = $image->writeImage($store);
         } else {
             $ret = $image->getImageBlob();
         }
         $image->destroy();
         return $ret;
     } catch (\GmagickException $e) {
         $image->destroy();
         throw new \RuntimeException($e->getMessage());
     }
 }
Пример #7
0
 public static function thumbnail($src, $tw = "0", $th = "0", $scale = true)
 {
     if (!self::$config['thumb']['enable']) {
         return;
     }
     $rs = array();
     $tw = empty($tw) ? self::$config['thumb']['width'] : (int) $tw;
     $th = empty($th) ? self::$config['thumb']['height'] : (int) $th;
     if ($tw && $th) {
         list($width, $height, $type) = getimagesize($src);
         if ($width < 1 && $height < 1) {
             $rs['width'] = $tw;
             $rs['height'] = $th;
             $rs['src'] = $src;
             return $rs;
         }
         if ($width > $tw || $height > $th) {
             $rs['src'] = $src . '_' . $tw . 'x' . $th . '.jpg';
             if (in_array('Gmagick', get_declared_classes())) {
                 $image = new Gmagick();
                 $image->readImage($src);
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight()));
                 $image->resizeImage($im['w'], $im['h'], null, 1);
                 $image->cropImage($tw, $th, 0, 0);
                 //$image->thumbnailImage($gm_w,$gm_h);
                 $image->writeImage($rs['src']);
                 $image->destroy();
             } else {
                 $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $width, "h" => $height), $scale);
                 $ret = self::imagecreate($type, $src);
                 $rs['width'] = $im['w'];
                 $rs['height'] = $im['h'];
                 if ($ret) {
                     $thumb = imagecreatetruecolor($im['w'], $im['h']);
                     imagecopyresampled($thumb, $ret, 0, 0, 0, 0, $im['w'], $im['h'], $width, $height);
                     self::image($thumb, $type, $rs['src']);
                 } else {
                     $rs['src'] = $src;
                 }
             }
         } else {
             $rs['src'] = $src;
             $rs['width'] = $width;
             $rs['height'] = $height;
         }
         return $rs;
     }
 }