P
P
Pychev Anatoly2018-12-19 23:59:33
PHP
Pychev Anatoly, 2018-12-19 23:59:33

Why does imagecropauto create a black bar at the bottom of an image?

Hello
There is a banal task. Bring all images to the same size ratio.
Such an algorithm was adopted using the GD
library 1 - crop the original image from extra margins
2 - recalculate new sizes taking into account the desired ratio
3 - create a new image, fill it with white
4 - copy the cropped image to a new image
And that's it OK, BUT the imagecropauto
function draws a black line at the bottom for some reason.

Here is the code
$destImage = __DIR__ . '/064-493-1101.jpg';
$orig_img = imagecreatefromjpeg($destImage);
// обрезаем
$cropped_img = imagecropauto($orig_img , IMG_CROP_THRESHOLD, null, 16777215);
imagejpeg( $cropped_img,  __DIR__ . '/064-493-1101-crop.jpg');
  //Get image width / height
    $crp_w = ImageSX($cropped_img);
    $crp_h = ImageSY($cropped_img);
  
  $crp_ratio = $crp_h / $crp_w;
  
  $ratio = 1.44;
  if ( $crp_ratio < $ratio ) {
    $new_h = $crp_w * $ratio;
    $new_w = $crp_w;
    $dst_x = 0;
    $dst_y = ($new_h - $crp_h) / 2;
  } else {
    $new_h = $crp_h;
    $new_w = $crp_h / $ratio;
    $dst_x = ($new_w - $crp_w) / 2;
    $dst_y = 0;
  }
  
        // создаем основу
  $canvas = imagecreatetruecolor($new_w, $new_h);
  $white = imagecolorallocate($canvas, 255, 255, 255);
  imagefilledrectangle($canvas, 0, 0, $new_w, $new_h, $white);

  // сливаем картинки
  imagecopyresampled($canvas, $cropped_img, $dst_x, $dst_y, 0, 0, $crp_w, $crp_h, $crp_w, $crp_h);
  
  imagedestroy($cropped_img);
  imagedestroy($orig_img);
  
  
imagejpeg( $canvas,  __DIR__ . '/064-493-1101-1.jpg');
original image
knap9tq1rqwmkotfjpb1cuxwula.jpeg
cropped image
yhqaaul3rzcljr5_f1tmgu2mayo.jpeg
finite, taking into account the relation
ykbvil4m3otmbaodayfg8viwthm.jpeg

maybe I'm missing something. Tell me who knows
Thank you in advance

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question