crop and resize in a single step when creating thumbnail

also so more explicit memory cleanup
This commit is contained in:
Robin Appelman 2012-03-26 23:53:48 +02:00
parent 3f881f1ca5
commit 73c6db5c8e
3 changed files with 27 additions and 11 deletions

View File

@ -72,6 +72,9 @@ class OC_Gallery_Photo {
$save_dir .= dirname($image_name). '/';
$image_path = $image_name;
$thumb_file = $save_dir . basename($image_name);
if (!is_dir($save_dir)) {
mkdir($save_dir, 0777, true);
}
if (file_exists($thumb_file)) {
$image = new OC_Image($thumb_file);
} else {
@ -81,17 +84,15 @@ class OC_Gallery_Photo {
}
$image = new OC_Image($image_path);
if ($image->valid()) {
$image->centerCrop();
$image->resize(200);
$image->centerCrop(200);
$image->fixOrientation();
if (!is_dir($save_dir)) {
mkdir($save_dir, 0777, true);
}
$image->save($thumb_file);
}
}
if ($image->valid()) {
return $image;
}else{
$image->destroy();
}
return null;
}

View File

@ -79,6 +79,7 @@ class OC_Gallery_Scanner {
if ($image && $image->valid()) {
imagecopyresampled($thumbnail, $image->resource(), $i*200, 0, 0, 0, 200, 200, 200, 200);
}
$image->destroy();
}
imagepng($thumbnail, OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/' . $albumName.'.png');
imagedestroy($thumbnail);

View File

@ -216,7 +216,7 @@ class OC_Image {
OC_Log::write('core','OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG);
return false;
}
$exif = @exif_read_data($this->filepath, 'IFD0');
$exif = @exif_read_data($this->filepath, 'IFD0');
if(!$exif) {
return false;
}
@ -267,6 +267,7 @@ class OC_Image {
if($res) {
if(imagealphablending($res, true)) {
if(imagesavealpha($res, true)) {
imagedestroy($this->resource);
$this->resource = $res;
return true;
} else {
@ -490,9 +491,10 @@ class OC_Image {
/**
* @brief Crops the image to the middle square. If the image is already square it just returns.
* @param int maximum size for the result (optional)
* @returns bool for success or failure
*/
public function centerCrop() {
public function centerCrop($size=0) {
if(!$this->valid()) {
OC_Log::write('core','OC_Image->centerCrop, No image loaded', OC_Log::ERROR);
return false;
@ -512,13 +514,20 @@ class OC_Image {
$y = ($height_orig/2) - ($height/2);
$x = 0;
}
$process = imagecreatetruecolor($width, $height);
if($size>0){
$targetWidth=$size;
$targetHeight=$size;
}else{
$targetWidth=$width;
$targetHeight=$height;
}
$process = imagecreatetruecolor($targetWidth, $targetHeight);
if ($process == false) {
OC_Log::write('core','OC_Image->centerCrop. Error creating true color image',OC_Log::ERROR);
imagedestroy($process);
return false;
}
imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $width, $height, $width, $height);
imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
if ($process == false) {
OC_Log::write('core','OC_Image->centerCrop. Error resampling process image '.$width.'x'.$height,OC_Log::ERROR);
imagedestroy($process);
@ -562,9 +571,14 @@ class OC_Image {
return true;
}
public function __destruct(){
if(is_resource($this->resource)){
public function destroy(){
if($this->valid()){
imagedestroy($this->resource);
}
$this->resource=null;
}
public function __destruct(){
$this->destroy();
}
}