This class extends Imagick () based on research into optimal image resizing techniques (). Using these methods with their default settings should provide image resizing that is visually indistinguishable from Photoshop’s “Save for Web…”, but at lower file sizes.
Author: David Newton (david@davidnewton.ca)
Inheritance: extends Imagick
 private function optimize($imagePath)
 {
     chmod($imagePath, 0777);
     $image = new Respimg($imagePath);
     $width = getimagesize($imagePath);
     if ($width > 54) {
         $width = 54;
     }
     $image->smartResize($width, 0, true);
     $image->cropImage($width, $width, 0, 0);
     $image->writeImage($imagePath);
     Respimg::optimize($imagePath, 0, 1, 1, 1);
 }
Example #2
0
 /**
  * Rasterizes an SVG image to a PNG.
  *
  * Uses phantomjs to save the SVG as a PNG image at the specified size.
  *
  * @access	public
  *
  * @param	string	$file			The path to the file that should be rasterized.
  * @param	string	$dest			The path to the directory where the output PNG should be saved.
  * @param	integer	$columns		The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
  * @param	integer	$rows			The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
  */
 public static function rasterize($file, $dest, $columns, $rows)
 {
     // check the input
     if (!file_exists($file)) {
         return false;
     }
     if (!file_exists($dest) || !is_dir($dest)) {
         return false;
     }
     // figure out the output width and height
     $svgTmp = new Respimg($file);
     $width = (double) $svgTmp->getImageWidth();
     $height = (double) $svgTmp->getImageHeight();
     $new_width = $columns;
     $new_height = $rows;
     $x_factor = $columns / $width;
     $y_factor = $rows / $height;
     if ($rows < 1) {
         $new_height = round($x_factor * $height);
     } elseif ($columns < 1) {
         $new_width = round($y_factor * $width);
     }
     // get the svg data
     $svgdata = file_get_contents($file);
     // figure out some path stuff
     $dest = rtrim($dest, '/');
     $filename = substr($file, strrpos($file, '/') + 1);
     $filename_base = substr($filename, 0, strrpos($filename, '.'));
     // setup the request
     $client = Client::getInstance();
     $serviceContainer = ServiceContainer::getInstance();
     $procedureLoaderFactory = $serviceContainer->get('procedure_loader_factory');
     $procedureLoader = $procedureLoaderFactory->createProcedureLoader(__DIR__);
     $client->getProcedureLoader()->addLoader($procedureLoader);
     $request = new RespimgCaptureRequest();
     $request->setType('svg2png');
     $request->setMethod('GET');
     $request->setSVG(base64_encode($svgdata));
     $request->setViewportSize($new_width, $new_height);
     $request->setRasterFile($dest . '/' . $filename_base . '-w' . $new_width . '.png');
     $request->setWidth($new_width);
     $request->setHeight($new_height);
     $response = $client->getMessageFactory()->createResponse();
     // send + return
     $client->send($request, $response);
     return true;
 }
Example #3
0
// setup
$path_raster_i = __DIR__ . '/assets/raster';
$path_raster_o = __DIR__ . '/generated/default/raster';
$path_svg_i = __DIR__ . '/assets/svg';
$path_svg_o = __DIR__ . '/generated/default/svg';
// widths
$widths = array(320, 640, 1280);
// resize raster inputs
if ($dir = opendir($path_raster_i)) {
    while (($file = readdir($dir)) !== false) {
        $base = pathinfo($file, PATHINFO_BASENAME);
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        if (in_array($ext, $exts)) {
            foreach ($widths as $w) {
                echo 'Resizing ' . $file . ' to ' . $w . '…';
                $image = new Respimg($path_raster_i . '/' . $file);
                $image->smartResize($w, 0, true);
                $image->writeImage($path_raster_o . '/' . $base . '-w' . $w . '.' . $ext);
                echo "OK\n";
            }
        }
    }
}
// rasterize SVGs
if ($dir = opendir($path_svg_i)) {
    while (($file = readdir($dir)) !== false) {
        $base = pathinfo($file, PATHINFO_BASENAME);
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        if ($ext === 'svg') {
            foreach ($widths as $w) {
                echo 'Rasterizing ' . $file . ' to ' . $w . '…';