ryanscherler Posted August 17, 2013 Share Posted August 17, 2013 I recently created a Dribbble API wrapper module (I am pretty new at creating modules) and wanted to know if its possible to use the $image API to retrieve the external Dribbble "shot" (image), resize it and store them along with the XML in the asset cache. That way I can load an image locally via the cached file(s) for the module instead of hitting Dribbble every time for the full size images. Any thoughts on how to best do this? Here is the current state of the module on Github: https://github.com/ryanscherler/ProcessDribbble 4 Link to comment Share on other sites More sharing options...
horst Posted August 17, 2013 Share Posted August 17, 2013 (edited) wanted to know if its possible to use the $image API to retrieve the external Dribbble "shot" (image), resize it and store them along with the XML in the asset cache. That way I can load an image locally via the cached file(s) for the module instead of hitting Dribbble every time for the full size images. 1) you may do this by using a hidden tools page with an images field and than you only have to use $page->images->add($shotImageUrl) to save the image to the tools page assets directory. And you have it as PageImage and you can do all image api stuff 2) I have uploaded a new module for Image Manipulation. With this you are able to use some well known image methods (width, height, size) and you can store the image together with your xml or json file instead of creating and using a tools page with InputFieldtypeImage. Example (this could be somewhere in your makeRequest method): // get your image from dribble and save it to your cache directory $shotImageUrl = 'http://dribble.com/some/path/to/image.jpg'; $imgCacheFile = $this->cachePath . md5($url) . '.image.jpg'; $result = file_put_contents($imgCacheFile, file_get_contents($shotImageUrl)); // if $result is not false and greater than zero the image was successfully saved, // now you can load the $imgCacheFile into the ImageManipulator, resize it and save it by overwriting the $imgCacheFile $boolResult = wire('modules')->get('PageImageManipulator')->imLoad($imgCacheFile)->resize(360)->save(); // thats all PS: if you want rely on the PageImageManipulator module, you can set it as needed dependency for your module: public static function getModuleInfo() { return array( // ... 'requires' => array('PageImageManipulator'), ); } see: http://processwire.com/talk/topic/778-module-dependencies/ Edited August 17, 2013 by horst 6 Link to comment Share on other sites More sharing options...
ryan Posted August 20, 2013 Share Posted August 20, 2013 Ryan, glad to see you working on this module! It looks like you already figured this out, but /site/assets/cache/[your dir] is usually a good place to store things like XML cache files. If connected with a page, then just use the page's files dir ($page->filesManager->path or $page->[your files field]->path). Horst is right that you can add files to pages and manipulate them using PW's methods from there. He's also written an excellent manipulation module. If you just need to resize images, also look into PW's ImageSizer class, which is ready to operate on any file (doesn't need to be connected to a page). In the dev branch, I'm about to push some updates to that class from Horst that add sharpening and auto rotation among other tweaks. Lastly, I wanted to mention that the dev branch also has the WireHttp class, which you may find more convenient than CURL. 3 Link to comment Share on other sites More sharing options...
ryanscherler Posted August 28, 2013 Author Share Posted August 28, 2013 Hey Ryan and Horst, I am going to look into these options for sizing and saving the images locally to the module cache in addition to the already cached JSON file. Thanks for all the help! Link to comment Share on other sites More sharing options...
ryanscherler Posted August 28, 2013 Author Share Posted August 28, 2013 Hey Ryan, If I was just to use the built in PW ImageSizer class, how would I access it directly without being associated to a page field object? Link to comment Share on other sites More sharing options...
horst Posted August 28, 2013 Share Posted August 28, 2013 something like this: $im = new ImageSizer($targetPath); $im->resize($targetWidth, $targetHeight); //or $im->width($targetWidth, $options); // $options are optional! 4 Link to comment Share on other sites More sharing options...
ryanscherler Posted August 28, 2013 Author Share Posted August 28, 2013 Thanks Horst! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now