• Resolved marianssen

    (@marianssen)


    Hi there, we have a custom gallery where we have links to download images in specific sizes. Using the wp_get_attachment_image_url leads to the webp format (even though the URL does not lead to webp but JPG). I was wondering how to always lead the URL to the original (but custom size) image file.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Hi @marianssen,

    Thank you for your message.

    Can you provide more information on this? I would like to understand this matter better – preferably with examples.

    Best,
    Mateusz

    Thread Starter marianssen

    (@marianssen)

    @mateuszgbiorczyk thanks for the quick response. We have a gallery of images, and under each image there is an option to download the original image file or a low-res image file (resized). For the original file source, I’m using the get_the_guid function, so that correctly returns the file (in jpg format, not webp). For the low-res, I cannot use the get_the_guid function (because it does not support custom sizes), so I’m using esc_url($image[‘sizes’][‘custom-size’]. This howeverr returns the webp format (not jpg). I’d like to be able to return the original resized file (jpg) for the low-res as well. Both fields are returned using the image ID.

    Below is the (simplified version of the) code i’m using.

    <?php foreach ($images as $image) :
    $image_id = $image['id'];
    ?>
    <a download class="image-download" href="<?php echo get_the_guid($image_id); ?>" target="_blank"><?php _e('Hi-res', 'dilapan'); ?></a>
    <a download class="image-download" href="<?php echo esc_url($image['sizes']['custom-size']); ?>" target="_blank"><?php _e('Low-res', 'dilapan'); ?></a>
    <?php endforeach; ?>

    Regards,
    Marian

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @marianssen The plugin does not change the URLs to the files, so you will always get the URL to the .jpg file. Redirection to WebP files takes place when the image is loaded by the browser.

    Below I have attached the instructions:

    Thread Starter marianssen

    (@marianssen)

    @mateuszgbiorczyk so is it possible to get a direct link to a resized image that would be in the original format (not webp)? E.g. the link below directs to a jpg, but the downloaded image is in webp.

    https://www.dilapan.com/wp-content/uploads/2022/03/1224-4180-800×599.jpg

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Yes @marianssen, you can achieve the desired effect by using filters provided in plugins.

    I have prepared such a filter for you:

    add_filter( 'webpc_htaccess_mod_rewrite', function ( $rules ) {
    	return preg_replace(
    		'/(RewriteCond %{HTTP_ACCEPT} image\/(?:[a-z]+))/',
    		'$1' . PHP_EOL . '  RewriteCond %{QUERY_STRING} !(^|&)' . 'load-original' . ' [NC]',
    		$rules
    	);
    } );

    Add this to the functions.php file in your theme directory and save the plugin settings again. To display an image in its original format, add the following suffix to the URL: ?load-original.

    Does this solve your problem?

    Thread Starter marianssen

    (@marianssen)

    @mateuszgbiorczyk thank you, this worked and helped a lot!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘What is the php function to retreat the original format in custom size?’ is closed to new replies.