/** * @return self */ public function grayscale() { $this->preModify(); $this->resource->setImageType(\Imagick::IMGTYPE_GRAYSCALEMATTE); $this->postModify(); return $this; }
function mkbilder($typ) { $this->err->write('mkbilder', $typ); $org = new Imagick(); if (!$org->readImage("/tmp/tmp.file_org")) { return false; } $big = new Imagick(); $big->newImage($this->bigwidth, $this->bigheight, new ImagickPixel('white')); $big->setImageFormat($typ); //$big->setImageColorspace($org->getImageColorspace() ); $big->setImageType(imagick::IMGTYPE_TRUECOLOR); $org->scaleImage($this->bigwidth, $this->bigheight, true); $d = $org->getImageGeometry(); $xoff = ($this->bigwidth - $d['width']) / 2; $big->compositeImage($org, imagick::COMPOSITE_DEFAULT, $xoff, 0); $rc = $big->writeImage("/tmp/tmp.file_org"); $big->clear(); $big->destroy(); $small = new Imagick(); $small->newImage($this->smallwidth, $this->smallheight, new ImagickPixel('white')); $small->setImageFormat($typ); //$small->setImageColorspace($org->getImageColorspace() ); $small->setImageType(imagick::IMGTYPE_TRUECOLOR); $org->scaleImage($this->smallwidth, $this->smallheight, true); $d = $org->getImageGeometry(); $xoff = ($this->smallwidth - $d['width']) / 2; $small->compositeImage($org, imagick::ALIGN_CENTER, $xoff, 0); $rc = $small->writeImage("/tmp/tmp.file_small"); $small->clear(); $small->destroy(); $org->clear(); $org->destroy(); return true; }
/** * Convert the current image to grayscale. */ public function grayscale() { try { $this->_imagick->setImageType(Imagick::IMGTYPE_GRAYSCALE); } catch (ImageException $e) { throw new Horde_Image_Exception($e); } }
/** * Converts the image to greyscale * * @return bool|PEAR_Error TRUE or a PEAR_Error object on error * @access public */ function greyscale() { $this->imagick->setImageType(Imagick::IMGTYPE_GRAYSCALE); /*$this->imagick->setImageColorSpace(Imagick::COLORSPACE_GRAY); $this->imagick->setImageDepth(8); $this->imagick->separateImageChannel(Imagick::CHANNEL_GRAY); $this->imagick->setImageChannelDepth(Imagick::CHANNEL_GRAY, 8);*/ return true; }
/** * Creates new image instance * * @param integer $width * @param integer $height * @param string $background * @return \Intervention\Image\Image */ public function newImage($width, $height, $background = null) { $background = new Color($background); // create empty core $core = new \Imagick(); $core->newImage($width, $height, $background->getPixel(), 'png'); $core->setType(\Imagick::IMGTYPE_UNDEFINED); $core->setImageType(\Imagick::IMGTYPE_UNDEFINED); $core->setColorspace(\Imagick::COLORSPACE_UNDEFINED); // build image $image = new \Intervention\Image\Image(new static(), $core); return $image; }
/** * Initiates new image from path in filesystem * * @param string $path * @return \Intervention\Image\Image */ public function initFromPath($path) { $core = new \Imagick(); try { $core->readImage($path); $core->setImageType(\Imagick::IMGTYPE_TRUECOLORMATTE); } catch (\ImagickException $e) { throw new \Intervention\Image\Exception\NotReadableException("Unable to read image from path ({$path}).", 0, $e); } // build image $image = $this->initFromImagick($core); $image->setFileInfoFromPath($path); return $image; }
private function background() { if ($this->config['method'] === 'image' && is_file($this->config['image'])) { $image = new \Imagick(); try { $image->readImage($this->config['image']); $image->setImageType(\Imagick::IMGTYPE_TRUECOLORMATTE); } catch (\ImagickException $e) { throw new Exception\NotSupportedException("Ivatar - ({$this->config['image']}) unable to open image."); } $image->cropThumbnailImage($this->options['size'], $this->options['size']); $this->ivatar->compositeImage($image, \Imagick::COMPOSITE_DEFAULT, 0, 0); } }
/** * Creates a true color image * * @param int $w the width of the image * @param int $h the height of the image * @return Imagick */ function zp_createImage($w, $h) { $im = new Imagick(); $im->newImage($w, $h, 'none'); $im->setImageType(Imagick::IMGTYPE_TRUECOLORMATTE); return $im; }
/** * @return Pimcore_Image_Adapter_Imagick */ public function grayscale() { $this->resource->setImageType(imagick::IMGTYPE_GRAYSCALEMATTE); $this->reinitializeImage(); return $this; }
function encode_imagick($ps_filepath, $ps_output_path, $pa_options) { if (!($magick = $this->mimetype2magick[$pa_options["output_mimetype"]])) { $this->error = "Invalid output format"; return false; } # # Open image # $h = new Imagick(); if (!$h->readImage($ps_filepath)) { $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(imagick::IMGTYPE_TRUECOLOR); if (!$h->setImageColorspace(imagick::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, imagick::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, imagick::FILTER_CUBIC, $pa_options["antialiasing"])) { $this->error = "Couldn't scale image"; return false; } } $i = 0; $layer_list[] = array(); while ($y < $image_height) { if (!($slice = $h->getImageRegion($wx, $wy, $x, $y))) { $this->error = "Couldn't create tile"; return false; } $slice->setCompressionQuality($pa_options["quality"]); if (!$slice->setImageFormat($magick)) { $reason = WandGetExceptionType($slice); $description = WandGetExceptionDescription($slice); $this->error = "Tile conversion failed: {$reason}; {$description}"; return false; } # --- remove color profile (saves lots of space) //$slice->removeImageProfile($slice); $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; } }
/** * Load actual image pixels from Imagick object * * @param Imagick $im Image to load from */ public function readImageFromImagick(Imagick $im) { /* Threshold */ $im->setImageType(Imagick::IMGTYPE_TRUECOLOR); // Remove transparency (good for PDF's) $max = $im->getQuantumRange(); $max = $max["quantumRangeLong"]; $im->thresholdImage(0.5 * $max); /* Make a string of 1's and 0's */ $geometry = $im->getimagegeometry(); $this->imgHeight = $im->getimageheight(); $this->imgWidth = $im->getimagewidth(); $this->imgData = str_repeat("", $this->imgHeight * $this->imgWidth); for ($y = 0; $y < $this->imgHeight; $y++) { for ($x = 0; $x < $this->imgWidth; $x++) { /* Faster to average channels, blend alpha and negate the image here than via filters (tested!) */ $cols = $im->getImagePixelColor($x, $y); $cols = $cols->getcolor(); $greyness = (int) (($cols['r'] + $cols['g'] + $cols['b']) / 3) >> 7; // 1 for white, 0 for black $this->imgData[$y * $this->imgWidth + $x] = 1 - $greyness; // 1 for black, 0 for white } } }
private function _isLightImage($imageData) { $image = new Imagick(); $image->readImageBlob($imageData['contents']); $max = $image->getQuantumRange(); $max = $max["quantumRangeLong"]; $image->setImageType(Imagick::IMGTYPE_GRAYSCALEMATTE); $float = 0.5; $image->thresholdImage($float * $max, 255); $image->setBackgroundColor('white'); $black = 0; $white = 0; for ($x = 0; $x < $image->getImageWidth(); $x++) { for ($y = 0; $y < $image->getImageHeight(); $y++) { $pixel = $image->getImagePixelColor($x, $y); $value = $pixel->getColor(); if ($value['r'] == 0 && $value['g'] == 0 && $value['b'] == 0) { $black++; } else { $white++; } } } return $white > $black; }
function setStrokeMiterLimit($strokeColor, $fillColor, $backgroundColor) { $draw = new \ImagickDraw(); $draw->setStrokeColor($strokeColor); $draw->setStrokeOpacity(0.6); $draw->setFillColor($fillColor); $draw->setStrokeWidth(10); $yOffset = 100; $draw->setStrokeLineJoin(\Imagick::LINEJOIN_MITER); for ($y = 0; $y < 3; $y++) { $draw->setStrokeMiterLimit(40 * $y); $points = [['x' => 22 * 3, 'y' => 15 * 4 + $y * $yOffset], ['x' => 20 * 3, 'y' => 20 * 4 + $y * $yOffset], ['x' => 70 * 5, 'y' => 45 * 4 + $y * $yOffset]]; $draw->polygon($points); } $image = new \Imagick(); $image->newImage(500, 500, $backgroundColor); $image->setImageFormat("png"); $image->drawImage($draw); $image->setImageType(\Imagick::IMGTYPE_PALETTE); //TODO - this should either be everywhere or nowhere $image->setImageCompressionQuality(100); $image->stripImage(); header("Content-Type: image/png"); echo $image->getImageBlob(); }
/** * {@inheritdoc} */ public function greyscale() { $this->image->setImageType(Imagick::IMGTYPE_GRAYSCALE); }