private function _gmagickRead($ps_filepath) { try { $handle = new Gmagick($ps_filepath); $this->setResourceLimits($handle); $handle->setimageindex(0); // force use of first image in multi-page TIFF $this->handle = $handle; $this->filepath = $ps_filepath; $this->metadata = array(); // handle metadata /* EXIF */ if (function_exists('exif_read_data') && !$this->opo_config->get('dont_use_exif_read_data')) { if (is_array($va_exif = caSanitizeArray(@exif_read_data($ps_filepath, 'EXIF', true, false)))) { $va_metadata['EXIF'] = $va_exif; } } // if the builtin EXIF extraction is not used or failed for some reason, try ExifTool if (!isset($va_metadata['EXIF']) || !is_array($va_metadata['EXIF'])) { if (caExifToolInstalled()) { $va_metadata['EXIF'] = caExtractMetadataWithExifTool($ps_filepath, true); } } // Rotate incoming image as needed if (isset($va_metadata['EXIF']['IFD0']['Orientation'])) { $vn_orientation = $va_metadata['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_metadata['EXIF']['IFD0']['Orientation']); $vb_is_rotated = true; break; case 6: $this->handle->rotateimage("#FFFFFF", 90); unset($va_metadata['EXIF']['IFD0']['Orientation']); $vb_is_rotated = true; break; case 8: $this->handle->rotateimage("#FFFFFF", -90); unset($va_metadata['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); } } // get XMP $o_xmp = new XMPParser(); if ($o_xmp->parse($ps_filepath)) { if (is_array($va_xmp_metadata = $o_xmp->getMetadata()) && sizeof($va_xmp_metadata)) { $va_metadata['XMP'] = array(); foreach ($va_xmp_metadata as $vs_xmp_tag => $va_xmp_values) { $va_metadata['XMP'][$vs_xmp_tag] = join('; ', $va_xmp_values); } } } // try to get IPTC and DPX with GraphicsMagick, if available if (caMediaPluginGraphicsMagickInstalled()) { /* IPTC metadata */ $vs_iptc_file = tempnam(caGetTempDirPath(), 'gmiptc'); @rename($vs_iptc_file, $vs_iptc_file . '.iptc'); // GM uses the file extension to figure out what we want $vs_iptc_file .= '.iptc'; exec($this->ops_graphicsmagick_path . " convert " . caEscapeShellArg($ps_filepath) . " " . caEscapeShellArg($vs_iptc_file) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); $vs_iptc_data = file_get_contents($vs_iptc_file); @unlink($vs_iptc_file); $va_iptc_raw = iptcparse($vs_iptc_data); $va_iptc_tags = array('2#004' => 'Genre', '2#005' => 'DocumentTitle', '2#010' => 'Urgency', '2#015' => 'Category', '2#020' => 'Subcategories', '2#025' => 'Keywords', '2#040' => 'SpecialInstructions', '2#055' => 'CreationDate', '2#060' => 'TimeCreated', '2#080' => 'AuthorByline', '2#085' => 'AuthorTitle', '2#090' => 'City', '2#095' => 'State', '2#100' => 'CountryCode', '2#101' => 'Country', '2#103' => 'OTR', '2#105' => 'Headline', '2#110' => 'Credit', '2#115' => 'PhotoSource', '2#116' => 'Copyright', '2#120' => 'Caption', '2#122' => 'CaptionWriter'); $va_iptc = array(); if (is_array($va_iptc_raw)) { foreach ($va_iptc_raw as $vs_iptc_tag => $va_iptc_tag_data) { if (isset($va_iptc_tags[$vs_iptc_tag])) { $va_iptc[$va_iptc_tags[$vs_iptc_tag]] = join('; ', $va_iptc_tag_data); } } } if (sizeof($va_iptc)) { $va_metadata['IPTC'] = $va_iptc; } /* DPX metadata */ exec($this->ops_graphicsmagick_path . " identify -format '%[DPX:*]' " . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); if ($va_output[0]) { $va_metadata['DPX'] = $va_output; } } $this->metadata = $va_metadata; return $handle; } catch (Exception $e) { $this->postError(1610, _t("Could not read image file"), "WLPlugGmagick->read()"); return false; // gmagick couldn't read file, presumably } }
/** * */ public function writeClip($ps_filepath, $ps_start, $ps_end, $pa_options = null) { if (!$this->opb_ffmpeg_available) { return false; } $o_tc = new TimecodeParser(); $vn_start = $vn_end = null; if ($o_tc->parse($ps_start)) { $vn_start = $o_tc->getSeconds(); } if ($o_tc->parse($ps_end)) { $vn_end = $o_tc->getSeconds(); } if (!$vn_start || !$vn_end) { return null; } if ($vn_start >= $vn_end) { return null; } $vn_duration = $vn_end - $vn_start; exec($this->ops_path_to_ffmpeg . " -i " . caEscapeShellArg($this->filepath) . " -f mp4 -vcodec libx264 -acodec mp3 -t {$vn_duration} -y -ss {$vn_start} " . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); if ($vn_return != 0) { @unlink($ps_filepath); $this->postError(1610, _t("Error extracting clip from %1 to %2: %3", $ps_start, $ps_end, join("; ", $va_output)), "WLPlugVideo->writeClip()"); return false; } return true; }
private function _CoreImageRead($ps_filepath) { if (caMediaPluginCoreImageInstalled($this->ops_CoreImage_path)) { $vs_output = shell_exec('sips --getProperty format --getProperty space --getProperty bitsPerSample --getProperty pixelWidth --getProperty pixelHeight --getProperty dpiWidth --getProperty dpiHeight ' . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : "")); $va_tmp = explode("\n", $vs_output); array_shift($va_tmp); $va_properties = array(); foreach ($va_tmp as $vs_line) { $va_line_tmp = explode(':', $vs_line); $va_properties[trim($va_line_tmp[0])] = trim($va_line_tmp[1]); } return array('mimetype' => $this->appleTypeToMimeType($va_properties['format']), 'magick' => $va_properties['format'], 'width' => $va_properties['pixelWidth'], 'height' => $va_properties['pixelHeight'], 'ops' => array(), 'filepath' => $ps_filepath); } return null; }
private function _graphicsMagickWrite($pa_handle, $ps_filepath, $ps_mimetype, $pn_quality = null) { if (caMediaPluginGraphicsMagickInstalled($this->ops_graphicsmagick_path)) { $va_ops = array(); foreach ($pa_handle['ops'] as $va_op) { switch ($va_op['op']) { case 'strip': // there is no such thing in graphicsmagick // $va_ops['convert'][] = "-strip"; $va_ops['convert'][] = '+profile "*"'; break; case 'annotation': $vs_op = '-gravity ' . $va_op['position'] . ' -fill ' . str_replace('#', '\\#', $va_op['color']) . ' -pointsize ' . $va_op['size'] . ' -draw "text ' . $va_op['inset'] . ',' . $va_op['inset'] . ' \'' . $va_op['text'] . '\'"'; if ($va_op['font']) { $vs_op .= ' -font ' . $va_op['font']; } $va_ops['convert'][] = $vs_op; break; case 'watermark': $vs_op = "-dissolve " . $va_op['opacity'] * 100 . " -gravity " . $va_op['position'] . " " . $va_op['watermark_image']; //" -geometry ".$va_op['watermark_width']."x".$va_op['watermark_height']; [Seems to be interpreted as scaling the image being composited on as of at least v6.5.9; so we don't scale watermarks in ImageMagick... we just use the native size] $va_ops['composite'][] = $vs_op; break; case 'size': if ($va_op['width'] < 1) { break; } if ($va_op['height'] < 1) { break; } $va_ops['convert'][] = '-resize ' . $va_op['width'] . 'x' . $va_op['height'] . ' -filter Cubic'; break; case 'crop': if ($va_op['width'] < 1) { break; } if ($va_op['height'] < 1) { break; } if ($va_op['x'] < 0) { break; } if ($va_op['y'] < 0) { break; } $va_ops['convert'][] = '-crop ' . $va_op['width'] . 'x' . $va_op['height'] . '+' . $va_op['x'] . '+' . $va_op['y']; break; case 'rotate': if (!is_numeric($va_op['angle'])) { break; } $va_ops['convert'][] = '-rotate ' . $va_op['angle']; break; case 'filter_despeckle': $va_ops['convert'][] = '-despeckle'; break; case 'filter_sharpen': if ($va_op['radius'] < 0) { break; } $vs_tmp = '-sharpen ' . $va_op['radius']; if (isset($va_op['sigma'])) { $vs_tmp .= 'x' . $va_op['sigma']; } $va_ops['convert'][] = $vs_tmp; break; case 'filter_median': if ($va_op['radius'] < 0) { break; } $va_ops['convert'][] = '-median ' . $va_op['radius']; break; case 'filter_unsharp_mask': if ($va_op['radius'] < 0) { break; } $vs_tmp = '-unsharp ' . $va_op['radius']; if (isset($va_op['sigma'])) { $vs_tmp .= 'x' . $va_op['sigma']; } if (isset($va_op['amount'])) { $vs_tmp .= '+' . $va_op['amount']; } if (isset($va_op['threshold'])) { $vs_tmp .= '+' . $va_op['threshold']; } $va_ops['convert'][] = $vs_tmp; break; } } if (isset($this->properties["orientation_rotate"]) && $this->properties["orientation_rotate"] != 0) { $va_ops['convert'][] = '-rotate ' . $this->properties["orientation_rotate"]; } if ($this->properties['gamma']) { if (!$this->properties['reference-black']) { $this->properties['reference-black'] = 0; } if (!$this->properties['reference-white']) { $this->properties['reference-white'] = 65535; } $va_ops['convert'][] = "-set gamma " . $this->properties['gamma']; $va_ops['convert'][] = "-set reference-black " . $this->properties['reference-black']; $va_ops['convert'][] = "-set reference-white " . $this->properties['reference-white']; } $vs_input_file = $pa_handle['filepath']; if (is_array($va_ops['convert']) && sizeof($va_ops['convert'])) { if (!is_null($pn_quality)) { array_unshift($va_ops['convert'], '-quality ' . intval($pn_quality)); } array_unshift($va_ops['convert'], '-colorspace RGB'); exec($this->ops_graphicsmagick_path . ' convert ' . caEscapeShellArg($vs_input_file . '[0]') . ' ' . join(' ', $va_ops['convert']) . ' ' . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : "")); $vs_input_file = $ps_filepath; } if (is_array($va_ops['composite']) && sizeof($va_ops['composite'])) { exec($this->ops_graphicsmagick_path . ' composite ' . join(' ', $va_ops['composite']) . ' ' . caEscapeShellArg($vs_input_file . '[0]') . ' ' . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : "")); } return true; } return null; }
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, "WLPlugImagick->read()"); return false; } } else { $this->handle = ""; $this->filepath = ""; $handle = new Imagick(); $this->setResourceLimits($handle); 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"), "WLPlugImagick->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"), "WLPlugImagick->read()"); return false; } exec($this->ops_dcraw_path . " -T " . caEscapeShellArg($vs_tmp_name) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); if ($vn_return != 0) { $this->postError(1610, _t("Camera RAW file conversion failed: %1", $vn_return), "WLPlugImagick->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"), "WLPlugImagick->read()"); return false; } $ps_filepath = $this->filepath_conv = $vs_tmp_name . '.tiff'; @unlink($vs_tmp_name); } if ($handle->readImage($ps_filepath)) { $this->handle = $handle; $this->filepath = $ps_filepath; $va_raw_metadata = $this->handle->getImageProperties(); $this->metadata = array(); foreach ($va_raw_metadata as $vs_tag => $vs_value) { if (sizeof($va_tmp = explode(':', $vs_tag)) > 1) { $vs_type = strtoupper($va_tmp[0]); $vs_tag = $va_tmp[1]; } else { $vs_type = 'GENERIC'; } if ($vs_type == 'EXIF') { continue; } $this->metadata[$vs_type][$vs_tag] = $vs_value; } // exif if (function_exists('exif_read_data') && !$this->opo_config->get('dont_use_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"] = $this->handle->getImageLength(); $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(imagick::IMGTYPE_TRUECOLOR); if (!$this->handle->setImageColorspace(imagick::COLORSPACE_RGB)) { $this->postError(1610, _t("Error during RGB colorspace transformation operation"), "WLPlugImagick->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 = $this->handle->clone(); return 1; } else { $this->postError(1610, _t("Could not read image file"), "WLPlugImagick->read()"); return false; } } } else { # image already loaded by previous call (probably divineFileFormat()) return 1; } }
public function &writePreviews($ps_filepath, $pa_options) { if (!(bool) $this->opo_config->get("video_preview_generate_frames")) { return false; } if (!isset($pa_options['outputDirectory']) || !$pa_options['outputDirectory'] || !file_exists($pa_options['outputDirectory'])) { if (!($vs_tmp_dir = $this->opo_config->get("taskqueue_tmp_directory"))) { // no dir return false; } } else { $vs_tmp_dir = $pa_options['outputDirectory']; } $o_tc = new TimecodeParser(); if (($vn_min_number_of_frames = $pa_options['minNumberOfFrames']) < 1) { $vn_min_number_of_frames = 0; } if (($vn_max_number_of_frames = $pa_options['maxNumberOfFrames']) < 1) { $vn_max_number_of_frames = 100; } $vn_duration = $this->properties["duration"]; if (!($vn_frame_interval = $o_tc->parse($pa_options['frameInterval']) ? $o_tc->getSeconds() : 0)) { $vn_frame_interval = 30; } if (!($vn_start_at = $o_tc->parse($pa_options['startAtTime']) ? $o_tc->getSeconds() : 0)) { $vn_start_at = 0; } if (!($vn_end_at = $o_tc->parse($pa_options['endAtTime']) ? $o_tc->getSeconds() : 0)) { $vn_end_at = 0; } if (($vn_previewed_duration = $vn_duration - $vn_start_at - $vn_end_at) < 0) { $vn_previewed_duration = $vn_duration; $vn_start_at = $vn_end_at = 0; } if ($vn_frame_interval > $vn_previewed_duration) { $vn_frame_interval = $vn_previewed_duration; } $vn_preview_width = isset($pa_options['width']) && (int) $pa_options['width'] > 0 ? (int) $pa_options['width'] : 320; $vn_preview_height = isset($pa_options['height']) && (int) $pa_options['height'] > 0 ? (int) $pa_options['height'] : 320; $vn_s = $vn_start_at; $vn_e = $vn_duration - $vn_end_at; $vn_num_frames = $vn_previewed_duration / $vn_frame_interval; if ($vn_num_frames < $vn_min_number_of_frames) { $vn_frame_interval = $vn_previewed_duration / $vn_min_number_of_frames; $vn_num_frames = $vn_min_number_of_frames; $vn_previewed_duration = $vn_num_frames * $vn_frame_interval; } if ($vn_num_frames > $vn_max_number_of_frames) { $vn_frame_interval = $vn_previewed_duration / $vn_max_number_of_frames; $vn_num_frames = $vn_max_number_of_frames; $vn_previewed_duration = $vn_num_frames * $vn_frame_interval; } $vs_freq = 1 / $vn_frame_interval; $vs_output_file_prefix = tempnam($vs_tmp_dir, 'caQuicktimeVRPreview'); $vs_output_file = $vs_output_file_prefix . '%05d.jpg'; exec($this->ops_path_to_ffmpeg . " -i " . caEscapeShellArg($this->filepath) . " -f image2 -r " . $vs_freq . " -ss {$vn_s} -t {$vn_previewed_duration} -s " . $vn_preview_width . "x" . $vn_preview_height . " -y " . caEscapeShellArg($vs_output_file) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); $vn_i = 1; $va_files = array(); while (file_exists($vs_output_file_prefix . sprintf("%05d", $vn_i) . '.jpg')) { // add frame to list $va_files['' . sprintf("%4.2f", ($vn_i - 1) * $vn_frame_interval + $vn_s) . 's'] = $vs_output_file_prefix . sprintf("%05d", $vn_i) . '.jpg'; $vn_i++; } if (!sizeof($va_files)) { $this->postError(1610, _t("Couldn't not write video preview frames to tmp directory (%1)", $vs_tmp_dir), "WLPlugQuicktimeVR->write()"); } @unlink($vs_output_file_prefix); return $va_files; }
/** * @param array $pa_options Options include: * dontUseDefaultIcons = If set to true, write will fail rather than use default icons when preview can't be generated. Default is false – to use default icons. * */ public function write($ps_filepath, $ps_mimetype, $pa_options = null) { if (!$this->handle) { return false; } $vb_dont_allow_default_icons = isset($pa_options['dontUseDefaultIcons']) && $pa_options['dontUseDefaultIcons'] ? true : false; # is mimetype valid? if (!($vs_ext = $this->info["EXPORT"][$ps_mimetype])) { $this->postError(1610, _t("Can't convert file to %1", $ps_mimetype), "WLPlugPDFWand->write()"); return false; } # write the file if ($ps_mimetype == "application/pdf") { if (!copy($this->filepath, $ps_filepath . ".pdf")) { $this->postError(1610, _t("Couldn't write file to '%1'", $ps_filepath), "WLPlugPDFWand->write()"); return false; } } else { $vb_use_default_icon = true; if (caMediaPluginGhostscriptInstalled($this->ops_ghostscript_path)) { $vn_scaling_correction = (double) $this->get("scaling_correction"); if ($vn_scaling_correction == 1) { $vn_scaling_correction = 0; } $vs_res = "72x72"; if (ceil($this->get("resolution")) > 0) { $vn_res = $this->get("resolution"); if ($vn_scaling_correction) { $vn_res *= 2; } $vs_res = ceil($vn_res) . "x" . ceil($vn_res); } $vn_page = ceil($this->get("page")); $vn_quality = ceil($this->get("quality")); if ($vn_quality > 100) { $vn_quality = 100; } if ($vn_quality < 1) { $vn_quality = 50; } if ($vn_page < 1) { $vn_page = 1; } if ($this->get("antialiasing")) { $vs_antialiasing = "-dTextAlphaBits=4 -dGraphicsAlphaBits=4"; } else { $vs_antialiasing = ""; } $vb_processed_preview = false; switch ($ps_mimetype) { case 'image/jpeg': exec($this->ops_ghostscript_path . " -dNOPAUSE -dBATCH -sDEVICE=" . ($vn_scaling_correction ? "tiff24nc" : "jpeg") . " {$vs_antialiasing} -dJPEGQ=" . $vn_quality . " -dFirstPage=" . $vn_page . " -dLastPage=" . $vn_page . " -sOutputFile=" . caEscapeShellArg($ps_filepath . "." . $vs_ext) . " -r" . $vs_res . " " . caEscapeShellArg($this->handle["filepath"]) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); if ($vn_return == 0) { $vb_processed_preview = true; } break; case 'image/png': exec($this->ops_ghostscript_path . " -dNOPAUSE -dBATCH -sDEVICE=pngalpha {$vs_antialiasing} -dFirstPage=" . $vn_page . " -dLastPage=" . $vn_page . " -sOutputFile=" . caEscapeShellArg($ps_filepath . "." . $vs_ext) . " -r" . $vs_res . " " . caEscapeShellArg($this->handle["filepath"]) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); if ($vn_return == 0) { $vb_processed_preview = true; } break; case 'image/tiff': case 'image/gif': exec($this->ops_ghostscript_path . " -dNOPAUSE -dBATCH -sDEVICE=tiff24nc {$vs_antialiasing} -dFirstPage=" . $vn_page . " -dLastPage=" . $vn_page . " -sOutputFile=" . caEscapeShellArg($ps_filepath . "." . $vs_ext) . " -r" . $vs_res . " " . caEscapeShellArg($this->handle["filepath"]) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); if ($vn_return == 0) { $vb_processed_preview = true; } break; default: //die("Unsupported output type in PDF plug-in: $ps_mimetype [this shouldn't happen]"); break; } if ($vb_processed_preview) { if ($vs_crop = $this->get("crop")) { $o_media = new Media(); list($vn_w, $vn_h) = explode("x", $vs_crop); if ($vn_w > 0 && $vn_h > 0) { $o_media->read($ps_filepath . "." . $vs_ext); if (!$o_media->numErrors()) { $o_media->transform('SCALE', array('mode' => 'fill_box', 'antialiasing' => 0.5, 'width' => $vn_w, 'height' => $vn_h)); $o_media->write($ps_filepath, $ps_mimetype, array()); if (!$o_media->numErrors()) { $this->properties["width"] = $vn_w; $this->properties["height"] = $vn_h; $vb_use_default_icon = false; } } } } else { if ($vn_scaling_correction) { $o_media = new Media(true); $o_media->read($ps_filepath . "." . $vs_ext); if (!$o_media->numErrors()) { $vn_w = $o_media->get('width') * $vn_scaling_correction; $vn_h = $o_media->get('height') * $vn_scaling_correction; if ($vn_w > $vn_h || $this->get("target_height") == 0) { $vn_r = $this->get("target_width") / $vn_w; $vn_w = $this->get("target_width"); $vn_h *= $vn_r; } else { $vn_r = $this->get("target_height") / $vn_h; $vn_h = $this->get("target_height"); $vn_w *= $vn_r; } $vn_w = ceil($vn_w); $vn_h = ceil($vn_h); $this->properties["width"] = $vn_w; $this->properties["height"] = $vn_h; $o_media->transform('SCALE', array('mode' => 'bounding_box', 'antialiasing' => 0.5, 'width' => $vn_w, 'height' => $vn_h)); $o_media->transform('UNSHARPEN_MASK', array('sigma' => 0.5, 'radius' => 1, 'threshold' => 1.0, 'amount' => 0.1)); $o_media->set('quality', $vn_quality); $o_media->write($ps_filepath, $ps_mimetype, array()); if (!$o_media->numErrors()) { $vb_use_default_icon = false; } } } else { $vb_use_default_icon = false; } } } } if ($vb_use_default_icon) { return $vb_dont_allow_default_icons ? null : __CA_MEDIA_DOCUMENT_DEFAULT_ICON__; } } $this->properties["mimetype"] = $ps_mimetype; $this->properties["filesize"] = filesize($ps_filepath . "." . $vs_ext); $this->properties["typename"] = $this->typenames[$ps_mimetype]; return $ps_filepath . "." . $vs_ext; }
/** * @param array $pa_options Options include: * dontUseDefaultIcons = If set to true, write will fail rather than use default icons when preview can't be generated. Default is false – to use default icons. * */ public function write($ps_filepath, $ps_mimetype, $pa_options = null) { if (!$this->handle) { return false; } $vb_dont_allow_default_icons = isset($pa_options['dontUseDefaultIcons']) && $pa_options['dontUseDefaultIcons'] ? true : false; # is mimetype valid? if (!($vs_ext = $this->info["EXPORT"][$ps_mimetype])) { $this->postError(1610, _t("Can't convert file to %1", $ps_mimetype), "WLPlugMediaOffice->write()"); return false; } # write the file if ($ps_mimetype == "application/msword") { if (!copy($this->filepath, $ps_filepath . ".doc")) { $this->postError(1610, _t("Couldn't write file to '%1'", $ps_filepath), "WLPlugMediaOffice->write()"); return false; } } else { if (!isset(WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath]) && $this->opb_libre_office_installed) { $vs_tmp_dir_path = caGetTempDirPath(); $va_tmp = explode("/", $this->filepath); $vs_out_file = array_pop($va_tmp); putenv("HOME={$vs_tmp_dir_path}"); // libreoffice will fail silently if you don't set this environment variable to a directory it can write to. Nice way to waste a day debugging. Yay! exec($this->ops_libreoffice_path . " --nologo --nofirststartwizard --headless -convert-to pdf " . caEscapeShellArg($this->filepath) . " -outdir " . caEscapeShellArg($vs_tmp_dir_path) . (caIsPOSIX() ? " 2>&1" : ""), $va_output, $vn_return); exec($this->ops_libreoffice_path . " --nologo --nofirststartwizard --headless -convert-to html " . caEscapeShellArg($this->filepath) . " -outdir " . caEscapeShellArg($vs_tmp_dir_path) . (caIsPOSIX() ? " 2>&1" : ""), $va_output, $vn_return); $va_out_file = explode(".", $vs_out_file); if (sizeof($va_out_file) > 1) { array_pop($va_out_file); } $this->handle['content'] = file_exists("{$vs_tmp_dir_path}/" . join(".", $va_out_file) . ".html") ? strip_tags(file_get_contents("{$vs_tmp_dir_path}/" . join(".", $va_out_file) . ".html")) : ''; $va_out_file[] = 'pdf'; WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath] = "{$vs_tmp_dir_path}/" . join(".", $va_out_file); $o_media = new Media(); if ($o_media->read(WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath])) { $this->set('width', $this->ohandle["width"] = $o_media->get('width')); $this->set('height', $this->ohandle["height"] = $o_media->get('height')); $this->set('resolution', $this->ohandle["resolution"] = $o_media->get('resolution')); } } if ($vs_media = WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath]) { switch ($ps_mimetype) { case 'application/pdf': $o_media = new Media(); $o_media->read($vs_media); $o_media->set('version', $this->get('version')); $o_media->write($ps_filepath, $ps_mimetype, array()); $vs_filepath_with_extension = $ps_filepath . ".pdf"; break; case 'image/jpeg': $o_media = new Media(); $o_media->read($vs_media); $o_media->set('version', $this->get('version')); foreach ($this->opa_transformations as $va_transform) { $o_media->transform($va_transform['op'], $va_transform['params']); } $o_media->write($ps_filepath, $ps_mimetype, array()); $this->set('width', $o_media->get('width')); $this->set('height', $o_media->get('height')); $vs_filepath_with_extension = $ps_filepath . ".jpg"; break; } } # use default media icons if (!file_exists($vs_filepath_with_extension)) { // always jpegs return $vb_dont_allow_default_icons ? null : __CA_MEDIA_DOCUMENT_DEFAULT_ICON__; } } $this->properties["mimetype"] = $ps_mimetype; $this->properties["filesize"] = filesize($ps_filepath . "." . $vs_ext); //$this->properties["typename"] = $this->typenames[$ps_mimetype]; return $ps_filepath . "." . $vs_ext; }
private function _graphicsMagickRead($ps_filepath) { if (caMediaPluginGraphicsMagickInstalled($this->ops_graphicsmagick_path)) { exec($this->ops_graphicsmagick_path . ' identify -format "%m;%w;%h\\n" ' . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return); $va_tmp = explode(';', $va_output[0]); if (sizeof($va_tmp) != 3) { return null; } return array('mimetype' => $this->magickToMimeType($va_tmp[0]), 'magick' => $va_tmp[0], 'width' => $va_tmp[1], 'height' => $va_tmp[2], 'ops' => array(), 'filepath' => $ps_filepath); } return null; }