|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +require dirname(__DIR__) . '/vendor/autoload.php'; |
| 5 | + |
| 6 | +use Jcupitt\Vips; |
| 7 | + |
| 8 | +if (count($argv) != 4) { |
| 9 | + echo "usage: $argv[0] IN-FILE OUT-FILE FORMAT\n"; |
| 10 | + echo " eg.: $argv[0] ~/pics/k2.jpg x.tif .tif[tile,pyramid]\n"; |
| 11 | + exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +$in_file = fopen($argv[1], 'r'); |
| 15 | +$source = new Vips\VipsSourceCustom(); |
| 16 | +$source->onRead(function ($bufferLength) use (&$in_file) { |
| 17 | + // return 0 for EOF, -ve for read error |
| 18 | + return fread($in_file, $bufferLength); |
| 19 | +}); |
| 20 | +// seek is optional |
| 21 | +$source->onSeek(function ($offset, $whence) use (&$in_file) { |
| 22 | + if (fseek($in_file, $offset, $whence)) { |
| 23 | + return -1; |
| 24 | + } |
| 25 | + |
| 26 | + return ftell($in_file); |
| 27 | +}); |
| 28 | + |
| 29 | +// open for write and read ... formats like tiff need to be able to seek back |
| 30 | +// in the output and update bytes later |
| 31 | +$out_file = fopen($argv[2], 'w+'); |
| 32 | +$target = new Vips\VipsTargetCustom(); |
| 33 | +$target->onWrite(function ($buffer) use (&$out_file) { |
| 34 | + $result = fwrite($out_file, $buffer); |
| 35 | + if ($result === false) { |
| 36 | + // IO error |
| 37 | + return -1; |
| 38 | + } |
| 39 | + else |
| 40 | + return $result; |
| 41 | +}); |
| 42 | +// read and seek are optional |
| 43 | +$target->onSeek(function ($offset, $whence) use (&$out_file) { |
| 44 | + if (fseek($out_file, $offset, $whence)) { |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + return ftell($out_file); |
| 49 | +}); |
| 50 | +$target->onRead(function ($bufferLength) use (&$out_file) { |
| 51 | + return fread($out_file, $bufferLength); |
| 52 | +}); |
| 53 | + |
| 54 | +$image = Vips\Image::newFromSource($source); |
| 55 | +$image->writeToTarget($target, $argv[3]); |
0 commit comments