/** * @return string * @throws CM_Exception */ public function getBlob() { $this->_imagick->setImageCompressionQuality($this->getCompressionQuality()); try { if ($this->_getAnimationRequired($this->getFormat())) { $imageBlob = $this->_imagick->getImagesBlob(); } else { $imageBlob = $this->_imagick->getImageBlob(); } } catch (ImagickException $e) { throw new CM_Exception('Cannot get image blob', null, ['originalExceptionMessage' => $e->getMessage()]); } return $imageBlob; }
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; }
/** * Outputs the image. * * @see XenForo_Image_Abstract::output() */ public function output($outputType, $outputFile = null, $quality = 85) { $this->_image->stripImage(); // NULL means output directly switch ($outputType) { case IMAGETYPE_GIF: if (is_callable(array($this->_image, 'optimizeimagelayers'))) { $this->_image->optimizeimagelayers(); } $success = $this->_image->setImageFormat('gif'); break; case IMAGETYPE_JPEG: $success = $this->_image->setImageFormat('jpeg') && $this->_image->setImageCompression(Imagick::COMPRESSION_JPEG) && $this->_image->setImageCompressionQuality($quality); break; case IMAGETYPE_PNG: $success = $this->_image->setImageFormat('png'); break; default: throw new XenForo_Exception('Invalid output type given. Expects IMAGETYPE_XXX constant.'); } try { if ($success) { if (!$outputFile) { echo $this->_image->getImagesBlob(); } else { $success = $this->_image->writeImages($outputFile, true); } } } catch (ImagickException $e) { return false; } return $success; }
public function store($file, $id) { $blobOpts = new CreateBlobOptions(); $blobOpts->setContentType($file->getMimeType()); $blobOpts->setCacheControl('max-age=315360000'); try { $img = new \Imagick($file->getRealPath()); $this->blobRestProxy->createBlockBlob($this->container, $id, $img->getImagesBlob(), $blobOpts); $img->coalesceImages(); foreach ($img as $frame) { $frame->thumbnailImage(180, 180); $frame->setImagePage(180, 180, 0, 0); } $this->blobRestProxy->createBlockBlob($this->container, $id . '.t', $img->getImagesBlob(), $blobOpts); } catch (\Exception $e) { return $e; } return null; }
/** * {@inheritdoc} */ public function get($format, array $options = array()) { try { $options['format'] = $format; $this->prepareOutput($options); } catch (\ImagickException $e) { throw new RuntimeException('Get operation failed', $e->getCode(), $e); } return $this->imagick->getImagesBlob(); }
protected function _render($type, $quality) { $type = $this->_save_function($type, $quality); $this->im->setImageCompressionQuality($quality); $this->type = $type; $this->mime = image_type_to_mime_type($type); if ($this->im->getNumberImages() > 1 && $type == "gif") { return $this->im->getImagesBlob(); } return $this->im->getImageBlob(); }
/** * 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(); }
/** * @return string * @throws CM_Exception */ public function getBlob() { try { if ($this->_getAnimationRequired($this->getFormat())) { $imageBlob = $this->_imagick->getImagesBlob(); } else { $imageBlob = $this->_imagick->getImageBlob(); } } catch (ImagickException $e) { throw new CM_Exception('Cannot get image blob: ' . $e->getMessage()); } return $imageBlob; }
public function register() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->password = $this->password; $user->fio = $this->fio; $user->pol = $this->pol; $user->birthday = $this->birthday; $user->country = $this->country; $user->place = $this->place; $user->email = $this->email; $user->registerDate = date('Y-m-d H:i:s'); Yii::trace($this->birthday); Yii::trace($user->birthday . ' ' . $user->registerDate); if (!$user->save()) { return false; } if (!Yii::$app->user->login(User::findByUsername($this->username), 0)) { return false; } $photo = new PhotoActiveRecord(); $photo->userId = Yii::$app->user->identity->id; $photo->photo = file_get_contents($this->photo->tempName); $imagick = new \Imagick(); $imagick->readImageBlob($photo->photo); foreach ($imagick as $frame) { $frame->thumbnailImage(800, 0); } $photo->thumbnail = $imagick->getImagesBlob(); $photo->posted = date('Y-m-d H:i:s'); $photo->name = $this->fio; if (!$photo->save()) { return false; } $user->photo = $photo->id; $user->save(); return true; } return false; }
public function overlay($layer, $x = 0, $y = 0) { //$layer_cs = $layer->image->getImageColorspace(); $layer->image->setImageColorspace($this->image->getImageColorspace()); if ($this->multiframe()) { $this->image = $this->image->coalesceImages(); $width = $this->image->getImageWidth(); $height = $this->image->getImageHeight(); foreach ($this->image as $frame) { $over = clone $layer->image; $frame->setImagePage($width, $height, 0, 0); $frame->compositeImage($over, $this->composition_mode, $x, $y); } // It's magic but it work $this->image->getImagesBlob(); $this->image->deconstructImages(); } else { $this->image->compositeImage($layer->image, $this->composition_mode, $x, $y); } //$layer->image->setImageColorspace($layer_cs); return $this; }
public function createThumb($key, $sw = 0, $sh = 0) { if (array_key_exists($key, $_FILES) && $_FILES[$key]['size']) { try { // イメージ読み込み $image = new Imagick(); $image->readImageBlob(file_get_contents($_FILES[$key]['tmp_name'])); $iw = $image->getImageWidth(); $ih = $image->getImageHeight(); $uniqid = uniqid('', true); if ($image->getImageFormat() == 'JPEG') { $format = 'jpg'; } elseif ($image->getImageFormat() == 'PNG') { $format = 'png'; } elseif ($image->getImageFormat() == 'GIF') { $format = 'gif'; } else { return null; } $path_t = APPLICATION_PATH . "/images/thumbnail/" . $uniqid . '.s.' . $format; $url_t = $this->view->app->base_url . "/images/thumbnail/" . $uniqid . '.s.' . $format; // リサイズなし if ($sw == $iw && $sh == $ih) { // ファイル書き込み $fh = fopen($path_t, 'wb'); fwrite($fh, $image->getImagesBlob()); fclose($fh); } elseif ($sw / $iw > $sh / $ih) { $ww = intval($iw * $sh / $ih); $hh = intval($ih * $sw / $iw); $image->scaleImage($sw, $hh); // 重ね合わせ $im2 = new Imagick(); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, 0, intval(($sh - $hh) / 2)); $im2->setImageFormat($format); // ファイル書き込み $fh = fopen($path_t, 'wb'); fwrite($fh, $im2->getImagesBlob()); fclose($fh); } else { $ww = intval($iw * $sh / $ih); $hh = intval($ih * $sw / $iw); $image->scaleImage($ww, $sh); // 重ね合わせ $im2 = new Imagick(); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, intval(($sw - $ww) / 2), 0); $im2->setImageFormat($format); // ファイル書き込み $fh = fopen($path_t, 'wb'); fwrite($fh, $im2->getImagesBlob()); fclose($fh); } return array('thumb_url' => $url_t); } catch (Exception $e) { return null; } } else { return null; } }
$combined->addImage($im); $im->destroy(); } #$combined->resetIterator(); #$combined = $combined->appendImages(true); #$combined->setPage(595,842,0,0); $combined->setImageFormat('pdf'); #$fp = fopen('/home/gujc/Downloads/test.pdf','wb'); #$combined->writeImagesFile($fp); $combined->writeImages($baseUri . 'test.pdf', true); #$combined->destroy(); $reload = new Imagick($baseUri . 'test.pdf'); #$type=$combined->getFormat(); header('Content-type: pdf'); #header('Content-Disposition: attachment;filename="test.pdf"'); echo $reload->getImagesBlob(); function add_text($image, $text, $x = 0, $y = 0, $angle = 0, $style = array()) { $draw = new ImagickDraw(); if (isset($style['font'])) { $draw->setFont($style['font']); } if (isset($style['font_size'])) { $draw->setFontSize($style['font_size']); } if (isset($style['fill_color'])) { $draw->setFillColor($style['fill_color']); } if (isset($style['back_color'])) { $draw->setTextUnderColor($style['back_color']); }
/** * {@inheritdoc} */ public function getStream() { return StreamUtils::create($this->imagick->getImagesBlob()); }
/** * Outputs an image resource as a given type * * @param Imagick $im * @param string $type * @param string $filename * @param int $qual * @return bool */ function zp_imageOutput($im, $type, $filename = NULL, $qual = 75) { $interlace = getOption('image_interlace'); $qual = max(min($qual, 100), 0); $im->setImageFormat($type); switch ($type) { case 'gif': $im->setImageCompression(Imagick::COMPRESSION_LZW); $im->setImageCompressionQuality($qual); if ($interlace) { $im->setInterlaceScheme(Imagick::INTERLACE_GIF); } break; case 'jpeg': case 'jpg': $im->setImageCompression(Imagick::COMPRESSION_JPEG); $im->setImageCompressionQuality($qual); if ($interlace) { $im->setInterlaceScheme(Imagick::INTERLACE_JPEG); } break; case 'png': $im->setImageCompression(Imagick::COMPRESSION_ZIP); $im->setImageCompressionQuality($qual); if ($interlace) { $im->setInterlaceScheme(Imagick::INTERLACE_PNG); } break; } $im->optimizeImageLayers(); if ($filename == NULL) { header('Content-Type: image/' . $type); return print $im->getImagesBlob(); } return $im->writeImages(imgSrcURI($filename), true); }
public function createThumbnail($rawdata, $sw, $sh, $path) { // イメージ読み込み $image = new Imagick(); $image->readImageBlob($rawdata); $iw = $image->getImageWidth(); $ih = $image->getImageHeight(); // リサイズなし if ($sw == $iw && $sh == $ih) { // ファイル書き込み $fh = fopen($path, 'wb'); fwrite($fh, $image->getImagesBlob()); fclose($fh); } elseif ($sw / $iw > $sh / $ih) { $ww = intval($iw * $sh / $ih); $hh = intval($ih * $sw / $iw); $image->scaleImage($sw, $hh); // 重ね合わせ $im2 = new Imagick(); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, 0, intval(($sh - $hh) / 2)); //$im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, 0, 0); $im2->setImageFormat('jpeg'); // ファイル書き込み $fh = fopen($path, 'wb'); fwrite($fh, $im2->getImagesBlob()); fclose($fh); } else { $ww = intval($iw * $sh / $ih); $hh = intval($ih * $sw / $iw); $image->scaleImage($ww, $sh); // 重ね合わせ $im2 = new Imagick(); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, intval(($sw - $ww) / 2), 0); //$im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, 0, 0); $im2->setImageFormat('jpeg'); // ファイル書き込み $fh = fopen($path, 'wb'); fwrite($fh, $im2->getImagesBlob()); fclose($fh); } }
/** * Outputs an image resource as a given type * * @internal Imagick::INTERLACE_[GIF|JPEG|PNG] require Imagick compiled against ImageMagick 6.3.4+ * * @param Imagick $im * @param string $type * @param string $filename * @param int $qual * @throws ImagickException * @return bool */ function zp_imageOutput($im, $type, $filename = NULL, $qual = 75) { global $_imagemagick_version, $_imagick_newer_interlace; if (!isset($_imagick_newer_interlace)) { $_imagick_newer_interlace = version_compare($_imagemagick_version['versionNumber'], '6.3.4', '>='); } $interlace = getOption('image_interlace'); $qual = max(min($qual, 100), 0); $im->setImageFormat($type); switch ($type) { case 'gif': $im->setCompression(Imagick::COMPRESSION_LZW); $im->setCompressionQuality($qual); if ($interlace) { if ($_imagick_newer_interlace) { $im->setInterlaceScheme(Imagick::INTERLACE_GIF); } else { $im->setInterlaceScheme(Imagick::INTERLACE_LINE); } } break; case 'jpeg': case 'jpg': $im->setCompression(Imagick::COMPRESSION_JPEG); $im->setCompressionQuality($qual); if ($interlace) { if ($_imagick_newer_interlace) { $im->setInterlaceScheme(Imagick::INTERLACE_JPEG); } else { $im->setInterlaceScheme(Imagick::INTERLACE_LINE); } } break; case 'png': $im->setCompression(Imagick::COMPRESSION_ZIP); $im->setCompressionQuality($qual); if ($interlace) { if ($_imagick_newer_interlace) { $im->setInterlaceScheme(Imagick::INTERLACE_PNG); } else { $im->setInterlaceScheme(Imagick::INTERLACE_LINE); } } break; } try { $im->optimizeImageLayers(); } catch (ImagickException $e) { if (DEBUG_IMAGE) { debugLog('Caught ImagickException in zp_imageOutput(): ' . $e->getMessage()); } } if ($filename == NULL) { header('Content-Type: image/' . $type); return print $im->getImagesBlob(); } return $im->writeImages($filename, true); }
public function scale($options = array()) { if (!$options['width']) { $options['width'] = '240'; } if (!$options['height']) { $options['height'] = '160'; } $imagedata = $this->get(array('image' => $options['image'])); $image = new Imagick(ROOT_DIR . '/media/' . $imagedata['filename']); foreach ($image as $frame) { $frame->thumbnailImage($options['width'], $options['height'], TRUE); } //header('Content-type: '.$image->getImageMimeType()); return $image->getImagesBlob(); }
public function doUpload($key, $sw = 0, $sh = 0) { $config = Zend_Registry::get('config'); if (array_key_exists($key, $_FILES) && $_FILES[$key]['size']) { try { $image = new Imagick(); $image->readImageBlob(file_get_contents($_FILES[$key]['tmp_name'])); $iw = $image->getImageWidth(); $ih = $image->getImageHeight(); $uniqid = uniqid('', true); $path_n = APPLICATION_PATH . "/images/thumbnail/" . $uniqid . ".jpg"; $url_n = $config->app->base_url . "/images/thumbnail/" . $uniqid . ".jpg"; if ($sw == 0 && $sh == 0) { $sw = $iw; $sh = $ih; $path_n = APPLICATION_PATH . "/images/" . $uniqid . ".jpg"; $url_n = $config->app->base_url . "/images/" . $uniqid . ".jpg"; } if ($sw == $iw && $sh == $ih) { $fh = fopen($path_n, 'wb'); fwrite($fh, $image->getImagesBlob()); fclose($fh); } elseif ($sw / $iw > $sh / $ih) { $ww = intval($iw * $sh / $ih); $hh = intval($ih * $sw / $iw); $image->scaleImage($sw, $hh); $im2 = new Imagick(); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, 0, intval(($sh - $hh) / 2)); $im2->setImageFormat('jpeg'); $fh = fopen($path_n, 'wb'); fwrite($fh, $im2->getImagesBlob()); fclose($fh); } else { $ww = intval($iw * $sh / $ih); $hh = intval($ih * $sw / $iw); if ($sw == 0 && $sh == 0) { $ww = 1; $hh = 1; $image->scaleImage($ww, $sh); $im2 = new Imagick(); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, intval(($sw - $ww) / 2), 0); $im2->setImageFormat('jpeg'); } else { //for thumbnail set compression to 90 $image->scaleImage($ww, $sh); $im2 = new Imagick(); $im2->setCompression(Imagick::COMPRESSION_JPEG); $im2->setCompressionQuality(90); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, intval(($sw - $ww) / 2), 0); $im2->setImageFormat('jpeg'); } /* $image->scaleImage($ww, $sh); $im2 = new Imagick(); $im2->newImage($sw, $sh, "none"); $im2->compositeImage($image, Imagick::COMPOSITE_DEFAULT, intval(($sw - $ww) / 2), 0); $im2->setImageFormat('jpeg'); */ $fh = fopen($path_n, 'wb'); fwrite($fh, $im2->getImagesBlob()); fclose($fh); } return $url_n; } catch (Exception $e) { return null; } } else { return null; } }
/** * @param Imagick $imagick * @param int $format * @param CM_File|null $fileNew * @throws CM_Exception */ private function _writeImagick(Imagick $imagick, $format, CM_File $fileNew = null) { if (true !== $imagick->setFormat($this->_getImagickFormat($format))) { throw new CM_Exception('Cannot set image format `' . $format . '`'); } $compressionQuality = $this->_getCompressionQuality(); if (true !== $imagick->setImageCompressionQuality($compressionQuality)) { throw new CM_Exception('Cannot set compression quality to `' . $compressionQuality . '`.'); } if (!$this->_getAnimationRequired($format)) { try { $imageBlob = $imagick->getImageBlob(); } catch (ImagickException $e) { throw new CM_Exception('Cannot get image blob: ' . $e->getMessage()); } } else { try { $imageBlob = $imagick->getImagesBlob(); } catch (ImagickException $e) { throw new CM_Exception('Cannot get images blob: ' . $e->getMessage()); } } $file = $fileNew ? $fileNew : $this; $file->write($imageBlob); if ($this->equals($file)) { $this->_imagick = $imagick; $this->_animated = $this->_getAnimationRequired($format); } }