示例#1
0
 /**
  * {@inheritdoc}
  */
 public function current()
 {
     if (!isset($this->layers[$this->offset])) {
         try {
             $this->resource->setimageindex($this->offset);
             $this->layers[$this->offset] = $this->resource->getimage();
         } catch (\GmagickException $e) {
             throw new RuntimeException(sprintf('Failed to extract layer %d', $this->offset), $e->getCode(), $e);
         }
     }
     return new Image($this->layers[$this->offset]);
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function getSize()
 {
     try {
         $i = $this->gmagick->getimageindex();
         $this->gmagick->setimageindex(0);
         //rewind
         $width = $this->gmagick->getimagewidth();
         $height = $this->gmagick->getimageheight();
         $this->gmagick->setimageindex($i);
     } catch (\GmagickException $e) {
         throw new RuntimeException('Get size operation failed', $e->getCode(), $e);
     }
     return new Box($width, $height);
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function offsetUnset($offset)
 {
     try {
         $this->extractAt($offset);
     } catch (RuntimeException $e) {
         return;
     }
     try {
         $this->resource->setimageindex($offset);
         $this->resource->removeimage();
     } catch (\GmagickException $e) {
         throw new RuntimeException('Unable to remove layer', $e->getCode(), $e);
     }
 }
示例#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
 public function read($ps_filepath, $mimetype = "")
 {
     if (!($this->handle && ${$ps_filepath} === $this->filepath)) {
         if ($mimetype == 'image/tilepic') {
             #
             # Read in Tilepic format image
             #
             $this->handle = new TilepicParser($ps_filepath);
             if (!$this->handle->error) {
                 $this->filepath = $ps_filepath;
                 foreach ($this->handle->properties as $k => $v) {
                     if (isset($this->properties[$k])) {
                         $this->properties[$k] = $v;
                     }
                 }
                 $this->properties["mimetype"] = "image/tilepic";
                 $this->properties["typename"] = "Tilepic";
                 return 1;
             } else {
                 $this->postError(1610, $this->handle->error, "WLPlugGmagick->read()");
                 return false;
             }
         } else {
             $this->handle = "";
             $this->filepath = "";
             if ($mimetype == 'image/x-dcraw') {
                 if ($this->filepath_conv) {
                     @unlink($this->filepath_conv);
                 }
                 if (!caMediaPluginDcrawInstalled($this->ops_dcraw_path)) {
                     $this->postError(1610, _t("Could not convert Camera RAW format file because conversion tool (dcraw) is not installed"), "WLPlugGmagick->read()");
                     return false;
                 }
                 $vs_tmp_name = tempnam(caGetTempDirPath(), "rawtmp");
                 if (!copy($ps_filepath, $vs_tmp_name)) {
                     $this->postError(1610, _t("Could not copy Camera RAW file to temporary directory"), "WLPlugGmagick->read()");
                     return false;
                 }
                 exec($this->ops_dcraw_path . " -T " . caEscapeShellArg($vs_tmp_name), $va_output, $vn_return);
                 if ($vn_return != 0) {
                     $this->postError(1610, _t("Camera RAW file conversion failed: %1", $vn_return), "WLPlugGmagick->read()");
                     return false;
                 }
                 if (!(file_exists($vs_tmp_name . '.tiff') && filesize($vs_tmp_name . '.tiff') > 0)) {
                     $this->postError(1610, _t("Translation from Camera RAW to TIFF failed"), "WLPlugGmagick->read()");
                     return false;
                 }
                 $ps_filepath = $this->filepath_conv = $vs_tmp_name . '.tiff';
                 @unlink($vs_tmp_name);
             }
             try {
                 $handle = new Gmagick($ps_filepath);
                 $handle->setimageindex(0);
                 // force use of first image in multi-page TIFF
                 $this->handle = $handle;
                 $this->filepath = $ps_filepath;
                 $this->metadata = array();
                 // exif
                 if (function_exists('exif_read_data')) {
                     if (is_array($va_exif = caSanitizeArray(@exif_read_data($ps_filepath, 'EXIF', true, false)))) {
                         //
                         // Rotate incoming image as needed
                         //
                         if (isset($va_exif['IFD0']['Orientation'])) {
                             $vn_orientation = $va_exif['IFD0']['Orientation'];
                             $vs_tmp_basename = tempnam(caGetTempDirPath(), 'ca_image_tmp');
                             $vb_is_rotated = false;
                             switch ($vn_orientation) {
                                 case 3:
                                     $this->handle->rotateimage("#FFFFFF", 180);
                                     unset($va_exif['IFD0']['Orientation']);
                                     $vb_is_rotated = true;
                                     break;
                                 case 6:
                                     $this->handle->rotateimage("#FFFFFF", 90);
                                     unset($va_exif['IFD0']['Orientation']);
                                     $vb_is_rotated = true;
                                     break;
                                 case 8:
                                     $this->handle->rotateimage("#FFFFFF", -90);
                                     unset($va_exif['IFD0']['Orientation']);
                                     $vb_is_rotated = true;
                                     break;
                             }
                             if ($vb_is_rotated) {
                                 if ($this->handle->writeimage($vs_tmp_basename)) {
                                     $va_tmp = $this->handle->getimagegeometry();
                                     $this->properties["faces"] = $this->opa_faces = caDetectFaces($vs_tmp_basename, $va_tmp['width'], $va_tmp['height']);
                                 }
                                 @unlink($vs_tmp_basename);
                             }
                         }
                         $this->metadata['EXIF'] = $va_exif;
                     }
                 }
                 // XMP
                 $o_xmp = new XMPParser();
                 if ($o_xmp->parse($ps_filepath)) {
                     if (is_array($va_xmp_metadata = $o_xmp->getMetadata()) && sizeof($va_xmp_metadata)) {
                         $this->metadata['XMP'] = $va_xmp_metadata;
                     }
                 }
                 # load image properties
                 $va_tmp = $this->handle->getimagegeometry();
                 $this->properties["width"] = $va_tmp['width'];
                 $this->properties["height"] = $va_tmp['height'];
                 $this->properties["quality"] = "";
                 $this->properties["filesize"] = filesize($ps_filepath);
                 $this->properties["bitdepth"] = $this->handle->getimagedepth();
                 $this->properties["resolution"] = $this->handle->getimageresolution();
                 $this->properties["colorspace"] = $this->_getColorspaceAsString($this->handle->getimagecolorspace());
                 // force all images to true color (takes care of GIF transparency for one thing...)
                 $this->handle->setimagetype(Gmagick::IMGTYPE_TRUECOLOR);
                 if (!$this->handle->setimagecolorspace(Gmagick::COLORSPACE_RGB)) {
                     $this->postError(1610, _t("Error during RGB colorspace transformation operation"), "WLPlugGmagick->read()");
                     return false;
                 }
                 if (!$this->properties["faces"]) {
                     $this->properties["faces"] = $this->opa_faces = caDetectFaces($ps_filepath, $va_tmp['width'], $va_tmp['height']);
                 }
                 $this->properties["mimetype"] = $this->_getMagickImageMimeType($this->handle);
                 $this->properties["typename"] = $this->handle->getimageformat();
                 $this->ohandle = clone $this->handle;
                 return 1;
             } catch (Exception $e) {
                 $this->postError(1610, _t("Could not read image file"), "WLPlugGmagick->read()");
                 return false;
             }
         }
     } else {
         # image already loaded by previous call (probably divineFileFormat())
         return 1;
     }
 }