php-vips will only read the part you need when possible. It is usually 3x to 5x faster than imagemagick and requires much less memory.
Many image formats do not allow random access. JPEG, PNG, GIF, and many others will make you decompress at least the pixels in front of the pixels you want for the huge sorting images you process will be very slow.
TIFF JPEG. ( ) 256x256 . TIFF , .
, JPEG JPEG libvips, :
$ time vips copy wac_nearside.jpg wac_nearside.tif[tile,compression=jpeg]
real 0m3.891s
user 0m6.332s
sys 0m0.198s
peak RES 40mb
, :
$ ls -l wac_nearside.*
-rw-r--r-- 1 john john 74661771 May 7 2015 wac_nearside.jpg
-rw-r--r-- 1 john john 76049323 Feb 24 15:39 wac_nearside.tif
$ vipsheader wac_nearside.jpg wac_nearside.jpg: 24000x24000 uchar, 1 band, b-w, jpegload
PHP :
<?php
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;
$image = Vips\Image::newFromFile($argv[1]);
$region_width = 100;
$region_height = 100;
for ($i = 0; $i < 100; $i++) {
$left = rand(0, $image->width - $region_width - 1);
$top = rand(0, $image->height - $region_height - 1);
$region = $image->crop($left, $top, $region_width, $region_height);
$region->writeToFile($i . ".jpg");
}
:
$ time ./crop.php ~/pics/wac_nearside.tif
real 0m0.207s
user 0m0.181s
sys 0m0.042s
peak RES 36mb
, ( ) 100 JPEG 0.2 .