Is it possible to get local image URL when also having photon active?

So I am using Photon, and it has helped increased my loading speed.

While this is great, I am now facing a CORS error, since I am using blur.js to blur the background image of my single posts.

The image it is referencing to is the ‘thumbnail’ size of the featured image. Since the featured image is being fetched via Photon, I am receiving the ‘Cannot access local image’ error.

Here is my current code to grab the thumbnail image.

<?php $feat_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'blog-widget'); ?>

<style>
  span.img-pxl {
    background-image: url('<?php echo $feat_image_url[0] ?>');
  }
</style> 

I just want to target the thumbnail image specifically just to load from my own site, not from photon.

Is this possible? My only reference to changing featured URLs to Photon URLs is here: http://jetpack.me/2013/07/17/use-photon-to-serve-post-thumbnails-with-custom/

Though I am not sure how to reverse that to use my own site’s URL.

Thanks!
Roc.


So I’ve found a hack solution – though I know this is not the correct way to do it.

function remove_jetpack_image_downsize() {
    global $post;
    if (is_single()) return true; else return false;
}
add_filter('jetpack_photon_override_image_downsize', 'remove_jetpack_image_downsize');

Now, it removes all photon URLs on my single posts, but that is not the solution I am trying to achieve. I just really need to remove photon URLs for 50×50 images. If I use this function above, it removes photon on 600×600 images, 300×300, etc – which is what I definitely do not want.

Is there a suggestion where I can disable photon only for all images that use this code: wp_get_attachment_image_src( get_post_thumbnail_id(), 'blog-widget');


Option #2:

So I am able to get the full-size featured image URL like so:

$bkg_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single_post_thumnail' );

And it returns my website’s url (i.e. – www.mysite.com/image.jpg)

But if I change ‘single_post_thumbnail’ to one of my registered image sizes like this:

$bkg_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'blog-widget' );

It returns the photon URL (i.e. – i0.wp.com/image.jpg).

With the URL above, is it possible for me to target the image URL with the smaller resized image that I am trying to get? Or even, crop the featured image via arguments.

Thanks!
Roc.

1 Answer
1

Definitely a hack solution, but the only one I know got to work thus far…

<?php $bkg_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single_post_thumbnail' ); ?>

<style>
  span {
    background-image: url('<?php echo preg_replace("/\.jpg.*/", "-50x50.jpg", $bkg_image[0]); ?>');
  }
</style>

Basically, I am fetching the non-photon URL, and doing a string replace of the end image URL ‘.jpg’ to ‘-50×50.jpg’ (which is the size of the blog-widget image size)

If there is a better solution, that is not as hacky PLEASE share!

The only issue that I can think of is when the extension is not .jpg, but .png or gif. If there is a way I can add ‘-50×50’ before the last ‘.’, that would help I assume.

Thanks,
Roc.


Of course, posting and then finding alternate solution:

To add characters before end of extension:
https://stackoverflow.com/questions/2429611/regex-how-to-insert-string-before-file-extension

<?php 
  $bkg_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single_post_thumbnail' );  
  $extension_pos = strrpos($bkg_image[0], '.'); // find position of the last dot, so where the extension starts
  $thumb = substr($bkg_image[0], 0, $extension_pos) . '-50x50' . substr($bkg_image[0], $extension_pos);
?>

<style>
  span {
    background-image: url('<?php echo $thumb; ?>');
  }
</style>

This is a alternate fix for the first answer I posted above, of how to add a string before an extension… not one for the photon solution.

Leave a Comment