# Attachment\_fu resizing images already uploaded

**URL:** <https://www.ruby-forum.com/t/attachment-fu-resizing-images-already-uploaded/139254>\
**Category:** Rails\
**Created:** [June 2, 2008, 10:28am UTC](https://www.ruby-forum.com/t/attachment-fu-resizing-images-already-uploaded/139254 "2008-06-02T10:28:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [June 2, 2008, 10:28am UTC](https://www.ruby-forum.com/t/attachment-fu-resizing-images-already-uploaded/139254/1 "2008-06-02T10:28:18Z")

</div>

hi, got a query.

i’m using the attachment\_fu plugin to handle photo storage and that’s  
working fine, however now i’ve come to realise the initial size i set  
the thumbnails and full photos isn’t right, so i’ve changed the sizing  
for future uploads but need to do this for past uploads.

any ideas how i’d do this, maybe in a migration or rake task?

i’m thinking i could build a task to reimport each full photo, after  
which it deletes the original record, which is do-able.

just wondering if there’s any magic method i’m missing to do it better?

appreciate any replies on this,

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [June 2, 2008, 11:05am UTC](https://www.ruby-forum.com/t/attachment-fu-resizing-images-already-uploaded/139254/2 "2008-06-02T11:05:01Z")

</div>

since you most likely have to do it only once,  
you can do it in script/console.

Either write a small script for that task and start it in the console.  
Or add a method to the model that contains the data and start that  
within te console.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [June 24, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/attachment-fu-resizing-images-already-uploaded/139254/3 "2008-06-24T22:15:34Z")

</div>

I eventually managed to get the time to put this one together, rake  
script below…

#(run =\> ‘rake images:[task]’ or ‘rake RAILS\_ENV=production  
images:[task]’)

namespace :images do  
desc “list images”  
task(:list =\> :environment) do  
# list all images  
@photos = Image.find(:all, :conditions =\> [‘parent\_id is null’])  
@photos.each do |@photo|  
iFile = @photo.filename #@photo.public\_filename  
iPath = RAILS\_ROOT+"/public#{@photo.public\_filename}"  
iID = @photo.id.to\_s  
iUser\_ID = @photo.user\_id.to\_s  
iWidth = @photo.width.to\_s  
iHeight = @photo.height.to\_s  
puts ‘id:’+iID+’ userid:’+iUser\_ID+’ width:’+iWidth+’  
height:’+iHeight+’ file:’+iFile+’ path:’+iPath  
end  
end

desc “add test image”  
task(:add\_test =\> :environment) do  
# add image  
iFile = ‘test2.jpg’  
iPath = ‘/var/www/apps/dates/test.jpg’  
@img = Image.new(:filename =\> iFile, :content\_type =\> ‘image/jpeg’,  
:temp\_path =\> iPath, :user\_id =\> 1).save!  
end

desc “update test image”  
task(:update\_test =\> :environment) do  
# update image  
iFile = ‘test2.jpg’  
iPath = ‘/var/www/apps/dates/test2.jpg’  
@img = Image.find(‘101’)  
if @img.update\_attributes(:filename =\> iFile, :content\_type =\>  
‘image/jpeg’, :temp\_path =\> iPath, :user\_id =\> 1)  
puts iFile + ’ updated!’  
end  
end

desc “reimage all existing images - test”  
task(:reimage\_test =\> :environment) do  
# re-import all existing images - test  
@photos = Image.find(:all, :conditions =\> [‘parent\_id is null and  
user\_id = 2’])  
@photos.each do |@photo|  
iFile = @photo.filename  
iPath = RAILS\_ROOT+"/public#{@photo.public\_filename}"  
iType = @photo.content\_type  
iUser\_ID = @photo.user\_id  
if @photo.update\_attributes(:filename =\> iFile, :content\_type =\>  
iType, :temp\_path =\> iPath, :user\_id =\> iUser\_ID)  
puts iFile + ’ updated!’  
end  
end  
end

desc “reimage all existing images”  
task(:reimage =\> :environment) do  
# re-import all existing images  
@photos = Image.find(:all, :conditions =\> [‘parent\_id is null’])  
@photos.each do |@photo|  
iFile = @photo.filename  
iPath = RAILS\_ROOT+"/public#{@photo.public\_filename}"  
iType = @photo.content\_type  
iUser\_ID = @photo.user\_id  
if @photo.update\_attributes(:filename =\> iFile, :content\_type =\>  
iType, :temp\_path =\> iPath, :user\_id =\> iUser\_ID)  
puts iFile + ’ updated!’  
end  
end  
end

end

it’s a bit quick & dirty but it does the job.

all the best,

John.
