<?php
Class Geo_Img_Resize {
private $_image = null;
function __construct($originalPath, $maxDim) {
$this->process($originalPath, $maxDim);
}
private function process($path, $max) {
$imageContent = file_get_contents($path);
$image = imagecreatefromstring($imageContent);
if ($image === false) {
// Image corrupted
return;
}
$imageHeight = imagesy($image);
$imageWidth = imagesx($image);
$newDim = null;
if ($imageHeight > $imageWidth) {
// Potrait
$newDim = array(
'height' => (int) $max,
'width' => round($imageWidth * $max / $imageHeight)
);
} else {
// Landscape
$newDim = array(
'height' => round($imageHeight * $max / $imageWidth),
'width' => $max
);
}
$newImage = imageCreateTrueColor($newDim['width'], $newDim['height']);
imagecopyresized(
$newImage, $image, 0,0,0,0,$newDim['width'],
$newDim['height'],$imageWidth,$imageHeight
);
$this->_image = $newImage;
}
public function save($path) {
if ($this->_image !== null) {
imagepng($this->_image, $path);
}
}
public function display() {
imagepng($this->_image);
}
}
?>
Tags
Image resize php
Most popular snippets