protected function _filter($type, $params = array()) { switch ($type) { case self::FILTER_GRAYSCALE: $this->im->setImageColorSpace(Imagick::COLORSPACE_GRAY); break; case self::FILTER_SEPIA: $this->im->sepiaToneImage(80); break; case self::FILTER_CONTRAST: $max_origin_level = 20; $level = isset($params['level']) ? $params['level'] : (isset($params[0]) ? $params[0] : 3); if ($level > 0) { $level = min($level, 100); $level = $max_origin_level / 100 * $level; $this->im->sigmoidalcontrastImage(true, $level, 100, imagick::CHANNEL_ALL); } break; case self::FILTER_BRIGHTNESS: $level = isset($params['level']) ? $params['level'] : (isset($params[0]) ? $params[0] : 3); if ($level > 0) { $level = min($level, 100); $level = 100 + $level; $this->im->modulateImage($level, 0, 100); } break; default: $this->im->setImageColorSpace(Imagick::COLORSPACE_GRAY); break; } }
/** * @param int $brightness * @param int $saturation * @param int $hue * @return $this */ public function brightnessSaturation($brightness = 100, $saturation = 100, $hue = 100) { $this->preModify(); $this->resource->modulateImage($brightness, $saturation, $hue); $this->postModify(); return $this; }
function opticrop($image, $w, $h, $out, $format) { // source dimensions $imginfo = getimagesize($image); $w0 = $imginfo[0]; $h0 = $imginfo[1]; if ($w > $w0 || $h > $h0) { die("Target dimensions must be smaller or equal to source dimensions."); } // parameters for the edge-maximizing crop algorithm $r = 1; // radius of edge filter $nk = 9; // scale count: number of crop sizes to try $gamma = GAMMA; // edge normalization parameter -- see documentation $ar = $w / $h; // target aspect ratio (AR) $ar0 = $w0 / $h0; // target aspect ratio (AR) dprint(basename($image) . ": {$w0} x {$h0} => {$w} x {$h}"); $img = new Imagick($image); $imgcp = clone $img; // compute center of edginess $img->edgeImage($r); $img->modulateImage(100, 0, 100); // grayscale $img->blackThresholdImage("#0f0f0f"); $img->writeImage($out); // use gd for random pixel access $im = ImageCreateFromJpeg($out); $xcenter = 0; $ycenter = 0; $sum = 0; $n = 100000; for ($k = 0; $k < $n; $k++) { $i = mt_rand(0, $w0 - 1); $j = mt_rand(0, $h0 - 1); $val = imagecolorat($im, $i, $j) & 0xff; $sum += $val; $xcenter += ($i + 1) * $val; $ycenter += ($j + 1) * $val; } $xcenter /= $sum; $ycenter /= $sum; // crop source img to target AR if ($w0 / $h0 > $ar) { // source AR wider than target // crop width to target AR $wcrop0 = round($ar * $h0); $hcrop0 = $h0; } else { // crop height to target AR $wcrop0 = $w0; $hcrop0 = round($w0 / $ar); } // crop parameters for all scales and translations $params = array(); // crop at different scales $hgap = $hcrop0 - $h; $hinc = $nk == 1 ? 0 : $hgap / ($nk - 1); $wgap = $wcrop0 - $w; $winc = $nk == 1 ? 0 : $wgap / ($nk - 1); // find window with highest normalized edginess $n = 10000; $maxbetanorm = 0; $maxfile = ''; $maxparam = array('w' => 0, 'h' => 0, 'x' => 0, 'y' => 0); for ($k = 0; $k < $nk; $k++) { $hcrop = round($hcrop0 - $k * $hinc); $wcrop = round($wcrop0 - $k * $winc); $xcrop = $xcenter - $wcrop / 2; $ycrop = $ycenter - $hcrop / 2; dprint("crop: {$wcrop}, {$hcrop}, {$xcrop}, {$ycrop}"); if ($xcrop < 0) { $xcrop = 0; } if ($xcrop + $wcrop > $w0) { $xcrop = $w0 - $wcrop; } if ($ycrop < 0) { $ycrop = 0; } if ($ycrop + $hcrop > $h0) { $ycrop = $h0 - $hcrop; } // debug $currfile = CACHE_PATH . "image{$k}.jpg"; if (DEBUG > 0) { $currimg = clone $img; $c = new ImagickDraw(); $c->setFillColor("red"); $c->circle($xcenter, $ycenter, $xcenter, $ycenter + 4); $currimg->drawImage($c); $currimg->cropImage($wcrop, $hcrop, $xcrop, $ycrop); $currimg->writeImage($currfile); $currimg->destroy(); } $beta = 0; for ($c = 0; $c < $n; $c++) { $i = mt_rand(0, $wcrop - 1); $j = mt_rand(0, $hcrop - 1); $beta += imagecolorat($im, $xcrop + $i, $ycrop + $j) & 0xff; } $area = $wcrop * $hcrop; $betanorm = $beta / ($n * pow($area, $gamma - 1)); dprint("beta: {$beta}; betan: {$betanorm}"); dprint("image{$k}.jpg:<br/>\n<img src=\"{$currfile}\"/>"); // best image found, save it if ($betanorm > $maxbetanorm) { $maxbetanorm = $betanorm; $maxparam['w'] = $wcrop; $maxparam['h'] = $hcrop; $maxparam['x'] = $xcrop; $maxparam['y'] = $ycrop; $maxfile = $currfile; } } dprint("best image: {$maxfile}"); if (FORMAT == 'json') { // return coordinates instead of image $data = json_encode($maxparam); file_put_contents($out, $data); } else { // return image $imgcp->cropImage($maxparam['w'], $maxparam['h'], $maxparam['x'], $maxparam['y']); $imgcp->scaleImage($w, $h); $imgcp->writeImage($out); } chmod($out, 0777); $img->destroy(); $imgcp->destroy(); return 0; }
/** * * @return bool */ public function blacknwhite() { $source = $this->_getTempEditorFullPath(); $picture = new Imagick($source[0]); $picture->modulateImage(100, 0, 100); $picture->writeImage($source[1]); $this->getRawEditorCache(); return true; }
/** * Apply transformations on image * * @param string $source Source image * @param array $params Transformations and parameters * @param string $store Temporary store on disk * * @return string */ public function transform($source, $params, $store = null) { try { $image = new \Imagick($source); $image->setImageCompression(\Imagick::COMPRESSION_JPEG); $image->setImageCompressionQuality($this->_quality); if (isset($params['negate'])) { $image->negateImage(false); } if (isset($params['rotate'])) { $image->rotateImage(new \ImagickPixel('#00000000'), $params['rotate']['angle']); } if (isset($params['crop'])) { $image->cropImage($params['crop']['w'], $params['crop']['h'], $params['crop']['x'], $params['crop']['y']); } if (isset($params['contrast'])) { $level = (int) $params['contrast']; if ($level < -10) { $level = -10; } else { if ($level > 10) { $level = 10; } } if ($level > 0) { for ($i = 0; $i < $level; $i++) { $image->contrastImage(1); } } else { if ($level < 0) { for ($i = $level; $i < 0; $i++) { $image->contrastImage(0); } } } } if (isset($params['brightness'])) { $value = (int) $params['brightness']; $brightness = null; if ($value <= 0) { $brightness = $value + 100; } else { $brightness = $value * 3 + 100; } $image->modulateImage($brightness, 100, 100); } $ret = null; if ($store !== null) { $ret = $image->writeImage($store); } else { $ret = $image->getImageBlob(); } $image->destroy(); return $ret; } catch (\ImagickException $e) { $image->destroy(); throw new \RuntimeException($e->getMessage()); } }
public function brightnessImage($brightness, $imagePath) { $image = new Imagick(); $image->readImage('../uploads/' . $imagePath); if ($brightness == false) { $image->modulateImage(90, 100, 100); } else { $image->modulateImage(110, 100, 100); } $image->writeImage('../uploads/' . $imagePath); }
/* Чтение изображения */ // $im = new Imagick("C:/OpenServer/domains/localhost/img/img.jpg"); // путь до картинки $real_image = $_SERVER['DOCUMENT_ROOT'] . '/img/20150729_134058.jpg'; $path_image = $_SERVER['DOCUMENT_ROOT'] . '/img/dark.jpg'; $new_image = $_SERVER['DOCUMENT_ROOT'] . '/img/new.jpg'; // echo $path_image."<br>"; $im = new Imagick($tmp_image); //$i=getImageGamma($im); $target_mean = 46000; $Img = new Imagick($real_image); $mean = $Img->getImageChannelMean(imagick::CHANNEL_ALL)['mean']; if ($target_mean > $mean * 1.05) { /* не изменится, если яркость в пределах 5% */ $perc_diff = $target_mean / $mean * 100; $Img->modulateImage($perc_diff, 100, 100); $Img->writeImage($new_image); echo "Созданно новое изображение"; } else { echo "Изображение валидно"; } $brightness = 30; $contrast = 11; $channel = 4; $gdHandle = $path_image; function getBrightness($gdHandle) { $width = imagesx($gdHandle); $height = imagesy($gdHandle); $totalBrightness = 0; for ($x = 0; $x < $width; $x++) {
/** * Decode a single OMR Page and return data array. * This function requires ImageMagick library and zbarimg (http://zbar.sourceforge.net/). * @param $image (string) image file to be decoded (scanned OMR page at 200 DPI with full color range). * @return array of answers data or false in case of error. */ function F_decodeOMRPage($image) { require_once '../config/tce_config.php'; // decode barcode containing first question number $command = K_OMR_PATH_ZBARIMG . ' --raw -Sdisable -Scode128.enable -q ' . escapeshellarg($image); $qstart = exec($command); $qstart = intval($qstart); if ($qstart == 0) { return false; } $img = new Imagick(); $img->readImage($image); $imginfo = $img->identifyImage(); if ($imginfo['type'] == 'TrueColor') { // remove red color $img->separateImageChannel(Imagick::CHANNEL_RED); } else { // desaturate image $img->modulateImage(100, 0, 100); } // get image width and height $w = $imginfo['geometry']['width']; $h = $imginfo['geometry']['height']; if ($h > $w) { // crop header and footer $y = round(($h - $w) / 2); $img->cropImage($w, $w, 0, $y); $img->setImagePage(0, 0, 0, 0); } // trim image $imgtmp = $img->clone(); $color = '#808080'; $img->blackthresholdImage("{$color}"); $img->whitethresholdImage("{$color}"); $img->trimImage(85); $imgpage = $img->getImagePage(); $w = $img->getImageWidth(); $h = $img->getImageHeight(); $img = $imgtmp->clone(); $imgtmp->clear(); $img->cropImage($w, $h, $imgpage['x'], $imgpage['y']); $img->setImagePage(0, 0, 0, 0); // increase contrast $img->normalizeImage(Imagick::CHANNEL_ALL); $img->enhanceImage(); $img->despeckleImage(); // straighten image $img->deskewImage(40); $img->setImagePage(0, 0, 0, 0); // trim image (remove white border) $imgtmp = $img->clone(); $color = '#808080'; $img->blackthresholdImage("{$color}"); $img->whitethresholdImage("{$color}"); $img->trimImage(85); $imgpage = $img->getImagePage(); $w = $img->getImageWidth(); $h = $img->getImageHeight(); $img = $imgtmp->clone(); $imgtmp->clear(); $img->cropImage($w, $h, $imgpage['x'], $imgpage['y']); $img->setImagePage(0, 0, 0, 0); // resize image $img->resizeImage(1028, 1052, Imagick::FILTER_CUBIC, 1); $img->setImagePage(0, 0, 0, 0); // binarize image $color = '#c0c0c0'; $img->blackthresholdImage("{$color}"); $img->whitethresholdImage("{$color}"); // scan block width $blkw = 16; // starting column in pixels $scol = 106; // starting row in pixels $srow = 49; // column distance in pixels between two answers $dcol = 75.364; // column distance in pixels between True/false circles $dtf = 25; // row distance in pixels between two questions $drow = 32.38; // verify image pattern $imgtmp = $img->clone(); $imgtmp->cropImage(1028, 10, 0, 10); $imgtmp->setImagePage(0, 0, 0, 0); // create reference block pattern $impref = new Imagick(); $impref->newImage(3, 10, new ImagickPixel('black')); $psum = 0; for ($c = 0; $c < 12; ++$c) { $x = round(112 + $c * $dcol); // get square region inside the current grid position $imreg = $img->getImageRegion(3, 10, $x, 0); $imreg->setImagePage(0, 0, 0, 0); // get root-mean-square-error with reference image $rmse = $imreg->compareImages($impref, Imagick::METRIC_ROOTMEANSQUAREDERROR); // cont reference blocks $psum += round(1.25 - $rmse[1]) . "\n"; } $imreg->clear(); $impref->clear(); if ($psum != 12) { return false; } // create reference block $imref = new Imagick(); $imref->newImage($blkw, $blkw, new ImagickPixel('black')); // array to be returned $omrdata = array(); // for each row (question) for ($r = 0; $r < 30; ++$r) { $omrdata[$r + $qstart] = array(); $y = round($srow + $r * $drow); // for each column (answer) for ($c = 0; $c < 12; ++$c) { // read true option $x = round($scol + $c * $dcol); // get square region inside the current grid position $imreg = $img->getImageRegion($blkw, $blkw, $x, $y); $imreg->setImagePage(0, 0, 0, 0); // get root-mean-square-error with reference image $rmse = $imreg->compareImages($imref, Imagick::METRIC_ROOTMEANSQUAREDERROR); // true option $opt_true = 2 * round(1.25 - $rmse[1]); // read false option $x += $dtf; // get square region inside the current grid position $imreg = $img->getImageRegion($blkw, $blkw, $x, $y); $imreg->setImagePage(0, 0, 0, 0); // get root-mean-square-error with reference image $rmse = $imreg->compareImages($imref, Imagick::METRIC_ROOTMEANSQUAREDERROR); // false option $opt_false = round(1.25 - $rmse[1]); // set array to be returned (-1 = unset, 0 = false, 1 = true) $val = $opt_true + $opt_false - 1; if ($val > 1) { $val = 1; } $omrdata[$r + $qstart][$c + 1] = $val; } } $imreg->clear(); $imref->clear(); return $omrdata; }
function opticrop2($image, $w, $h, $out) { // get size of the original $imginfo = getimagesize($image); $w0 = $imginfo[0]; $h0 = $imginfo[1]; if ($w > $w0 || $h > $h0) { die("Target dimensions must be smaller or equal to source dimensions."); } // parameters for the edge-maximizing crop algorithm $r = 2; // radius of edge filter $nk = 1; // scale count: number of crop sizes to try $nx = 3; // number of x-translations to try $ny = 3; // number of y-translations to try $gamma = 0.8; // edge-sum normalization parameter -- see documentation $ar = $w / $h; // target aspect ratio (AR) dprint('$img: ' . $image); dprint('$w x $h: ' . $w . 'x' . $h); dprint('$w0 x $h0: ' . $w0 . 'x' . $h0); $img = new Imagick($image); // crop source img to target AR if ($w0 / $h0 > $ar) { // source AR wider than target // crop width to target AR $wcrop0 = round($ar * $h0); $hcrop0 = $h0; } else { // crop height to target AR $wcrop0 = $w0; $hcrop0 = round($w0 / $ar); } // crop parameters for all scales and translations $params = array(); // crop at different scales $hgap = $hcrop0 - $h; $hinc = $nk == 1 ? 0 : $hgap / ($nk - 1); for ($k = 0; $k < $nk; $k++) { $hcrop = round($hcrop0 - $k * $hinc); $wcrop = $hcrop * $ar; // crop at different locations // space translations out evenly across source image $xgap = $w0 - $wcrop; $xinc = $xgap / $nx; $ygap = $h0 - $hcrop; $yinc = $ygap / $ny; // crop is only slightly smaller than source // proceed by 1px increments $nxtemp = $nx; $nytemp = $ny; if ($xgap < $nx - 1) { $nxtemp = $xgap + 1; $xinc = 1; } if ($ygap < $ny - 1) { $nytemp = $ygap + 1; $yinc = 1; } // generate parameters for trial crops for ($i = 0; $i < $nxtemp; $i++) { $xcrop = round($i * $xinc); for ($j = 0; $j < $nytemp; $j++) { $ycrop = round($j * $yinc); $params[] = array('wcrop' => $wcrop, 'hcrop' => $hcrop, 'xcrop' => $xcrop, 'ycrop' => $ycrop); } } } dprint("original:<br/><img src=\"" . $_GET['src'] . "\"/>"); // crop each trial image, save the one with most edges $i = 0; $imgcopy = clone $img; $maxbetanorm = 0; $maxparam = ""; $timeparts = explode(' ', microtime()); $starttime = $timeparts[1] . substr($timeparts[0], 1); foreach ($params as $param) { $i++; //$currfile = CACHE_PATH."image$i.jpg"; $beta = 0; $img->edgeImage($r); $img->modulateImage(100, 0, 100); // grayscale $pi = $img->getPixelRegionIterator((int) $param['xcrop'], (int) $param['ycrop'], (int) $param['wcrop'], (int) $param['hcrop']); foreach ($pi as $row => $pixels) { foreach ($pixels as $column => $pixel) { $beta += $pixel->getColorValue(imagick::COLOR_RED); } } $area = $param['wcrop'] * $param['hcrop']; $betanorm = $beta / pow($area, $gamma); dprint($param, true); // best image found, save it if ($betanorm > $maxbetanorm) { $maxbetanorm = $betanorm; $maxparam = $param; } } $timeparts = explode(' ', microtime()); $endtime = $timeparts[1] . substr($timeparts[0], 1); $elapsed = bcsub($endtime, $starttime, 6); echo "<br/>Metric computation time (s): " . $elapsed; $img->destroy(); $img = $imgcopy; $img->cropImage($maxparam['wcrop'], $maxparam['hcrop'], $maxparam['xcrop'], $maxparam['ycrop']); $img->scaleImage($w, $h); $img->writeImage($out); return 0; }
/** * */ function renderCustomImageCreases() { $tshirt = new \Imagick(realpath("images/tshirt/tshirt.jpg")); $logo = new \Imagick(realpath("images/tshirt/Logo.png")); $logo->resizeImage(100, 100, \Imagick::FILTER_LANCZOS, 1, TRUE); $tshirt->setImageFormat('png'); //First lets find the creases //Get the average color of the tshirt and make a new image from it. $colorString = getAverageColorString($tshirt); $creases = new \Imagick(); $creases->newpseudoimage($tshirt->getImageWidth(), $tshirt->getImageHeight(), "XC:" . $colorString); //Composite difference finds the creases $creases->compositeimage($tshirt, \Imagick::COMPOSITE_DIFFERENCE, 0, 0); $creases->setImageFormat('png'); //We need the image negated for the maths to work later. $creases->negateimage(true); //We also want "no crease" to equal 50% gray later //$creases->brightnessContrastImage(-50, 0); $creases->modulateImage(50, 100, 100); //Copy the logo into an image the same size as the shirt image //to make life easier $logoCentre = new \Imagick(); $logoCentre->newpseudoimage($tshirt->getImageWidth(), $tshirt->getImageHeight(), "XC:none"); $logoCentre->setImageFormat('png'); $logoCentre->compositeimage($logo, \Imagick::COMPOSITE_SRCOVER, 110, 75); //Save a copy of the tshirt sized logo $logoCentreMask = clone $logoCentre; //Blend the creases with the logo $logoCentre->compositeimage($creases, \Imagick::COMPOSITE_MODULATE, 0, 0); //Mask the logo so that only the pixels under the logo come through $logoCentreMask->compositeimage($logoCentre, \Imagick::COMPOSITE_SRCIN, 0, 0); //Composite the creased logo onto the shirt $tshirt->compositeimage($logoCentreMask, \Imagick::COMPOSITE_DEFAULT, 0, 0); //And Robert is your father's brother header("Content-Type: image/png"); echo $tshirt->getImageBlob(); }
/** * {@inheritdoc} */ public function brightness($level = 50) { $this->image->modulateImage(100 + $level, 100, 100); }
/** * Build image * @access protected * @return Imagick */ protected function _buildImage() { $canvas = new Imagick(); $canvas->newImage($this->_width, $this->_height, 'none'); $draw = new ImagickDraw(); //drawing Background $draw->setFillColor(new ImagickPixel($this->_theme->bgColor)); $draw->roundRectangle(0, 0, $this->_width - 1, $this->_height - 1, 5, 5); //drawing Title and Description $this->_addTextToDraw($draw, $this->_theme->titleColor, $this->_theme->titleFont, $this->_theme->titleFontSize, 84, $this->_theme->titleYPos, $this->_title); $this->_addTextToDraw($draw, $this->_theme->textColor, $this->_theme->textFont, $this->_theme->textFontSize, 84, $this->_theme->descYPos, $this->_description); //drawing Reward if (!empty($this->_reward)) { $this->_addTextToDraw($draw, $this->_theme->textColor, $this->_theme->textFont, $this->_theme->textFontSize, 84, $this->_theme->rewardYPos, $this->_reward); } $canvas->drawImage($draw); //drawing pix (if not unlocked, set black and white) $expired = new Imagick($this->_pix); if ($this->_state != 'unlocked') { $expired->modulateImage(100, 0, 100); } $canvas->compositeImage($expired, imagick::COMPOSITE_OVER, 4, 4); //if expired, draw the expired logo if ($this->_state == 'expired') { $expired = new Imagick(dirname(__FILE__) . '/extra/expired.png'); $canvas->compositeImage($expired, imagick::COMPOSITE_OVER, 0, 0); } return $canvas; }
function draw_figure($poster, $path, $y) { $im = new Imagick($path); $im->modulateImage(100, 0, 100); $im->contrastImage(true); $im->contrastImage(true); $im->contrastImage(true); $im->gaussianBlurImage(5, 0.5); if ($im->getImageHeight() > 100) { $im->scaleImage($im->getImageWidth() * 100 / $im->getImageHeight(), 100); } if ($im->getImageWidth() > 125) { $im->scaleImage(125, $im->getImageHeight() * 125 / $im->getImageWidth()); } $noise_layer2 = new Imagick(); $noise_layer2->newImage($im->getImageWidth(), $im->getImageHeight(), 'none', 'png'); $noise_layer2->addNoiseImage(imagick::NOISE_RANDOM); $noise_layer2->setImageOpacity(0.5); $noise_layer1 = new Imagick(); $noise_layer1->newImage($im->getImageWidth(), $im->getImageHeight(), 'none', 'png'); $noise_layer1->addNoiseImage(imagick::NOISE_RANDOM); $noise_layer1->modulateImage(100, 0, 100); $noise_layer1->setImageOpacity(0.3); $x = ($poster->getImageWidth() - $im->getImageWidth()) / 2; $poster->compositeImage($im, imagick::COMPOSITE_COLORBURN, $x, $y); $poster->compositeImage($noise_layer1, imagick::COMPOSITE_LIGHTEN, $x, $y); $poster->compositeImage($noise_layer2, imagick::COMPOSITE_SOFTLIGHT, $x, $y); }