is there any way to delete the original image file after it has been manipulated. I have a site that manages 4 newspapers and it is quickly running out of space. Is it safe to manually delete the original?

Comments

adavis.co.uk’s picture

I'd also be very interested in an action to delete the original image for both space and security reasons. How about a preset called 'original' that acts on the original images?

arthursk’s picture

I second this, 20GB of space on a VPS is not enough when the original files are 2mb+

Deleting the originals seems to work as long as the imagecache is never flushed, and you never request an action on a file that no longer exists...

I think the ugly but safer solution for the time being would be running a batch on the originals to the minimum size you need and replacing them rather than deleting them...

EDIT:

Here's my attempt at resolving this issue VIA cron job.

I've compiled this script using a modified image resize class and recursive folder search found on the net.

Notes:

  • This assumes you can set your ini settings via php, depending on the time and amount of files in the folder it may need to be adjusted
  • You need the GD Library installed (assuming you already have it with imagecache and all)
  • You must set a maximum width to be scaled to
  • You can set a minimum filesize to scan and resize
  • The script only allows GIF PNG and JPG filetypes to be converted
  • The script will automatically resize and overwrite every file within a specified folder including all sub-directories

PHP Gurus feel free to tear it apart and speed it up! If you get any errors let me know.


<?php
$upload_path = 'sites/default/files/';
$img_minsize = 200000; // 200 kb
$img_max_width = 530; // px
$img_allowed_types = 'null,jpg,JPG,JPEG,jpeg,png,PNG,GIF,gif'; //first null for array_search '0' results
$img_allowed = explode(',', $img_allowed_types);

ini_set('memory_limit',			'128M');
ini_set('max_execution_time',	'360');

function get_filetype($filename) {
  $file = explode('.', $filename);
  $file = array_reverse($file);
  $img_type = $file[0];
  return $img_type;
}

function get_correct_files($filename) {
  global $img_allowed;
  global $img_minsize;
  $filetype = get_filetype($filename);
  if (array_search($filetype, $img_allowed) != NULL) :
    if (filesize($filename) >= $img_minsize) :
      return true;
    endif;
  endif;
}

class img_resizer {
	private $originalFile = '';
	public function __construct($originalFile = '') {
		$this -> originalFile = $originalFile;
	}
	public function resize($newWidth, $targetFile) {
		if (empty($newWidth) || empty($targetFile)) {
			return false;
		}

		if (preg_match('/jpg|jpeg/', $this -> originalFile)) :
		  $src = imagecreatefromjpeg($this -> originalFile);
		elseif (preg_match('/png/', $this -> originalFile)) :
		  $src = imagecreatefrompng($this -> originalFile);
		elseif (preg_match('/gif/', $this -> originalFile)) :
		  $src = imagecreatefromgif($this -> originalFile);
		endif;
	      list($width, $height) = getimagesize($this -> originalFile);
		  $newHeight = ($height / $width) * $newWidth;
		  $tmp = imagecreatetruecolor($newWidth, $newHeight);
		  imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
		  if (file_exists($targetFile)) {
		    unlink($targetFile);
		  }
		if (preg_match('/jpg|jpeg/', $this -> originalFile)) :
		  imagejpeg($tmp, $targetFile, 85);
		elseif (preg_match('/png/', $this -> originalFile)) :
		  imagepng($tmp, $targetFile, 9);
		elseif (preg_match('/gif/', $this -> originalFile)) :
		  imagegif($tmp, $targetFile);
		endif;
		imagedestroy($tmp);
	}
}

function resize_proper_img_recursive($file) {
  global $img_max_width;
  if (get_correct_files($file)) :
	$img = $file;
	$resized = new img_resizer($img);
	$resized -> resize($img_max_width, $img);
  endif;
}

function dir_walk($callback, $dir, $types = null, $recursive = true, $baseDir = '') {
  if ($dh = opendir($dir)) {
	while (($file = readdir($dh)) !== false) {
	  if ($file === '.' || $file === '..') {
	    continue;
	  } 
	  if (is_file($dir . $file)) {
	    if (is_array($types)) {
		  if (!in_array(strtolower(pathinfo($dir . $file, PATHINFO_EXTENSION)), $types, true)) {
		    continue;
		  }
	    }
		$callback($dir . $file);
		} elseif ($recursive && is_dir($dir . $file)) {
		  dir_walk($callback, $dir . $file . DIRECTORY_SEPARATOR, $types, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR);
	  }
	}
	closedir($dh);
	}
}

dir_walk('resize_proper_img_recursive', $upload_path);

?>

sheba’s picture

You can define "Maximum resolution for Images:" to some reasonable number. It will automatically resize images upon upload. It should save you lot of space. Also you can define image format (JPEG, GIF, PNG) which can be used.

1mundus’s picture

I usually delete originals manually and all of the created files in imagecache presets folders are displayed correctly. However, I'm annoyed by all of the warnings I get an therefore have two questions:

1. Is it possible to force imagecache (or filefield?) not to look for original file after preset file has been created?

2. If above is not possible, what would happen if I upload 1x1px images instead of originals? Would it change preset images? For example I visit "image_cache_preset/example.jpg" and it displays 500x400px image of a house. If I change original in files folder to 1x1 would my image_cache_preset/example.jpg be 500x400px or it would change to 1x1px?

drewish’s picture

Status: Active » Fixed

The best approach is what sheba suggest in #3. Use ImageField's scaling to reduce the size of the original down.

1mundus’s picture

I resize images before uploading so they are the same size as I want them to be. Since Drupal processes these images, they loose on quality, therefore I delete the images in imagecache preset folders and upload the "resized originals" in these folders.

The problem is that then I have two versions - one in .../files folder and the other in imagecache presets folders. I usually delete these in the files folder, since there would be more than 1gb of such files (thousands of images).

Therefore I'd like to know the logic behind imagecache module - does it recreate images using pictures from /files folder only when it detects that there are no corresponding images in imagecache preset folders or it recreates them on every page view when there is an image in /files folder?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

playfulwolf’s picture

maybe any Drupal based solution appeared?