Weblog Tools Collection: Removing Width/Height from the Image Uploader

Posted on May 29, 2008 by Ronald Huereca.
Categories: Contributors.
Reader Vivien writes in:
Is there a way to prevent WordPress from inserting the width and the height for images in the new 2.5 media manager?
In short, yes, but it requires you to insert some code into your theme’s functions.php file. Fortunately, there is a WordPress filter we can use called image_downsize, which takes in three arguments (a boolean, an attachment ID, and a size string).
  1. add_filter('image_downsize', 'my_image_downsize',1,3);
All I’m doing in the above filter is setting the filter name, the function to call (my_image_downsize), what priority I want the filter, and how many arguments the function takes. From there, I mimic the function image_downsize in the ‘wp-includes/media.php’ file, but do not return a width or a height. As a result, when the image is sent to the editor, no width or height is present.
  1. function my_image_downsize($value = false,$id = 0, $size = "medium") {
  2.     if ( !wp_attachment_is_image($id) )
  3.         return false;
  4.     $img_url = wp_get_attachment_url($id);
  5.     //Mimic functionality in image_downsize function in wp-includes/media.php
  6.     if ( $intermediate = image_get_intermediate_size($id, $size) ) {
  7.         $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
  8.     }
  9.     elseif ( $size == 'thumbnail' ) {
  10.         // fall back to the old thumbnail
  11.         if ( $thumb_file = wp_get_attachment_thumb_file() && $info = getimagesize($thumb_file) ) {
  12.             $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
  13.         }
  14.     }
  15.     if ( $img_url)
  16.         return array($img_url, 0, 0);
  17.     return false;
  18. }

Download the Code

Here is a sample functions.php file of the code presented in this article. I also used Andrew’s plugin generator to quickly put together a plugin that I will creatively call No Image Width or Height (download link). It will accomplish the same thing for those not comfortable with code. Just unzip, place in your WordPress plugin’s folder, and activate. Thanks Vivien for the interesting question.

Using jQuery to directly call ASP.NET AJAX page methodsEncosia

Posted on by Dave Ward.
Categories: ASP.NET, Contributors.
When it comes to lightweight client-side communication, I’ve noticed that many of you prefer ASP.NET AJAX’s page methods to full ASMX web services. In fact, page methods came up in the very first comment on my article about using jQuery to consume ASMX web services. Given their popularity, I’d like to give them their due attention. As a result of Justin’s question in those comments, I discovered that you can call page methods via jQuery. In fact, it turns out that you can even do it without involving the ScriptManager at all. In this post, I will clarify exactly what is and isn’t necessary in order to use page methods. Then, I’ll show you how to use jQuery to call a page method without using the ScriptManager.

Creating a page method for testing purposes.

Writing a page method is easy. They must be declared as static, and they must be decorated with the [WebMethod] attribute. Beyond that, ASP.NET AJAX takes care of the rest on the server side. This will be our page method for the example:
  1. public partial class _Default : Page
  2. {
  3.   [WebMethod]
  4.   public static string GetDate()
  5.   {
  6.     return DateTime.Now.ToString();
  7.   }
  8. }

What about the ScriptManager and EnablePageMethods?

Traditionally, one of your first steps when utilizing page methods is to set the ScriptManager’s EnablePageMethods property to true. Luckily, that property is a bit of a misnomer. It doesn’t enable page methods at all, but simply generates an inline JavaScript proxy for all of the appropriate methods in your page’s code-behind. For example, if a ScriptManager is added to the above example’s corresponding Default.aspx and its EnablePageMethods property is set to true, this JavaScript will be injected into the page:
  1. var PageMethods = function() {
  2.  PageMethods.initializeBase(this);
  3.  this._timeout = 0;
  4.  this._userContext = null;
  5.  this._succeeded = null;
  6.  this._failed = null;
  7. }
  8.  
  9. PageMethods.prototype = {
  10. _get_path:function() {
  11.  var p = this.get_path();
  12.  if (p) return p;
  13.  else return PageMethods._staticInstance.get_path();},
  14. GetDate:function(succeededCallback, failedCallback, userContext) {
  15. return this._invoke(this._get_path(), 'GetDate',false,{},
  16.   succeededCallback,failedCallback,userContext); }}
  17. PageMethods.registerClass('PageMethods',Sys.Net.WebServiceProxy);
  18. PageMethods._staticInstance = new PageMethods();
  19.  
  20. // Generic initialization code omitted for brevity.
  21.  
  22. PageMethods.set_path("/jQuery-Page-Method/Default.aspx");
  23. PageMethods.GetDate = function(onSuccess,onFailed,userContext) {
  24.  PageMethods._staticInstance.GetDate(onSuccess,onFailed,userContext);
  25. }
Don’t worry if you don’t understand this code. You don’t need to understand how it works. Just understand that this JavaScript proxy is what allows you to call page methods via the PageMethods.MethodName syntax. The important takeaway here is that the PageMethods proxy object boils down to a fancy wrapper for a regular ASP.NET service call.

Calling the page method with jQuery instead.

Knowing that a page method is consumed in the same way as a web service, consuming it with jQuery isn’t difficult. For more detailed information, see my previous article about making jQuery work with ASP.NET AJAX’s JSON serialized web services. Using the jQuery.ajax method, this is all there is to it:
  1. $.ajax({
  2.   type: "POST",
  3.   url: "PageName.aspx/MethodName",
  4.   beforeSend: function(xhr) {
  5.     xhr.setRequestHeader("Content-type",
  6.                          "application/json; charset=utf-8");
  7.   },
  8.   dataType: "json",
  9.   success: function(msg) {
  10.     // Do something interesting here.
  11.   }
  12. });

Putting it all together.

Corresponding to the example page method above, here’s our Default.aspx:
  1. <title>Calling a page method with jQuery</title>
  2. <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
  3. <script type="text/javascript" src="Default.js"></script>
  4. </head>
  5. <div id="Result">Click here for the time.</div>
  6. </body>
  7. </html>
As you can see, there’s no ScriptManager required, much less EnablePageMethods. As referenced in Default.aspx, this is Default.js:
  1. $(document).ready(function() {
  2.   // Add the page method call as an onclick handler for the div.
  3.   $("#Result").click(function() {
  4.     $.ajax({
  5.       type: "POST",
  6.       url: "Default.aspx/GetDate",
  7.       beforeSend: function(xhr) {
  8.         xhr.setRequestHeader("Content-type",
  9.                              "application/json; charset=utf-8");
  10.       },
  11.       dataType: "json",
  12.       success: function(msg) {
  13.         // Replace the div's content with the page method's return.
  14.         $("#Result").text(msg.d);
  15.       }
  16.     });
  17.   });
  18. });
The end result is that when our result div is clicked, jQuery makes an AJAX call to the GetDate page method and replaces the div’s text with its result.

Conclusion

Page methods are much more openly accessible than it may seem at first. The relative unimportance of EnablePageMethods is a nice surprise. To demonstrate the mechanism with minimal complications, this example has purposely been a minimal one. If you’d like to see a real-world example, take a look at Moses’ great example of using this technique to implement a master-detail drill down in a GridView. If you’re already using the ScriptManager for other purposes, there’s no harm in using its JavaScript proxy to call your page methods. However, if you aren’t using a ScriptManager or have already included jQuery on your page, I think it makes sense to use the more efficient jQuery method. Originally posted at: Encosia.

Search and Grab Icon Files at ICONlook [Icons]Lifehacker

Posted on May 27, 2008 by Kevin Purdy.
Categories: Contributors.

iconlook_scaled.jpgICONlook offers a pretty handy interface for searching and downloading icon files, whether for replacing out-of-place icons on your system or adding some graphical polish to a site. The engine's reach is somewhat limited at this point, but it helpfully provides a link to the license type for each result, when known, and seems to lean toward free and Creative Commons sources to begin with. Until Google adds icon files to their filetype:x capabilities, ICONlook is a good bet for designing your desktop.

ICONlook [via Download Squad]


Free eBook - Best of Simple Talk ASP.NETDan Wahlin's WebLog

Posted on by dwahlin.
Categories: ASP.NET, Contributors.

image

I wrote a few articles for the Simple Talk Website (run by RedGate Software) over the past year and was honored to be included in the "Best of Simple Talk ASP.NET" eBook they just released.  The book is a free PDF that covers a lot of different topics including:

  • ASP.NET Master Pages Tips and Tricks
  • Web Parts in ASP.NET 2.0
  • Implementing Waiting Pages in ASP.NET
  • Token Replacement in ASP.NET
  • Regular Expression Based Token Replacement in ASP.NET
  • A Complete URL Rewriting Solution for ASP.NET 2.0
  • Take Row-Level Control of Your GridView
  • Enhance Your Website with ASP.NET AJAX Extensions
  • Calling Cross-Domain Web Services in AJAX
  • Using Web Services with ASP.NET
  • Gathering RSS Feeds using Visual Studio and RSS.NET
  • Getting Started with XAML
  • Silverlight Skinnable User Interfaces

You can download the free eBook here.  On a side note, definitely check out RedGate's tools if you get a chance.  They have a lot of great stuff.

My ASP.NET Demo Gallery (mattberseth2.com/demo)Matt Berseth

Posted on May 26, 2008 by matt@mattberseth.com.
Categories: Contributors.

Well, I finally found some time over the long weekend to sit down and organize my 'live demo' links into a single, browsable site.  You can check it our here: http://mattberseth2.com/demo/.  It is far from perfect, but in my opinion it is much better than having all of my demo links scattered through out my blog posts.  The live demo's for all of my new work will be hooked into this page - so feel free to bookmark it.

You can filter the demo links using the 'Filter' drop down list.  So if you only want to see my GridView demo's, just select that option from the drop down list and the 'Live Demos' links will only include links to my GridView articles.  Once an item from the 'Live Demos' section is selected, the content area of the page fills with the title of the post, a short description of what the demo is showing (too short, I need to improve these descriptions), as well as a link to my original blog post and the location of where the code can be donwloaded from.  I have a few demo's that require a little more screen space than the others - for these examples you can click the Demo hyperlink and open the demo page in a new browser window.

I load the demo page into an IFRAME within the main page.  I have noticed that every once in a while this seems to cause an issue (JavaScript errors).  I will get this fixed once I track it down, but in the mean time I added a Refresh Demo link that will refresh the IFRAME. 

If you have any suggestions for the page, send me an email or post a comment.

image

Besides the new site, I also took care of a few other blog housekeeping items ...

  • I the 'Popular' links section based according to what Google Analytics says are my most popular posts.  I cheated a bit and added a link to the gallery page to the top of the list - I figured it was pretty safe to assume most folks would dig the new site.
  • I got rid of the 'Recent Posts' section on the right hand pane.  My home page (mattberseth.com) is already configured to show my 10 most recent posts, so I replaced this section with a Recent Posts link under the 'Popular' section
  • I moved the RSS, Atom and Email Me links to the title bar.  I thought this was a better use of real estate than the right hand pane.
  • I added 2 advertising sections to each of my pages.  One is located in the right hand pane between the Popular an Topics sections, and the other is at the footer of each of my pages.  I took some time styling them to make sure it matched the rest of my sites theme, but I still don't really care for how it looks.  I am going to give it a month to see what kind of return these ads give me.  Then I will decide if I am going to keep them or not.
  • I added the 'Topics' section to my archive pages so you can see what Topics an entry is filed under.

 

That's it.  Enjoy!