add OC_Image::fitIn

This commit is contained in:
Robin Appelman 2012-09-08 23:26:19 +02:00
parent abc930c57c
commit 697061fa9f
1 changed files with 23 additions and 15 deletions

View File

@ -543,21 +543,7 @@ class OC_Image {
$new_height = $maxsize;
}
$process = imagecreatetruecolor(round($new_width), round($new_height));
if ($process == false) {
OC_Log::write('core',__METHOD__.'(): Error creating true color image',OC_Log::ERROR);
imagedestroy($process);
return false;
}
imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
if ($process == false) {
OC_Log::write('core',__METHOD__.'(): Error resampling process image '.$new_width.'x'.$new_height,OC_Log::ERROR);
imagedestroy($process);
return false;
}
imagedestroy($this->resource);
$this->resource = $process;
$this->preciseResize(round($new_width), round($new_height));
return true;
}
@ -666,6 +652,28 @@ class OC_Image {
return true;
}
/**
* @brief Resizes the image to fit within a boundry while preserving ratio.
* @param $maxWidth
* @param $maxHeight
* @returns bool
*/
public function fitIn($maxWidth, $maxHeight) {
if(!$this->valid()) {
OC_Log::write('core',__METHOD__.'(): No image loaded', OC_Log::ERROR);
return false;
}
$width_orig=imageSX($this->resource);
$height_orig=imageSY($this->resource);
$ratio = $width_orig/$height_orig;
$newWidth = min($maxWidth, $ratio*$maxHeight);
$newHeight = min($maxHeight, $maxWidth/$ratio);
$this->preciseResize(round($newWidth), round($newHeight));
return true;
}
public function destroy() {
if($this->valid()) {
imagedestroy($this->resource);