aflores12 / timthumb

Automatically exported from code.google.com/p/timthumb
0 stars 0 forks source link

Add option to prohibit upscaling beyond images original size #189

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
If a width or height parameter is provided which is larger than the image size, 
the image is scaled up. Most images will look terrible when scaled up, so I 
would very much appreciate an option to disable upscaling. 

Original issue reported on code.google.com by asmus...@gmail.com on 19 May 2011 at 2:15

GoogleCodeExporter commented 8 years ago
The use case for this is, for instance, my WordPress plugin "Image Symlinks": 
http://wordpress.org/extend/plugins/image-symlinks/ -- wherein I've added a 
hack that fixes this. 

The idea of the plugin is you provide ONE width for all images, usually the 
width of the main column. You then insert your images using a WordPress 
shortcode, [img src="pathtoimage.jpg"], which then through the magic of the 
plugin inserts the image through a timthumb wrapper with the global width 
parameter. Why would you do this? Because if you change your theme, instead of 
changing all inserted images, you just change the global width variable and 
timthumb will take care of the rest. 

Which obviously also means if you insert a small image, you don't want that 
scaled up. 

Original comment by asmus...@gmail.com on 19 May 2011 at 2:36

GoogleCodeExporter commented 8 years ago
so what do you imagine would happen with this parameter? If the size is larger 
than the original size just display the image? Out of interest couldn't you 
check that in your plugin?

Original comment by BinaryMoon on 10 Jun 2011 at 11:41

GoogleCodeExporter commented 8 years ago
Yes, if the desired size is larger than the image source, then simply show the 
image in its native resolution.

Yes it's possible to do this in my plugin, however that would require 
duplicating a lot of image scaling code already in TimThumb and add extra 
bloat. I think I might have actually already made such a work around. 

If you don't see the benefit of this, then perhaps it's just me -- naturally 
only add to the core what makes sense for the most users. 

Original comment by asmus...@gmail.com on 10 Jun 2011 at 12:23

GoogleCodeExporter commented 8 years ago
I made a small change to the version of timthumb that I use with my plugin to 
disable upscaling:

Change this:

  // Get original width and height
    $width = imagesx ($image);
    $height = imagesy ($image);
    $origin_x = 0;
    $origin_y = 0;

    // generate new w/h if not provided
    if ($new_width && !$new_height) {
        $new_height = floor ($height * ($new_width / $width));
    } else if ($new_height && !$new_width) {
        $new_width = floor ($width * ($new_height / $height));
    }

To this

  // Get original width and height
    $width = imagesx ($image);
    $height = imagesy ($image);
    $origin_x = 0;
    $origin_y = 0;

  // don't allow new width or height to be greater than the original      
    if( $new_width > $width ) {     
        $new_width = $width;        
    }       
    if( $new_height > $height ) {       
        $new_height = $height;      
    }   

    // generate new w/h if not provided
    if ($new_width && !$new_height) {
        $new_height = floor ($height * ($new_width / $width));
    } else if ($new_height && !$new_width) {
        $new_width = floor ($width * ($new_height / $height));
    }

I don't know if it's the most sensible way of doing i, but it seems to work for 
me. I agree, images should never be upscaled... the native WP image resize 
function doesn't allow upscaling, does it?

Original comment by dal...@madebyraygun.com on 24 Aug 2011 at 12:53

GoogleCodeExporter commented 8 years ago
I think this would be a desirable feature for many websites. Adding it by 
default would obviously break behaviour on existing sites so would be better to 
allow an extra boolean parameter, for example "up" that can be set to 
true/false 1/0 to allow or disallow upscaling.

Original comment by cont...@robertchurchill.co.uk on 4 Apr 2012 at 1:11

GoogleCodeExporter commented 8 years ago
i really need this too. smaller images looks awful coming out distorted of 
timthumb.

Original comment by alhose...@gmail.com on 27 Dec 2012 at 9:54

GoogleCodeExporter commented 8 years ago
this code works. thank you man.

Change this:

  // Get original width and height
    $width = imagesx ($image);
    $height = imagesy ($image);
    $origin_x = 0;
    $origin_y = 0;

    // generate new w/h if not provided
    if ($new_width && !$new_height) {
        $new_height = floor ($height * ($new_width / $width));
    } else if ($new_height && !$new_width) {
        $new_width = floor ($width * ($new_height / $height));
    }

To this

  // Get original width and height
    $width = imagesx ($image);
    $height = imagesy ($image);
    $origin_x = 0;
    $origin_y = 0;

  // don't allow new width or height to be greater than the original      
    if( $new_width > $width ) {     
        $new_width = $width;        
    }       
    if( $new_height > $height ) {       
        $new_height = $height;      
    }   

    // generate new w/h if not provided
    if ($new_width && !$new_height) {
        $new_height = floor ($height * ($new_width / $width));
    } else if ($new_height && !$new_width) {
        $new_width = floor ($width * ($new_height / $height));
    }

Original comment by alhose...@gmail.com on 27 Dec 2012 at 10:00

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Up-scaling is great :( For 320x160px image make a 640x320 up-scaled version 
with quality about 50 or less and check out size!!! and how it looks!!! and how 
it looks on retinas ;)

Original comment by kolorowe...@gmail.com on 6 Jan 2015 at 7:47