Is there a way to have Avatar Crop create pictures with the same filename convention as Drupal core user pictures? Rather than creating the images with the filename of the image being uploaded (which may cause problems with filename overlap if not accounted for), I believe it would be better to use the same naming convention as Drupal core, i.e. "picture-1.jpg". Also, I'd feel more comfortable having avatar pictures created with the same filename as Drupal core, in case I decide to uninstall Avatar Crop later or if there is a problem with the module in relation to new Drupal version, because the images would still be usable if Avatar crop is not working or is uninstalled.

Comments

rsommer’s picture

Category: feature » bug

I'd like to have this behaviour as default because if I upload a file that do not match the drupal naming convention (picture-uid.extension) my uploaded image will be deleted on the file system after a while. I don't know the reason why or after which time, but if I rename the local image to the drupal naming convention and then upload the file via avatar crop the picture stays in the filesystem.

rsommer’s picture

Assigned: Unassigned » rsommer
Category: bug » task
Status: Active » Needs review

I've done a dirty hack on this to generate a filename that matches the drupal naming conventions. The only difference between the default behaviour is, that the old files will not be replaced. Instead I respect the renaming like this: If user with uid 5 uploads his first picture the filename will be "picture-5.jpg". If he changes the picture, the following pictures will be named like this: "picture-5_0.jpg", "picture-5_1.jpg", "picture-5_2.jpg" and so on.

Here is what I have done in the file "avatarcrop.module" in Version 6.x-1.1
search the following line of code surround line 124:

$file = file_save_upload('file_upload', $validators, $dest);

then append the following code snippet:

  global $user;
  $owner=$user->uid;
  $filetype;
  $filetype = $file->filemime; 
	$extension='';
	switch ($filetype) {
		case 'image/jpeg':
      $extension='jpg';
			break;
		case 'image/png':
      $extension='png';
			break;
		case 'image/gif':
      $extension='gif';
			break;			
	}
  $dest = variable_get('user_picture_path', 'pictures') .'/picture-'. $owner .'.'. $extension;

For the last step, change "file_copy" with "file_move" in the following line of code:
Change From:
$result = file_copy($file, $dest, FILE_EXISTS_RENAME);

To this:
$result = file_move($file, $dest, FILE_EXISTS_RENAME);

That's done the trick for me.
cheers

Coupon Code Swap’s picture

Hi rsommer. Thanks for sharing this, avatarcrop is totally usable now. I went with:

$result = file_move($file, $dest, FILE_EXISTS_REPLACE);

to replace the existing image so that the pictures folder doesn't get cluttered. The only issues I'm seeing now are that:

1. If a user goes to upload a new avatar, after uploading the image the old avatar image is still displayed for cropping and the page needs to be refreshed to show the new image.

2. After hitting the Continue button, it seems that the image that was originally uploaded is displayed on the users profile, rather than the cropped image. After refreshing the browser, the cropped image is properly displayed.

Perhaps I should file these as separate bug reports. Are you seeing the same behavior?

rsommer’s picture

Hello undoIT

Thank you for reply

I cannot confirm the same behavior on my site. If a user goes to upload a new avatar, the shown picture for cropping IS the pic that was chosen before from file system. Also after hitting continue, the correct picture is displayed in the user profile without the need of reloading the page. Perhaps it's because I'm using imagecache for image pre-processing and displaying user pics. Another issue could be the caching of the webpage. For my site I do not use any kind of drupal-caching.

A solution would be to delete first the user avatar from file system before uploading the new one. One problem could be, if the cropping process will be canceled no user avatar will be displayed anymore because of deleting it before. I'll do some research.

I do not think it's worth for filing a seperate bug

rsommer’s picture

Hi again

Here's a possible solution for your problem

search and delete the following lines of your avatarcrop.module file

  $upload_dir=variable_get('user_picture_path','pictures');
  $filepath=file_directory_path();
  $dest = $filepath . '/' .$upload_dir;
  $file = file_save_upload('file_upload', $validators, $dest);
	global $user;
  $owner=$user->uid;
  $filetype;
  $filetype = $file->filemime; 
	$extension='';
	switch ($filetype) {
		case 'image/jpeg':
      $extension='jpg';
			break;
		case 'image/png':
      $extension='png';
			break;
		case 'image/gif':
      $extension='gif';
			break;			
	}
  $dest = variable_get('user_picture_path', 'pictures') .'/picture-'. $owner .'.'. $extension; 

and replace it with the following lines:

   global $user;
  $owner=$user->uid;
  $userpicpath = db_result(db_query("SELECT picture FROM {users} WHERE uid=%d",$owner));
  file_delete($userpicpath);
  $upload_dir=variable_get('user_picture_path','pictures');
  $filepath=file_directory_path();
  $dest = $filepath . '/' .$upload_dir;
  $file = file_save_upload('file_upload', $validators, $dest);
  $filetype;
  $filetype = $file->filemime; 
	$extension='';
	switch ($filetype) {
		case 'image/jpeg':
      $extension='jpg';
			break;
		case 'image/png':
      $extension='png';
			break;
		case 'image/gif':
      $extension='gif';
			break;			
	}
  $dest = variable_get('user_picture_path', 'pictures') .'/picture-'. $owner .'.'. $extension; 

Now, the old user avatar will be first deleted and afterwards the new one will be uploaded. So ther should always appear the new uploaded pic without the need of reloading.

Please test it and give feedback

cheers

Coupon Code Swap’s picture

I tested this and I am still seeing the old avatar when attempting to replace the current avatar, and I also still see the uncropped version after being sent back to the profile page. Perhaps the old avatar image is being pulled from the browser cache.

rsommer’s picture

Hi undoIT

Perhaps a refresh should be done by the module after uploading and after cropping an image. I'll have a look, if this is possible. But I cannot promise anything. My drupal module hacking skills aren't very good yet.

For me I went back to my first solution without deleting the user pic first because of a problem interacting with imagecache. But without imagecache the second solution should be better and more space-saving.

cheers

adeel.iqbal’s picture

Status: Needs review » Closed (fixed)

fixed in later versions.