Skip to content

Instantly share code, notes, and snippets.

@suderman
Created April 14, 2015 17:40
Show Gist options
  • Save suderman/8e40078263648411c556 to your computer and use it in GitHub Desktop.
Save suderman/8e40078263648411c556 to your computer and use it in GitHub Desktop.
<?php
require_once 'n_model.php';
class PhotoLibraryImage extends NModel {
function __construct() {
$this->__table = 'photo_library_image';
$this->form_ignore_fields = array('image_web_170', 'image_web_400', 'image_web_960');
$this->form_elements['image_full'] = array('cms_file', 'image_full', 'High Res Image');
$this->form_required_fields = array('photo_title', 'image_full');
$this->_order_by = 'cms_headline';
parent::__construct();
}
function beforeUpdate() {
require_once 'n_image.php';
$this->processFullImage();
$this->fetch(); // Filename may have changed...
$this->generateSizes();
}
function processFullImage(){
$image = new NImage();
$image->checkImageFormat('JPG', $this->image_full, 'photo_library_image', 'image_full', $this->id);
}
function generateSizes() {
$this->image_web_960 = $this->scale($this->image_full, 960);
$this->image_web_400 = $this->scale($this->image_full, 400);
$this->image_web_170 = $this->thumbnail($this->image_full, 170, 115);
}
function thumbnail($file, $width, $height) {
$path_parts = explode('.', $file);
$path = $_SERVER['DOCUMENT_ROOT'] . $file;
$outfile = $path_parts[0].".{$width}x{$height}.jpg";
$outpath = $_SERVER['DOCUMENT_ROOT'] . $outfile;
echo 'generating'.$outpath;
if (file_exists($outpath)) return $outfile;
$image = new Imagick();
$image->readImage("$path");
$image->cropThumbnailImage($width, $height);
$image->writeImage($outpath);
echo 'wrote'.$outpath;
$image->destroy();
return $outfile;
}
function scale($file, $width) {
$path_parts = explode('.', $file);
$path = $_SERVER['DOCUMENT_ROOT'] . $file;
$outfile = $path_parts[0].".{$width}.jpg";
$outpath = $_SERVER['DOCUMENT_ROOT'] . $outfile;
echo 'generating'.$outpath;
if (file_exists($outpath)) return $outfile;
$image = new Imagick();
$image->readImage("$path");
$image->resizeImage($width, 0, Imagick::FILTER_GAUSSIAN, 1);
$image->setImageCompressionQuality(85);
$image->writeImage($outpath);
echo 'wrote'.$outpath;
$image->destroy();
return $outfile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment