protected function executeExecImagick() { // Step 1. Generate the watermark $op = ''; // store original values $fileType = $this->outputType; $targetFile = $this->targetFile; $this->outputAlpha = true; if ($this->type == 'text') { $this->outputType = 'png'; $this->targetFile = FileSystemUtils::secureTmpname($this->workDir, 'watermark', '.' . $this->outputType); $op .= " -background transparent"; $op .= " -fill '{$this->fontColor}'"; if (!empty($this->fontFamily)) { $op .= " -font '{$this->fontFamily}'"; } if (!empty($this->fontStyle)) { $op .= " -style '{$this->fontStyle}'"; } if (!empty($this->fontWeight)) { $op .= " -weight '{$this->fontWeight}'"; } $op .= " -size {$this->width}x{$this->height}"; if (!empty($this->fontSize)) { $op .= " -pointsize {$this->fontSize}"; } $op .= " caption:\"" . preg_replace('/"/', '\\"', $this->text) . "\""; $watermark = ImagickExecFilterHelper::executeFilter($this, $op, true); } else { if ($this->type == 'image') { $watermark = $this->image; } } // Step 2. Apply the watermark // restore original values $this->outputType = $fileType; $this->targetFile = $targetFile; /** This only works with IM > 6.5.3-4 **/ /*$op = "\"{$watermark}\""; $op .= " -compose dissolve"; $op .= " -define compose:args={$this->opacity}"; $op .= " -gravity {$this->anchor}"; $op .= " -geometry {$this->offsetX}{$this->offsetY}"; $op .= " -composite"; $image = ImagickExecFilterHelper::executeFilter($this, $op);*/ $inFile = escapeshellarg($this->sourceFile); $outFile = escapeshellarg($this->targetFile); $op = " -dissolve {$this->opacity}"; $op .= " -gravity {$this->anchor}"; $op .= " -geometry {$this->offsetX}{$this->offsetY}"; $op .= " \"{$watermark}\""; $op .= " {$inFile}"; if ($this->outputAlpha != null) { if ($this->outputAlpha == true) { $op .= " -alpha On"; } else { $op .= " -alpha Off"; } } $op .= " -quality {$this->outputQuality}"; $cmdParams = "{$op} {$outFile}"; $cmd = "{$this->imageCompositeExecPath} {$cmdParams} 2>&1"; $retval = 1; $output = array(); exec($cmd, $output, $retval); if ($retval > 0) { throw new ImageFilterException("Image filter process failed", $output); } @unlink($watermark); return $this->targetFile; }
protected function executeExecImagick() { // get the width of the original image $retval = 1; $output = array(); exec("{$this->imageIdentifyExecPath} -format %w " . escapeshellarg($this->sourceFile) . " 2>&1", $output, $retval); if ($retval > 0) { throw new ImageFilterException("Unable to read source file."); } $width = $output[0] - $this->padding * 2; // Step 1. Generate the photo credit $op = ''; // store original values $targetFile = $this->targetFile; $this->targetFile = FileSystemUtils::secureTmpname($this->workDir, 'credit', '.' . $this->outputType); $op .= " -background '{$this->backgroundColor}'"; if ($this->fontColor != null) { $op .= " -fill '{$this->fontColor}'"; } if ($this->fontFamily != null) { $op .= " -font '{$this->fontFamily}'"; } if ($this->fontStyle != null) { $op .= " -style '{$this->fontStyle}'"; } if ($this->fontWeight != null) { $op .= " -weight '{$this->fontWeight}'"; } $op .= " -pointsize {$this->fontSize}"; // Force height to fontSize because IM < version 6.5.2-4 will ignore -pointsize to make the text fill the maximum width specified // I think this will limit all credit to 1 line of text $op .= " -size {$width}x{$this->fontSize}"; $op .= " -gravity {$this->anchor}"; $op .= " label:\"" . preg_replace('/"/', '\\"', $this->text) . "\""; $op .= " -bordercolor '{$this->backgroundColor}'"; $op .= " -border {$this->padding}x{$this->padding}"; $credit = ImagickExecFilterHelper::executeFilter($this, $op, true); // Step 2. Apply the photo credit // restore original values $this->targetFile = $targetFile; $op = ''; if ($this->type == 'overlay') { $op .= " -gravity {$this->anchor}"; $op .= " -draw 'image over 0,0 0,0 \"{$credit}\"'"; } else { if ($this->type == 'add') { $op .= " -background '{$this->backgroundColor}'"; $op .= " {$credit}"; if (preg_match('/^North/i', $this->anchor)) { $op .= " +swap"; } if (preg_match('/^(North|South)$/i', $this->anchor)) { $op .= " -gravity Center"; } else { if (preg_match('/East$/i', $this->anchor)) { $op .= " -gravity East"; } } $op .= " -append"; } } return ImagickExecFilterHelper::executeFilter($this, $op); }
protected function _importPhotosFromJson($file) { try { $contents = file_get_contents($file); $json = JSONUtils::decode($contents); foreach ($json as $v) { echo "importing {$v->title}..."; $url = $v->src; $parts = parse_url($url); $slug = SlugUtils::createSlug(basename($parts['path'])); preg_match('/(\\.\\w*)$/', $parts['path'], $ext); $nodeRef = $this->NodeRefService->oneFromAspect('@images'); $nodeRef = $this->NodeRefService->generateNodeRef($nodeRef, $slug); $node = $nodeRef->generateNode(); if (!$this->NodeService->refExists($node->getNodeRef())) { // go fetch file from url $data = $this->HttpRequest->fetchURL($url); // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'urlfetch', !empty($ext[1]) ? strtolower($ext[1]) : null); file_put_contents($sourceFile, $data); $node->Title = rtrim($v->title); $node->Status = "published"; $node->ActiveDate = $this->DateFactory->newStorageDate(); $node = $this->ImageService->storeMedia($sourceFile, $node, basename($parts['path'])); $this->NodeService->add($node); echo "done\n"; } else { echo "exists\n"; } unset($nodeRef); unset($node); } } catch (Exception $e) { echo "Exception: " . $e->getMessage() . "\n"; } }
/** * Checks and prepares the image for use. * * @param $data base64 encoded data string * @return string file path on disk * @throws ImageFilterException */ protected function prepareImageData($data) { if (is_array($data)) { $uploadedFiles = $this->Request->getUploadedFiles(); $sourceFileName = $data['filename']; $filename = null; foreach ($uploadedFiles as $uploadFile) { if ($uploadFile->getName() == $sourceFileName) { $filename = $uploadFile->getTemporaryName(); break; } } if (empty($filename)) { throw new ImageFilterException("File '{$filename}' was not uploaded"); } $file = file_get_contents($filename); unlink($filename); } else { if (preg_match('/^data:/', $data)) { $data = substr($data, strpos($data, ",") + 1); if (!($file = base64_decode($data, true))) { throw new ImageFilterException('Data string is not a valid base64 encoded file.'); } } else { throw new ImageFilterException('No file was provided.'); } } // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'base64fetch'); file_put_contents($sourceFile, $file); return $sourceFile; }
/** * Gets the local file from supplied file node. * * @return array [sourceFile], [sourceFileName] */ protected function getSourceFileFromNode($fileNode) { $element = $fileNode->getNodeRef()->getElement(); list($storageFacility, $sfParams) = $this->deriveStorageFacility($element); $file = $storageFacility->getFile($sfParams, $fileNode->Title, true); $contents = $file->getContents(); // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'sffetch'); file_put_contents($sourceFile, $contents); $sourceFileName = basename($fileNode->Title); return array($sourceFile, $sourceFileName); }