/** * 初始化imagick对象 * * @param $filepath string 文件地址 * @param $isBinary boolean 是否是二进制内容或文件地址 */ public function __construct($filepath, $isBinary = FALSE) { parent::__construct(); $this->image = new \Imagick(); try { // imagick::readImage 在win下5.3版本居然不能直接用。改为readImageBlob可用。 $this->image->readImageBlob($isBinary ? $filepath : file_get_contents($filepath)); $this->originalFileSize = $this->image->getimagesize(); } catch (\ImagickException $e) { throw new MediaException($e->getMessage()); } }
public function pdfAction() { $model = $this->_processSvg(); $scale = $this->getRequest()->getPost('print_scale'); $model->addWhiteFontForPDF(); $data = $model->normalizeSvgData(); if ($model->getAdditionalData('order_increment_id')) { $filename = 'Order_' . $model->getAdditionalData('order_increment_id') . '_Image.pdf'; } else { $filename = 'Customer_Product_Image.pdf'; } $this->getResponse()->setHttpResponseCode(200)->setHeader('Pragma', 'public', true)->setHeader('Content-type', 'application/pdf', true)->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"', true); $this->getResponse()->clearBody(); //$this->getResponse()->setBody(str_replace('xlink:','',$data)); $imagick = new Imagick(); $imagick->readImageBlob($data); if ($scale != 1 && $scale <= 15) { $imagick->scaleImage($scale * $imagick->getImageWidth(), $scale * $imagick->getImageHeight()); } $imagick->setImageFormat("pdf"); /*$imagick->writeImage(MAGENTO_ROOT.'/media/us-map.pdf');scaleImage */ $this->getResponse()->setBody($imagick); $imagick->clear(); $imagick->destroy(); }
/** * (non-PHPdoc) * @see Imagine\ImagineInterface::load() */ public function load($string) { try { $imagick = new \Imagick(); $imagick->readImageBlob($string); $imagick->setImageMatte(true); return new Image($imagick); } catch (\ImagickException $e) { throw new RuntimeException('Could not load image from string', $e->getCode(), $e); } }
public function load($bytes) { $this->image = new $this->image_class(); $this->image->readImageBlob($bytes); $this->update_size($this->image->getImageWidth(), $this->image->getImageHeight(), true); return $this; }
/** * @param mixed $image * @param string $name * * @throws ProcessorException * * @returns $this */ public function readBinary($image, $name) { $this->initialize(true); try { $this->im->readImageBlob($image, $name); } catch (\Exception $e) { throw new ProcessorException('Could not read binary image %s: %s', null, $e, (string) $name, (string) $e->getMessage()); } return $this; }
public function post() { if ($this->validate()) { $image = new PhotoActiveRecord(); $image->userId = Yii::$app->user->identity->id; $image->name = $this->name . ''; $image->photo = file_get_contents($this->file->tempName); $image->posted = date('Y-m-d H-i-s'); $imagick = new \Imagick(); $imagick->readImageBlob($image->photo); $size = $imagick->getImageGeometry(); if ($size['width'] > 800) { foreach ($imagick as $frame) { $frame->thumbnailImage(800, 0); } } $image->thumbnail = $imagick->getImagesBlob(); if (!$image->save()) { return false; } $tags = split(',', $this->tags); foreach ($tags as $item) { if ($item != '') { $tag = new TagsActiveRecord(); $tag->photo = $image->id; $tag->tag = trim($item); if (!$tag->save()) { return false; } } } return true; } return false; }
public static function get($username) { $lowercase = strtolower($username); $skin = './minecraft/skins/' . $lowercase . '.png'; if (file_exists($skin)) { if (time() - filemtime($skin) < self::$expires) { return $lowercase; } $cached = true; } $binary = self::fetch('http://s3.amazonaws.com/MinecraftSkins/' . $username . '.png'); if ($binary === false) { if ($cached) { return $lowercase; } header('Status: 404 Not Found'); return $username == self::DEFAULT_SKIN ? $lowercase : self::get(self::DEFAULT_SKIN); } $img = new Imagick(); $img->readImageBlob($binary); $img->stripImage(); // strip metadata $img->writeImage($skin); self::_getHelm($img)->writeImage('./minecraft/helms/' . $lowercase . '.png'); self::_getHead($img)->writeImage('./minecraft/heads/' . $lowercase . '.png'); self::_getPlayer($img)->writeImage('./minecraft/players/' . $lowercase . '.png'); return $lowercase; }
function alfath_svg_compiler($options) { global $wp_filesystem; if (!class_exists('Imagick')) { return new WP_Error('class_not_exist', 'Your server not support imagemagick'); } $im = new Imagick(); $im->setBackgroundColor(new ImagickPixel('transparent')); if (empty($wp_filesystem)) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } $file = get_template_directory() . '/assets/img/nav.svg'; $target = get_template_directory() . '/assets/img/nav-bg.png'; if ($wp_filesystem->exists($file)) { //check for existence $svg = $wp_filesystem->get_contents($file); if (!$svg) { return new WP_Error('reading_error', 'Error when reading file'); } //return error object $svg = preg_replace('/fill="#([0-9a-f]{6})"/', 'fill="' . $options['secondary-color'] . '"', $svg); $im->readImageBlob($svg); $im->setImageFormat("png24"); $im->writeImage($target); $im->clear(); $im->destroy(); } else { return new WP_Error('not_found', 'File not found'); } }
/** * {@inheritDoc} */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { try { $svg = new \Imagick(); $svg->setBackgroundColor(new \ImagickPixel('transparent')); $content = stream_get_contents($fileview->fopen($path, 'r')); if (substr($content, 0, 5) !== '<?xml') { $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content; } // Do not parse SVG files with references if (stripos($content, 'xlink:href') !== false) { return false; } $svg->readImageBlob($content); $svg->setImageFormat('png32'); } catch (\Exception $e) { \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR); return false; } //new image object $image = new \OC_Image(); $image->loadFromData($svg); //check if image object is valid if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); return $image; } return false; }
protected function loadSVG($image) { $this->log('Load SVG image', 2); if (!class_exists('Imagick')) { $this->log('Fail', 2); throw new ImageMagickNotAvailableException(); } $content = @file_get_contents($image); if (!is_string($content)) { $this->log('Fail', 2); throw new InvalidImageException(sprintf('Could not load <info>%s</info>, could not read resource.', $image)); } $image = new \Imagick(); $image->readImageBlob($content); $width = $image->getImageWidth(); $height = $image->getImageHeight(); if (310 > $width || 310 > $height) { $this->log('Fail', 2); throw new InvalidImageException(sprintf('Source image must be at least <info>%s</info>,' . ' actual size is: <info>%s</info>', '310x310', $width . 'x' . $height)); } $image->setImageFormat('png24'); $this->gd = @imagecreatefromstring((string) $image); $image->destroy(); if (!is_resource($this->gd)) { $this->log('Fail', 2); throw new InvalidImageException(sprintf('Could not load <info>%s</info>, ImageMagick could not convert' . ' the resource to a valid GD compatible image.', $image)); } $this->log('Success', 2); return $this; }
public function svgToImage($file, $size = array('width' => 100, 'height' => 100), $type = 'png', $fixed = false) { $image = new Imagick(); $image->setBackgroundColor(new ImagickPixel('transparent')); $data = file_get_contents($this->file); $image->readImageBlob($data); $image->setImageFormat($type); if ($fixed === true) { $newWidth = $size['width']; $newHeight = $size['height']; } else { $imageprops = $image->getImageGeometry(); $width = $imageprops['width']; $height = $imageprops['height']; if ($width > $height) { $newHeight = $size['height']; $newWidth = $size['height'] / $height * $width; } else { $newWidth = $size['width']; $newHeight = $size['width'] / $width * $height; } } $image->resizeImage($newWidth, $newHeight, imagick::FILTER_LANCZOS, 1); $image->writeImage(dirname(__FILE__) . '/image.png'); $image->clear(); $image->destroy(); }
/** * Inject the image blob from the image model into the shared imagick instance * * @param EventInterface $event The event instance */ public function readImageBlob(EventInterface $event) { if ($event->hasArgument('image')) { // The image has been specified as an argument to the event $image = $event->getArgument('image'); } else { if ($event->getName() === 'images.post') { // The image is found in the request $image = $event->getRequest()->getImage(); } else { // The image is found in the response $image = $event->getResponse()->getModel(); } } // Inject the image blob $this->imagick->readImageBlob($image->getBlob()); }
public function getPng($resolutionX = 100, $resolutionY = 100) { $img = new \Imagick(); $img->setResolution($resolutionX, $resolutionY); $img->readImageBlob($this->get()); $img->setImageFormat('png'); return $img->getImageBlob(); }
/** * Return image resolution based on the Image object provided. * Please note that this method seems to return the image density, or DPI, * not it's output resolution. * * @param Image $image * * @return ImageDimensions */ protected function getImageResolution(Image $image) { if (!$image->isHydrated()) { throw new ImageManagerException(ImageManager::ERR_NOT_HYDRATED); } $img = new \Imagick(); $img->readImageBlob($image->getData()); $d = $img->getImageResolution(); $dimensions = new ImageDimensions($d['x'], $d['y']); return $dimensions; }
/** * Create a image from a non image file content. (Ex. PDF, PSD or TIFF) * * @param $content * @param string $format * * @return bool|string */ public static function createImageFromNonImage($content, $format = 'jpeg') { if (!extension_loaded('imagick')) { return false; } $image = new \Imagick(); $image->readImageBlob($content); $image->setIteratorIndex(0); $image->setImageFormat($format); return $image->getImageBlob(); }
/** * @return \ImageStorage\Image\Structure\Image * @throws \Exception */ private function _watermark_rand() { if (false === file_exists($this->_watermarkRand->fileName)) { throw new \Exception('File ' . $this->_watermarkRand->fileName . ' not exist!'); } $config = $this->_watermarkRand; if (false === class_exists('\\Imagick')) { return $this->_imageStruct; } //prepare source in Imagick $source = new \Imagick(); $source->readImageBlob($this->getJpgBlobFromGd($this->_imageStruct->image)); //prepare watermark $watermark = new \Imagick($this->_watermarkRand->fileName); $maxWatermarkSize = $source->getImageWidth() * $config->getSize(); if ($maxWatermarkSize < $config->getMinSize()) { $maxWatermarkSize = $config->getMinSize(); } $watermark->thumbnailImage($maxWatermarkSize, $maxWatermarkSize); //cache image size $imageWidth = $source->getImageWidth(); $imageHeight = $source->getImageHeight(); //set max watermarks $maxCols = $imageWidth / $config->getMinSize() / 2; if ($maxCols < $config->getCols()) { $config->setCols(ceil($maxCols)); } $maxRows = $imageHeight / $config->getMinSize() / 2; if ($maxRows < $config->getRows()) { $config->setRows(ceil($maxRows)); } //step width $stepWidth = $imageWidth / $config->getCols(); $stepHeight = $imageHeight / $config->getRows(); //put images for ($row = 0; $row < $config->getRows(); $row++) { for ($col = 0; $col < $config->getCols(); $col++) { $x = rand($col * $stepWidth, ($col + 1) * $stepWidth); $y = rand($row * $stepHeight, ($row + 1) * $stepHeight); //make sure that watermark is in image bound if ($x + $maxWatermarkSize > $imageWidth) { $x = $imageWidth - $maxWatermarkSize; } if ($y + $maxWatermarkSize > $imageHeight) { $y = $imageHeight - $maxWatermarkSize; } //put watermark to image $source->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $x, $y); } } $source->setImageFormat("jpeg"); $this->_imageStruct->image = imagecreatefromstring($source->getImageBlob()); return $this->_imageStruct; }
/** * Generate the animated gif * * @return string binary image data */ private function createAnimation($images) { $animation = new \Imagick(); $animation->setFormat('gif'); foreach ($images as $image) { $frame = new \Imagick(); $frame->readImageBlob($image); $animation->addImage($frame); $animation->setImageDelay(50); } return $animation->getImagesBlob(); }
public function processBinaryImageData(string $binaryImageData) : string { $this->validateImageDimensions($this->width, $this->height); $imagick = new \Imagick(); try { $imagick->readImageBlob($binaryImageData); } catch (\ImagickException $e) { throw new InvalidBinaryImageDataException($e->getMessage()); } $imagick->resizeImage($this->width, $this->height, \Imagick::FILTER_LANCZOS, 1); return $imagick->getImageBlob(); }
/** * Get Thumbnail Response * * @param integer $width * @param Image $image * * @return Response */ public function getThumbnailResponse($width, $image) { if (!$image instanceof Image) { throw new \LogicException("Not a valid image"); } $core = explode(";", $image->getContent()); $content = base64_decode(substr($core[1], 7)); $thumbnail = new \Imagick(); $thumbnail->readImageBlob($content); $thumbnail->scaleImage($width, 0); return new Response($thumbnail->getImageBlob(), 200, array('Content-Type' => $image->getMimeType())); }
public function load($string) { try { $magick = new \Imagick(); $magick->readImageBlob($string); $magick->setImageMatte(true); $palette = self::createPalette($magick->getImageColorspace()); } catch (\ImagickException $e) { throw new RuntimeException("Imagick: Could not load image from string. {$e->getMessage()}", $e->getCode(), $e); } return new RImage($magick, $palette, self::$emptyBag); }
/** * Initiates new image from binary data * * @param string $data * @return \Intervention\Image\Image */ public function initFromBinary($binary) { $core = new \Imagick(); try { $core->readImageBlob($binary); } catch (\ImagickException $e) { throw new \Intervention\Image\Exception\NotReadableException("Unable to read image from binary data.", 0, $e); } // build image $image = $this->initFromImagick($core); $image->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $binary); return $image; }
/** * Loads the image data from a string. * * @param string $image_data The data to use for the image. * * @throws Horde_Image_Exception */ public function loadString($image_data) { parent::loadString($image_data); $this->_imagick->clear(); try { $this->_imagick->readImageBlob($this->_data); $this->_imagick->setImageFormat($this->_type); $this->_imagick->setIteratorIndex(0); } catch (ImagickException $e) { throw new Horde_Image_Exception($e); } unset($this->_data); }
/** * @return string */ private function getCombinedImage() { $im = new \Imagick(); foreach ($this->screenshots as $screenshot) { $im->readImageBlob($screenshot); } /* Append the images into one */ $im->resetIterator(); $combined = $im->appendImages(true); /* Output the image */ $combined->setImageFormat("png"); return (string) $combined; }
/** * Load the image data from a file. * * @param string $filename The full path and filename to the file to load * the image data from. The filename will also be * used for the image id. * * @return mixed */ public function loadFile($filename) { // parent function loads image data into $this->_data parent::loadFile($filename); $this->_imagick->clear(); try { $this->_imagick->readImageBlob($this->_data); $this->_imagick->setImageFormat($this->_type); $this->_imagick->setIteratorIndex(0); } catch (ImagickException $e) { throw new Horde_Image_Exception($e); } unset($this->_data); }
public function processBinaryImageData(string $binaryImageData) : string { $this->validateImageDimensions($this->width, $this->height); $this->validateBackgroundColor(); $image = new \Imagick(); try { $image->readImageBlob($binaryImageData); } catch (\ImagickException $e) { throw new InvalidBinaryImageDataException($e->getMessage()); } $image->resizeImage($this->width, $this->height, \Imagick::FILTER_LANCZOS, 1, true); $canvas = $this->inscribeImageIntoCanvas($image); return $canvas->getImageBlob(); }
function createIcon($file) { // @todo: Add support for others methods. if (class_exists("Imagick")) { $img = new Imagick(); $img->readImageBlob(file_get_contents($file)); $img->thumbnailImage(100, 100, true); return base64_encode($img); } elseif (extension_loaded('gd')) { return createIconGD($file); } else { return giftThumbnail(); } }
/** * Gets the user's flag * * @param $user array The user * * @return Imagick|null The flag, or nothing. */ private function getFlag($user) { $country = $user['country']; if (!$country) { return null; } $flag = new Imagick(); $cachedPicture = $this->mc->get("osusigv3_flag_" . strtolower($country)); if (!$cachedPicture) { $flagBlob = @file_get_contents(self::FLAGS_DIRECTORY . $country . '.png'); if ($flagBlob === false) { return null; } $flag->readImageBlob($flagBlob); $flag->setImageFormat('png'); $this->mc->set("osusigv3_flag_" . strtolower($country), base64_encode($flag->getImageBlob()), 43200); return $flag; } else { $decodedPicture = base64_decode($cachedPicture); $flag->readImageBlob($decodedPicture); $flag->setImageFormat('png'); return $flag; } }
/** * Get image object from image data * @param string|AjaxFile $data * @throws Exception * @return \Imagick|null */ public static function data2image($data) { if ($data instanceof AjaxFile) { $data = $data->val(); } elseif ($data instanceof \Imagick) { return clone $data; } try { $img = new \Imagick(); $img->readImageBlob($data); return $img; } catch (\ImagickException $ex) { throw new Exception('Invalid image file format'); } }
public function storeBase64Image($name, $dir, $fileContentBase64) { $uploadDir = $this->rootDir . '/../web' . $dir; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0777, true); } $image = new \Imagick(); $data = explode(',', $fileContentBase64); $fileContent = base64_decode($data[1]); $image->readImageBlob($fileContent); $filename = $name . "." . time() . '.jpg'; $image->setImageFormat('jpeg'); $image->setCompression(\Imagick::COMPRESSION_JPEG); $image->setCompressionQuality(50); $image->writeImage($uploadDir . $filename); return $filename; }