mkdir('./images/output'); $climate->Green("Comparing level is {$percent}%\n"); $climate->Green("Preparing to comparing...\n"); $progress = $climate->progress()->total(count($json->path)); $files = array(); $files_error = array(); foreach ($json->path as $path_key => $path) { $file1 = "./images/{$tag_one}/{$path_key}.png"; $file2 = "./images/{$tag_two}/{$path_key}.png"; chmod("./images/{$tag_one}", 0777); chmod("./images/{$tag_two}", 0777); $progress->current($path_key + 1, "Comparing between {$file1} and {$file2}"); if (file_exists($file1) && file_exists($file2)) { $image1 = new imagick($file1); $image2 = new imagick($file2); $d1 = $image1->getImageGeometry(); $w1 = $d1['width']; $h1 = $d1['height']; $d2 = $image2->getImageGeometry(); $w2 = $d2['width']; $h2 = $d2['height']; if ($w1 == $w2 && $h1 == $h2) { $result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR); if ($result[1] > $percent) { $result[0]->setImageFormat("png"); $files[] = "{$path_key}.png"; file_put_contents("./images/output/{$path_key}.png", $result[0]); } } else { $files_error[] = "Images {$file1} and {$file2} have different sizes"; }
public function _resizeImagick(Model $model, $field, $path, $size, $geometry, $thumbnailPath) { $srcFile = $path . $model->data[$model->alias][$field]; $pathInfo = $this->_pathinfo($srcFile); $thumbnailType = $imageFormat = $this->settings[$model->alias][$field]['thumbnailType']; $isMedia = $this->_isMedia($model, $this->runtime[$model->alias][$field]['type']); $image = new imagick(); if ($isMedia) { $image->setResolution(300, 300); $srcFile = $srcFile . '[0]'; } $image->readImage($srcFile); $height = $image->getImageHeight(); $width = $image->getImageWidth(); if (preg_match('/^\\[[\\d]+x[\\d]+\\]$/', $geometry)) { // resize with banding list($destW, $destH) = explode('x', substr($geometry, 1, strlen($geometry) - 2)); $image->thumbnailImage($destW, $destH, true); $imageGeometry = $image->getImageGeometry(); $x = ($imageGeometry['width'] - $destW) / 2; $y = ($imageGeometry['height'] - $destH) / 2; $image->setGravity(Imagick::GRAVITY_CENTER); $image->extentImage($destW, $destH, $x, $y); } elseif (preg_match('/^[\\d]+x[\\d]+$/', $geometry)) { // cropped resize (best fit) list($destW, $destH) = explode('x', $geometry); $image->cropThumbnailImage($destW, $destH); } elseif (preg_match('/^[\\d]+w$/', $geometry)) { // calculate heigh according to aspect ratio $image->thumbnailImage((int) $geometry - 1, 0); } elseif (preg_match('/^[\\d]+h$/', $geometry)) { // calculate width according to aspect ratio $image->thumbnailImage(0, (int) $geometry - 1); } elseif (preg_match('/^[\\d]+l$/', $geometry)) { // calculate shortest side according to aspect ratio $destW = 0; $destH = 0; $destW = $width > $height ? (int) $geometry - 1 : 0; $destH = $width > $height ? 0 : (int) $geometry - 1; $imagickVersion = phpversion('imagick'); $image->thumbnailImage($destW, $destH, !($imagickVersion[0] == 3)); } if ($isMedia) { $thumbnailType = $imageFormat = $this->settings[$model->alias][$field]['mediaThumbnailType']; } if (!$thumbnailType || !is_string($thumbnailType)) { try { $thumbnailType = $imageFormat = $image->getImageFormat(); // Fix file casing while (true) { $ext = false; $pieces = explode('.', $srcFile); if (count($pieces) > 1) { $ext = end($pieces); } if (!$ext || !strlen($ext)) { break; } $low = array('ext' => strtolower($ext), 'thumbnailType' => strtolower($thumbnailType)); if ($low['ext'] == 'jpg' && $low['thumbnailType'] == 'jpeg') { $thumbnailType = $ext; break; } if ($low['ext'] == $low['thumbnailType']) { $thumbnailType = $ext; } break; } } catch (Exception $e) { $this->log($e->getMessage(), 'upload'); $thumbnailType = $imageFormat = 'png'; } } $fileName = str_replace(array('{size}', '{filename}', '{primaryKey}'), array($size, $pathInfo['filename'], $model->id), $this->settings[$model->alias][$field]['thumbnailName']); $destFile = "{$thumbnailPath}{$fileName}.{$thumbnailType}"; $image->setImageCompressionQuality($this->settings[$model->alias][$field]['thumbnailQuality']); $image->setImageFormat($imageFormat); if (!$image->writeImage($destFile)) { return false; } $image->clear(); $image->destroy(); return true; }
private function resizeCropImg($pFolder, $pImg, $pW, $pH) { $im = new imagick(); $im->readImage($pFolder . $pImg); $image = new stdClass(); $image->dimensions = $im->getImageGeometry(); $image->w = $image->dimensions['width']; $image->h = $image->dimensions['height']; $image->ratio = $image->w / $image->h; if ($image->w / $pW < $image->h / $pH) { $h = ceil($pH * $image->w / $pW); $y = ($image->h - $pH * $image->w / $pW) / 2; $im->cropImage($image->w, $h, 0, $y); } else { $w = ceil($pW * $image->h / $pH); $x = ($image->w - $pW * $image->h / $pH) / 2; $im->cropImage($w, $image->h, $x, 0); } $im->ThumbnailImage($pW, $pH, true); if ($img->type === "PNG") { $im->setImageCompressionQuality(55); $im->setImageFormat('png'); } elseif ($img->type === "JPG" || $img->type === "JPEG") { $im->setCompressionQuality(100); $im->setImageFormat("jpg"); } $im->writeImage($this->ad->url->folder . '/assets/' . $pImg); $im->destroy(); }