JamesHeinrich / phpThumb

phpThumb() - The PHP thumbnail generator
Other
315 stars 98 forks source link

Prevent image resize if it smaller than w and h #88

Closed Kila14 closed 6 years ago

Kila14 commented 6 years ago

Hello. If image width and height values smaller than w and h values than image will stretch to w and h values. It's bug, or it's a normal behavior? And how we can prevent this?

JamesHeinrich commented 6 years ago

Normal behaviour is to proportionately resize larger images smaller to fit in a box of WxH. Images smaller than WxH will not be enlarged to fit, they will be returned at their original size

If the "aoe" (Allow Output Enlargement) parameter is set then smaller images will be enlarged proportionately to fit inside the WxH box (but may not be exactly WxH if the aspect ratio of the source image does not match the aspect ratio of WxH). http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x3

If the "far" (Force Aspect Ratio) parameter is set then the image will be stretched to fit exactly WxH, even if the target and source aspect ratios are different. http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x22

If the "iar" (Ignore Aspect Ratio) parameter is set then the image will be resized proportionately to fit inside WxH box, and then padded with background color ("bg" parameter) to fit exactly WxH. http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x22

If the "zc" (Zoom-Crop) parameter is set then the image will be resized proportionately to fit the smallest dimension inside WxH. If the target and source aspect ratios are different then the longer dimension will be cropped. http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x23

Kila14 commented 6 years ago

JamesHeinrich, thank you for taking the time to help me! Your PHP thumbnail generator is really good.

So, there is 2 way for small images (smaller than WxH): 1) Fill in WxH box (with defferent options). 2) Return as is. There will be bad picture quality at the first way. At the same time phpThumb will not work at all in the second way, because we get output img = input img. But how can we get this: if image is smaller than WxH then output image width = W, height = H and our small image position = center of image (with adding bg color)? The best parameter for this is "far". But it stretches the image fit WxH every time even when image is very small :(

JamesHeinrich commented 6 years ago

Using for example img.jpg = 100x75 phpThumb.php?src=img.jpg&w=200&h=200 -- your thumbnail will be returned as 100x75 phpThumb.php?src=img.jpg&w=200&h=200&aoe=1 -- your thumbnail will be returned as 200x150 but bad quality (enlarged to 200%) phpThumb.php?src=img.jpg&w=200&h=200&zc=1 -- your thumbnail will be returned as 100x75 (Zoom-Crop is only useful when the source image is larger in both W and H than the box) phpThumb.php?src=img.jpg&w=200&h=200&aoe=1&zc=1 -- your thumbnail will be returned as 200x200 but bad quality and cropped (enlarged to 267% and cropped left/right) phpThumb.php?src=img.jpg&w=200&h=200&iar=1 -- your thumbnail will be returned as 200x200 but bad quality and also distorted/stretched phpThumb.php?src=img.jpg&w=200&h=200&far=1 -- your thumbnail will be returned as 200x200 with your 100x75 image centered in a white background (you can change the background color with the bg parameter and hex color, e.g. bg=FF99CC)

Kila14 commented 6 years ago

phpThumb.php?src=img.jpg&w=200&h=200 -- your thumbnail will be returned as 100x75 phpThumb.php?src=img.jpg&w=200&h=200&aoe=1 -- your thumbnail will be returned as 200x150 but bad quality (enlarged to 200%) phpThumb.php?src=img.jpg&w=200&h=200&zc=1 -- your thumbnail will be returned as 100x75 (Zoom-Crop is only useful when the source image is larger in both W and H than the box) phpThumb.php?src=img.jpg&w=200&h=200&aoe=1&zc=1 -- your thumbnail will be returned as 200x200 but bad quality and cropped (enlarged to 267% and cropped left/right) phpThumb.php?src=img.jpg&w=200&h=200&iar=1 -- your thumbnail will be returned as 200x200 but bad quality and also distorted/stretched

All this methods are useless for small images((

phpThumb.php?src=img.jpg&w=200&h=200&far=1 -- your thumbnail will be returned as 200x200 with your 100x75 image centered in a white background (you can change the background color with the bg parameter and hex color, e.g. bg=FF99CC)

It's not that. Because 100x75 image will not be centered in 200x200 image, it's just will be stretched to 200x200. Your example: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x22

JamesHeinrich commented 6 years ago

No. Please note there are two example images at demo 22, "far" (centres original small image in background padding) and "iar" (stretches image to fill box). Please look at the first example, you want to Force Aspect Ratio: phpThumb.php?src=img.jpg&w=200&h=200&far=1

Kila14 commented 6 years ago

JamesHeinrich, sory for my misunderstanding. To do this: dtydyjydjy I need use this: phpThumb.php?src=img.jpg&w=200&h=200&far=1 ?

JamesHeinrich commented 6 years ago

Yes. Except it should probably be phpThumb.php?src=img.jpg&w=200&h=200&far=1&bg=000000 if you want a black background.

Kila14 commented 6 years ago

I use MODX 2.5.8 And there is phpThumb 1.7.14-201608101311 I wrote a snippet to display the resulting pictures.

<?php
if (!isset($modx)) {
    define('MODX_API_MODE', true);

    $modx->getService('error','error.modError');
    $modx->setLogLevel(modX::LOG_LEVEL_ERROR);
    $modx->setLogTarget('FILE');
}

$dst = MODX_BASE_PATH.'img_thumb_small.jpg';

$params = array(
    'w' => 300,
    'h' => 300,
    'bg' => '000000',
    'q' => 90,
    'far' => '1'
);

$phpThumb = $modx->getService('modphpthumb','modPhpThumb', MODX_CORE_PATH . 'model/phpthumb/', array());
$phpThumb->setSourceFilename($src);
foreach ($params as $k => $v) {
    $phpThumb->setParameter($k, $v);
}

if ($phpThumb->GenerateThumbnail()) {
    if (!$phpThumb->renderToFile($dst)) {
        $modx->log(modX::LOG_LEVEL_ERROR, 'Could not save rendered image to'.$dst);
    }
}
else {
    $modx->log(modX::LOG_LEVEL_ERROR, print_r($phpThumb->debugmessages, 1));
}

This is result: https://beauty-shtuchki.ru/phpthumb The second image stretched to 300x300 box.

JamesHeinrich commented 6 years ago

Without knowing what the original image looks like it's hard to comment.

Kila14 commented 6 years ago

Added original images before output images.

JamesHeinrich commented 6 years ago

You might want to cycle through the debug log to see exactly what's happening.

foreach ($this->debugmessages as $debugstring) {
    echo $debugstring.'<br>';
}

(warning, it will output a lot of information...)

Kila14 commented 6 years ago

Did it.

JamesHeinrich commented 6 years ago

The debug messages you have are for the large image, not the small one.

Kila14 commented 6 years ago

Sorry. Fixed.

JamesHeinrich commented 6 years ago

It's a bit hard to trace through because you're using an old version and the line numbers don't match up. Any chance you can update to the lastest version of phpThumb (v1.7.15-201704271708) please?

Kila14 commented 6 years ago

Yes of course. I'll update phpThumb and i'll message the result here. JamesHeinrich, thank you very much for your help me!

Kila14 commented 6 years ago

I did it. But it still the same as was. May be it's because i replaced not all files. The following have been replaced: tkrkydtkytdk

header('Location: ./demo/'); was replaced on header('Location: /'); in index.php phpThumb.config.php.default was renamed to phpThumb.config.php

JamesHeinrich commented 6 years ago

It turns out there was some missing code -- it would behave as intended if ImageMagick was not available, but if ImageMagick was available it would incorrectly stretch the image. Please try updating to the latest version again (1.7.15-201709210936), should only be phpthumb.class.php that changed. Fixed in https://github.com/JamesHeinrich/phpThumb/commit/bf1d1d021dbce34c4bacb00bca44f402f4cbf1b0

Kila14 commented 6 years ago

It works correctly with updated phpthumb.class.php! Thank you very much, James!!! You are a excellent specialist.

JamesHeinrich commented 6 years ago

Thanks for reporting the bug and your patience in testing it.