R
R
realnin2015-11-06 00:35:54
PHP
realnin, 2015-11-06 00:35:54

How to resize an image by height?

How to resize images by height, i.e. get a fixed height of the images at the output, but without specifying the width, so that the aspect ratio is preserved
resize works this way, only a fixed width is obtained

//Создание миниатюры
                $tmb = new thumbnail($album_dir.$image_rename.$res_type);
                $tmb->size_auto(300);
                $tmb->jpeg_quality('85');
                $tmb->save($album_dir.'viss_'.$image_rename.$res_type);

where 300 is the width of the picture, and I need a height instead of this width ...
That is, I want to get a fixed height and different widths at the output
function
function size_auto($size = 100, $site = 0, $jqCrop = 0) {

    $size = explode ("x", $size);
    
    if($jqCrop){

      return $this->jqCrop( intval($size[0]), intval($size[1]), $jqCrop);
      
    } else if ( count($size) == 2 ) {
      $size[0] = intval($size[0]);
      $size[1] = intval($size[1]);
      return $this->crop( intval($size[0]), intval($size[1]) );

    } else {
      $size[0] = intval($size[0]);
      return $this->scale( intval($size[0]), $site);

    }

  }

  function crop($nw, $nh) {

    $w = $this->img['lebar'];
    $h = $this->img['tinggi'];

    if( $w <= $nw AND $h <= $nh ) {
      $this->img['lebar_thumb'] = $w;
      $this->img['tinggi_thumb'] = $h;
      return 0;
    }

    $nw = min($nw, $w);
    $nh = min($nh, $h);

    $size_ratio = max($nw / $w, $nh / $h);

    $src_w = ceil($nw / $size_ratio);
    $src_h = ceil($nh / $size_ratio);

    $sx = floor(($w - $src_w)/2);
    $sy = floor(($h - $src_h)/2);

    $this->img['des'] = imagecreatetruecolor($nw, $nh);

    if ( $this->img['format'] == "PNG" ) {
      imagealphablending( $this->img['des'], false);
      imagesavealpha( $this->img['des'], true);
    }

    imagecopyresampled($this->img['des'],$this->img['src'],0,0,$sx,0,$nw,$nh,$src_w,$src_h);

    $this->img['src'] = $this->img['des'];
    return 1;
  }
  
  function jqCrop($nw, $nh, $cropData) {
    $cropDataExp = explode('|', $cropData);
    $left = $cropDataExp[0];
    $top = $cropDataExp[1];
    
    if(!$left OR $left <= 0) $left = 0;
    if(!$top OR $top <= 0) $top = 0;

    if($nw < 100) $nw = 100;
    if($nh < 100) $nh = 100;
    
    $w = $this->img['lebar'];
    $h = $this->img['tinggi'];

    if( $w <= $nw AND $h <= $nh ) {
      $this->img['lebar_thumb'] = $w;
      $this->img['tinggi_thumb'] = $h;
      return 0;
    }

    $nw = min($nw, $w);
    $nh = min($nh, $h);

    $size_ratio = max($nw / $w, $nh / $h);

    $src_w = ceil($nw / $size_ratio);
    $src_h = ceil($nh / $size_ratio);

    $this->img['des'] = imagecreatetruecolor($nw, $nh);

    if ( $this->img['format'] == "PNG" ) {
      imagealphablending( $this->img['des'], false);
      imagesavealpha( $this->img['des'], true);
    }

    imagecopyresampled($this->img['des'], $this->img['src'], 0, 0, $left, $top, $nw, $nh, $nw, $nh);

    $this->img['src'] = $this->img['des'];
    
    return 1;
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlikDex, 2015-11-06
@AlikDex

function size_auto($size = 100, $site = 0, $jqCrop = 0) {
    //....
}

$site = 0 seems to be responsible for the underlying vertical or horizontal.
Those. I guess
it should scale in height.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question