Working with already uploaded images

Greetings

I have been able to resize and copy images while they are being uploaded, but once they are stored on the server, I can't figure out how to work with an ElggFile object to copy and modify user's images that are already uploaded.

To work with an image that is being uploaded, one can use the following pattern:

 

$imagehandler = new ElggFile();
$imagehandler->owner_guid = $user_guid;
$imagehandler->access_id = 2;
$imagehandler->image_prefix = $image_prefix;
$imagehandler->setFilename($image_prefix . $image_ext);
$imagehandler->extention = $image_ext;
$imagehandler->open("write");
$imagehandler->write(get_uploaded_file('photo_file_upload'));
$imagehandler->close();

So, the new image has been written and now has a guid. If I want to copy the image to another location or modifiy it, how would I reaccess it? I've tried:

$image new ElggFile($guid);
$image->open("read");
$contents = $image->read($image->getFileSize());

Now that I have the contents of the old image, do I need to create a new ElggFile and write the contents of the old image in a new location? Do I need to read in the whole file in order to copy it?

I would just use php functions to copy and resize the image however, I can't seem to access the path value where Elgg stores uploaded images (i.e. /home/user/elggfiles/2010/12/25/folder/image.jpg). I want to access the date/user id part of the path, $image->getFilename() just returns the folder/image.jpg part of the path.

Thank you everyone!

 

  • There's got to be a quick and fast way to do this, I'm going to be processing lots of images in a batch. Any thoughts on this?

  •  

    correction to first post:
    $contents = $image
    ->read($image->size());


    Also, I've now been successful with reading in the contents, using: 

    $image_filename=$image->getFilenameOnFilestore();
    $myNewImage = get_resized_image_from_existing_file($image_filename,....);

    //my new path to write to outside of the filestore
    $file
    = "somepath/someimagename.jpg";

    //then just using PHP's file writting functions I wrote the resized image to the new location
    $fp
    = fopen($file, 'a');
    $write = fwrite($fp, $myNewImage);
    fclose($fp);

    Now, using .htaccess mod_rewrites and my own little image serving script, I can load images securely without having to load the entire Elgg engine for each image 

     

     

     

  • Whats the purpose of copying the same file and wasting the server resource? Why cant you reuse the same image for your different purpose?

  • kinda true @ that comment. why merely (over) copy -> just point to orig image shud work fine !
    it will help decide what is needed if you claify or elaborate on the '..or modifiy it" part - the functional requirement ?

  • thanks for the replies

    i need to resize and crop the original image, then write the new image to a folder outside of the filestore. outside the filestore so that i don't have to load the whole elgg engine to serve each image. i've written a simple script that serves the images and i am using .htaccess to obscure the true location of the images