Image sizes
-
Hey, have you had to deal with images much using airtable? The thumbnails that airtable provides aren’t going to work for what I need and the full image is too big for thumbnailing purposes. Trying to decide on the best way to approach it.
-
I have a great (I think) solution just about ready to push live.
If a specific image field is specified in the AirpressQuery, all images will be downloaded to the server and arbitrary custom thumbnail sizes will be generated.
I have it all working, I’m just trying to figure out a more elegant set of API calls to make it work with all included options. For example, you may want additional thumbnail sizes cached, but not the actual image.
Oh cool! I actually made a work around today probably similar to what you have by using wp_generate_attachment_metadata.
Overview:
cacheImageFields in v1.1.15 will examine the records returned prior to caching them in a transient so that the URL in specified fields can be replaced by local URLs. The goal is not only to allow a convenient way to get “WordPress-sized” images from Airtable, but also to provide a layer of redundancy, not relying on Airtable’s image hosting (which in light of the recent massive Amazon S3 failure could be a really good thing!) This method uses as many of the WordPress API functions as possible while still allowing for complete override and customization of image sizes. Also, I wanted to avoid adding any Airtable data to the WordPress database outside of what Airpress currently does with transients.Usage:
cacheImageFields( mixed $fields [, mixed $sizes, boolean $crop = false, boolean $regenerate = false]);
$fields is a string or an array of strings of Airtable fields names you expect to contain an array of “type”:”image/*” images.
$fields = "My Field Name"
or
$fields = array("My Field Name");
$sizes is an optional string or array of strings or a multi-dimensional array.
$sizes = "full";
$sizes = array("full","thumbnail","medium","large");
$sizes = array("medium","custom-size" => array("width" => 25, "height" => 25, "crop" => true));
$crop is an optional boolean that when true will crop and when false will contain/constrain. $crop can be specified in two places—in the sizes array and in the cacheImageFields call itself. The $crop inside the $sizes array will override the $crop passed in the cacheImageFields method.
$regenerate is an optional boolean that when true will regenerate the given size and/or given fields.
Some examples:
This is a basic $query initialization:$query = new AirpressQuery("Table Name","Configuration Name"); $query->filterByFormula("NOT({Photos} = BLANK())"); $query->fields(array("Name","Photos","Alt Photos"))->maxRecords(5);
Here’s a variety of ways to use the cacheImageFields method
These are the same. They’ll cache/use the full image(s) in the {Photos} field of whatever records are returned
$query->cacheImageFields("Photos"); $query->cacheImageFields("Photos","full"); $query->cacheImageFields(array("Photos"),array("full"));
And This:
$query->cacheImageFields("Photos"); $query->cacheImageFields("Alt Photos");
Is the same as this:
$query->cacheImageFields(array("Photos","Alt Photos"));
This will use the “full” image to create the standard sizes as defined in the WordPress media settings, however it WILL NOT save/serve a local cached copy of the full image since “full” isn’t specified
$query->cacheImageFields("Photos",array("thumbnail","medium","large"));
This will create a WordPress defined “thumbnail” and “medium” image. It will NOT save the full image, but it will obviously have to download it in order to create the smaller sizes, it just won’t be written to disk. It will also create two new arbitrary sizes called “new-size” and “alt-size”. All the sizes will be hard cropped due to the “true” in the method call, except the “alt-size” image which has overridden that with a false.
$sizes = array( "thumbnail", "medium", "new-size" => array( "width" => 50, "height" => 50, ), "alt-size" => array( "width" => 120, "height" => 50, "crop" => false, ) ); $query->cacheImageFields("Photos",$sizes,true);
And, here’s the way to get results from the query:
$records = new AirpressCollection($query); foreach($records as $record){ echo $record["Name"]."\n"; foreach($record["Photos"] as $image){ echo "<img src='{$image["url"]}' />\n"; echo "<img src='{$image["thumbnails"]["small"]["url"]}' />\n"; echo "<img src='{$image["thumbnails"]["medium"]["url"]}' />\n"; echo "<img src='{$image["thumbnails"]["alt-size"]["url"]}' />\n"; echo "<img src='{$image["thumbnails"]["large"]["url"]}' />\n"; } echo "<hr>"; }
ENJOY! And please give me any suggestions, bug reports, etc! thanks so much.
Overview:
cacheImageFields in v1.1.15 will examine the records returned prior to caching them in a transient so that the URL in specified fields can be replaced by local URLs. The goal is not only to allow a convenient way to get “WordPress-sized” images from Airtable, but also to provide a layer of redundancy, not relying on Airtable’s image hosting (which in light of the recent massive Amazon S3 failure could be a really good thing!) This method uses as many of the WordPress API functions as possible while still allowing for complete override and customization of image sizes. Also, I wanted to avoid adding any Airtable data to the WordPress database outside of what Airpress currently does with transients.Usage:
cacheImageFields( mixed $fields [, mixed $sizes, boolean $crop = false, boolean $regenerate = false]);
$fields is a string or an array of strings of Airtable fields names you expect to contain an array of “type”:”image/*” images.
$fields = "My Field Name"
or
$fields = array("My Field Name");
$sizes is an optional string or array of strings or a multi-dimensional array.
$sizes = "full";
$sizes = array("full","thumbnail","medium","large");
$sizes = array("medium","custom-size" => array("width" => 25, "height" => 25, "crop" => true));
$crop is an optional boolean that when true will crop and when false will contain/constrain. $crop can be specified in two places—in the sizes array and in the cacheImageFields call itself. The $crop inside the $sizes array will override the $crop passed in the cacheImageFields method.
$regenerate is an optional boolean that when true will regenerate the given size and/or given fields.
Some examples:
This is a basic $query initialization:$query = new AirpressQuery("Table Name","Configuration Name"); $query->filterByFormula("NOT({Photos} = BLANK())"); $query->fields(array("Name","Photos","Alt Photos"))->maxRecords(5);
Here’s a variety of ways to use the cacheImageFields method
These are the same. They’ll cache/use the full image(s) in the {Photos} field of whatever records are returned
$query->cacheImageFields("Photos"); $query->cacheImageFields("Photos","full"); $query->cacheImageFields(array("Photos"),array("full"));
And This:
$query->cacheImageFields("Photos"); $query->cacheImageFields("Alt Photos");
Is the same as this:
$query->cacheImageFields(array("Photos","Alt Photos"));
This will use the “full” image to create the standard sizes as defined in the WordPress media settings, however it WILL NOT save/serve a local cached copy of the full image since “full” isn’t specified
$query->cacheImageFields("Photos",array("thumbnail","medium","large"));
This will create a WordPress defined “thumbnail” and “medium” image. It will NOT save the full image, but it will obviously have to download it in order to create the smaller sizes, it just won’t be written to disk. It will also create two new arbitrary sizes called “new-size” and “alt-size”. All the sizes will be hard cropped due to the “true” in the method call, except the “alt-size” image which has overridden that with a false.
$sizes = array( "thumbnail", "medium", "new-size" => array( "width" => 50, "height" => 50, ), "alt-size" => array( "width" => 120, "height" => 50, "crop" => false, ) ); $query->cacheImageFields("Photos",$sizes,true);
And, here’s the way to get results from the query:
$records = new AirpressCollection($query); foreach($records as $record){ echo $record["Name"]."\n"; foreach($record["Photos"] as $image){ echo $image["url"]; echo $image["thumbnails"]["small"]["url"]; echo $image["thumbnails"]["medium"]["url"]; echo $image["thumbnails"]["alt-size"]["url"]; echo $image["thumbnails"]["large"]["url"]; } echo "<hr>"; }
ENJOY! And please give me any suggestions, bug reports, etc! thanks so much.
Wow awesome, I’ll let you know if I have any issues when I get a chance to use it. Keep up the great work!
- The topic ‘Image sizes’ is closed to new replies.