Programmatically Resizing Images
Permalink 2 users found helpful
I'm working on a custom upload page where the user will be able to attach an image file that will end up in the file manager. I'm trying to reduce the size of those uploaded images by using the getThumbnail function, deleting the original file, and adding the thumbnail back to the file manager. Everything goes great until I attempt to import the thumbnail using its path.. then no go. Any help would be appreciated..
Here's the code.. I annotated the output results in the comments..
Any Thoughts?
Here's the code.. I annotated the output results in the comments..
$temppath = $_FILES['photo']['tmp_name']; // get the temp path $filename = $_FILES['photo']['name']; //get the temp name Loader::library("file/importer"); $fi = new FileImporter(); echo $temppath . " is the path "; //prints /tmp/phpHKKvZx is the path echo $filename . " is the filename "; // prints sanmansticker02.jpg is the filename $newFile = $fi->import($temppath, $filename); //upload the file to the file manager var_dump($newFile); // prints all the info for a file version object $f = $newFile->getFile(); // get file object from created file version object $im = Loader::helper('image'); $newthumb = $im->getThumbnail($f,150,300); // create a thumbnail image $pieces = explode("/", $newthumb->src); // splits out the filename $f->delete(); $pathtothumb = "/www/files/cache"; //I have tried everything here, from the entire url etc.. I'm thinking this must be where my problem is.. echo $pathtothumb . " is the path "; //prints /www/files/cache is the path
Viewing 15 lines of 18 lines. View entire code block.
Any Thoughts?
Great! Thanks for the help. I figured it had to do with that path..
I ended up needing to use the entire path + file name so the $pathtothumb ended up as follows..
For any interested parties, here was the final code, it takes a file upload named 'photo', resizes it to a max of 1000 pixels in either direction, and puts it into the file manager.
I ended up needing to use the entire path + file name so the $pathtothumb ended up as follows..
$pathtothumb = $_SERVER['DOCUMENT_ROOT'].$newthumb->src;
For any interested parties, here was the final code, it takes a file upload named 'photo', resizes it to a max of 1000 pixels in either direction, and puts it into the file manager.
$temppath = $_FILES['photo']['tmp_name']; // get the temp path $filename = $_FILES['photo']['name']; //get the temp name Loader::library("file/importer"); $fi = new FileImporter(); $newFile = $fi->import($temppath, $filename); //upload the file to the file manager $f = $newFile->getFile(); // get file object from created file version object $im = Loader::helper('image'); $newthumb = $im->getThumbnail($f,1000,1000); // create a thumbnail image no larger than 1000 pixels $f->delete(); // delete the original file object $pathtothumb = $_SERVER['DOCUMENT_ROOT'].$newthumb->src; // setup the first parameter for the import function $anotherfile = $fi->import($pathtothumb, $filename); //upload the file to the file manager
/www/files/cache is really full path?
try
$path = dirname($_SERVER['PHP_SELF']);
echo $path;
and look at output...
or, may be:
$pathtothumb = $_SERVER['DOCUMENT_ROOT']."/files/cache";