public function createVersion($imagePath, $sizeString = false) { if (strlen($this->urlAlias) < 1) { throw new \Exception('Image without urlAlias!'); } $cachePath = $this->getModule()->getCachePath(); $subDirPath = $this->getSubDur(); $fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION); if ($sizeString) { $sizePart = '_' . $sizeString; } else { $sizePart = ''; } $pathToSave = $cachePath . '/' . $subDirPath . '/' . $this->urlAlias . $sizePart . '.' . $fileExtension; BaseFileHelper::createDirectory(dirname($pathToSave), 0777, true); if ($sizeString) { $size = $this->getModule()->parseSize($sizeString); } else { $size = false; } if ($this->getModule()->graphicsLibrary == 'Imagick') { $image = new \Imagick($imagePath); $image->setImageCompressionQuality(100); if ($size) { if ($size['height'] && $size['width']) { $image->cropThumbnailImage($size['width'], $size['height']); } elseif ($size['height']) { $image->thumbnailImage(0, $size['height']); } elseif ($size['width']) { $image->thumbnailImage($size['width'], 0); } else { throw new \Exception('Something wrong with this->module->parseSize($sizeString)'); } } $image->writeImage($pathToSave); } else { $image = new \abeautifulsite\SimpleImage($imagePath); if ($size) { if ($size['height'] && $size['width']) { //$image->thumbnail($size['width'], $size['height']); $image->best_fit($size['width'], $size['height']); } elseif ($size['height']) { $image->fit_to_height($size['height']); } elseif ($size['width']) { $image->fit_to_width($size['width']); } else { throw new \Exception('Something wrong with this->module->parseSize($sizeString)'); } } //WaterMark if ($this->getModule()->waterMark) { if (!file_exists(Yii::getAlias($this->getModule()->waterMark))) { throw new Exception('WaterMark not detected!'); } $wmMaxWidth = intval($image->get_width() * 0.4); $wmMaxHeight = intval($image->get_height() * 0.4); $waterMarkPath = Yii::getAlias($this->getModule()->waterMark); $waterMark = new \abeautifulsite\SimpleImage($waterMarkPath); if ($waterMark->get_height() > $wmMaxHeight or $waterMark->get_width() > $wmMaxWidth) { $waterMarkPath = $this->getModule()->getCachePath() . DIRECTORY_SEPARATOR . pathinfo($this->getModule()->waterMark)['filename'] . $wmMaxWidth . 'x' . $wmMaxHeight . '.' . pathinfo($this->getModule()->waterMark)['extension']; //throw new Exception($waterMarkPath); if (!file_exists($waterMarkPath)) { $waterMark->fit_to_width($wmMaxWidth); $waterMark->save($waterMarkPath, 100); if (!file_exists($waterMarkPath)) { throw new Exception('Cant save watermark to ' . $waterMarkPath . '!!!'); } } } $image->overlay($waterMarkPath, 'bottom right', 0.5, -10, -10); } $image->save($pathToSave, 100); } return $image; }
function serve($width, $height, $person) { $app = \Slim\Slim::getInstance(); $response = $app->response(); $response['Content-Type'] = 'image/jpeg'; $img = new abeautifulsite\SimpleImage(getBestImage($width, $height, $person)); if ($img->get_width() / $img->get_height() >= $width / $height) { $img->fit_to_height($height); $centre = round($img->get_width() / 2); $x1 = $centre - $width / 2; $x2 = $centre + $width / 2; $img->crop($x1, 0, $x2, $height); } else { $img->fit_to_width($width); $centre = round($img->get_height() / 2); $y1 = $centre - $height / 2; $y2 = $centre + $height / 2; $img->crop(0, $y1, $width, $y2); } $img = applyFilters($img); $img->output(); }
public function createVersion($imagePath, $sizeString = false, $crop = true, $fitImageInCanvas = false) { if (strlen($this->urlAlias) < 1) { throw new \Exception('Image without urlAlias!'); } $cachePath = $this->getModule()->getCachePath(); $subDirPath = $this->getSubDur(); $fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION); if ($sizeString) { $sizePart = '_' . $sizeString; } else { $sizePart = ''; } $pathToSave = $cachePath . '/' . $subDirPath . '/' . $this->urlAlias . $sizePart . '.' . $fileExtension; BaseFileHelper::createDirectory(dirname($pathToSave), 0777, true); if ($sizeString) { $size = $this->getModule()->parseSize($sizeString); } else { $size = false; } if ($this->getModule()->graphicsLibrary == 'Imagick') { // Fixes interlaced images $interlaceFix = $this->interlaceFix($imagePath); if ($interlaceFix) { $image = new \Imagick(); $image->readImageBlob($interlaceFix); } else { $image = new \Imagick($imagePath); } $image->setImageCompressionQuality(100); // Fixes image rotations $this->ImagickAutoRotateImage($image); // If the dimensions of the original image match the requested // dimensions the original image is just copied to the new path if ($image->getImageWidth() == $size['width'] && $image->getImageHeight() == $size['height']) { copy($imagePath, $pathToSave); return $image; } if ($size) { if ($size['height'] && $size['width']) { if (!$crop && $fitImageInCanvas) { $image = $this->fitImageInCanvas($image, $size['width'], $size['height']); } elseif ($crop) { $image->cropThumbnailImage($size['width'], $size['height']); } else { $image->resizeImage($size['width'], $size['height'], \Imagick::FILTER_HAMMING, 1, false); } } elseif ($size['height']) { $image->thumbnailImage(0, $size['height']); } elseif ($size['width']) { $image->thumbnailImage($size['width'], 0); } else { throw new \Exception('Something wrong with this->module->parseSize($sizeString)'); } } $image->writeImage($pathToSave); } else { $image = new \abeautifulsite\SimpleImage($imagePath); // If the dimensions of the original image match the requested // dimensions the original image is just copied to the new path if ($image->get_width() == $size['width'] && $image->get_height() == $size['height']) { copy($imagePath, $pathToSave); return $image; } if ($size) { if ($size['height'] && $size['width']) { if ($crop) { $image->thumbnail($size['width'], $size['height']); } else { $image->resize($size['width'], $size['height']); } } elseif ($size['height']) { $image->fit_to_height($size['height']); } elseif ($size['width']) { $image->fit_to_width($size['width']); } else { throw new \Exception('Something wrong with this->module->parseSize($sizeString)'); } } //WaterMark if ($this->getModule()->waterMark) { if (!file_exists(Yii::getAlias($this->getModule()->waterMark))) { throw new Exception('WaterMark not detected!'); } $wmMaxWidth = intval($image->get_width() * 0.4); $wmMaxHeight = intval($image->get_height() * 0.4); $waterMarkPath = Yii::getAlias($this->getModule()->waterMark); $waterMark = new \abeautifulsite\SimpleImage($waterMarkPath); if ($waterMark->get_height() > $wmMaxHeight or $waterMark->get_width() > $wmMaxWidth) { $waterMarkPath = $this->getModule()->getCachePath() . DIRECTORY_SEPARATOR . pathinfo($this->getModule()->waterMark)['filename'] . $wmMaxWidth . 'x' . $wmMaxHeight . '.' . pathinfo($this->getModule()->waterMark)['extension']; //throw new Exception($waterMarkPath); if (!file_exists($waterMarkPath)) { $waterMark->fit_to_width($wmMaxWidth); $waterMark->save($waterMarkPath, 100); if (!file_exists($waterMarkPath)) { throw new Exception('Cant save watermark to ' . $waterMarkPath . '!!!'); } } } $image->overlay($waterMarkPath, 'bottom right', 0.5, -10, -10); } $image->save($pathToSave, 100); } return $image; }
/** * Resize and save an image * * @param * @return */ function resizeAndSaveImage($originalPath, $destination, $W, $H) { list($width, $height, $type, $attr) = getimagesize($originalPath); $img = new abeautifulsite\SimpleImage($originalPath); if ($width / $height < $W / $H) { //portrait $y1 = ($height * $W / $width - $H) / 2; $y2 = ($height * $W / $width - $H) / 2 + $H; $img->fit_to_width($W)->crop(0, $y1, $W, $y2)->save($destination); } else { //landscape $x1 = ($width * $H / $height - $W) / 2; $x2 = ($width * $H / $height - $W) / 2 + $W; $img->fit_to_height($H)->crop($x1, 0, $x2, $H)->save($destination); } /* if($width/$height < $W/$H){ $img->fit_to_width($W)->crop(0, ($height - $H) / 2, $W, (($height - $H) / 2) + $H)->save($destination); } else{ $img->fit_to_height($H)->crop(($width - $W) / 2, 0, (($width - $W) / 2) + $W, $H)->save($destination); } */ }