- var_dump: documentation ( source)
- memory_get_usage: documentation ( source)
<?php
class Image {
public function __construct($path) {
$this->image = imagecreatefromjpeg($path);
}
public function getSize() {
return array(imagesx($this->image), imagesy($this->image));
}
}
class ImageProxy extends Image {
protected $image;
public function __construct($path) {
$this->path = $path;
}
private function init() {
if ( ! $this->image) {
$this->image = new Image($this->path);
}
}
public function getSize() {
$this->init();
return $this->image->getSize();
}
}
$img1 = new ImageProxy('/path/to/image1.jpg');
var_dump(memory_get_usage()); // ~200Kb
$img2 = new ImageProxy('/path/to/image2.jpg');
var_dump(memory_get_usage()); // ~200Kb
$img3 = new ImageProxy('/path/to/image3.jpg');
var_dump(memory_get_usage()); // ~200Kb
$size1 = $img1->getSize();
var_dump(memory_get_usage()); // ~4Mb
$size2 = $img2->getSize();
var_dump(memory_get_usage()); // ~8Mb