PHP imagecopyresampled() function cutting in the wrong place

Asked

Viewed 1,434 times

2

I already reversed the parameters several times to check if I was passing them erroneously, but I was not successful. What is happening is that Crop is performed in a different location than specified. I am using jQuery and jCrop plugin to find the coordinates. On the client side everything works normal.

As in the image below: inserir a descrição da imagem aqui

I selected the area that is in the print above and it cropped in a totally different location: inserir a descrição da imagem aqui

Initialization of Jcrop

$('#target').Jcrop({
     onSelect: showCoords,
     onChange: showCoords,
     aspectRatio: 960/720,
     boxWidth: 600,
     boxHeight: 400,
     bgColor: '#674323'
 });

Jsfiddle

This is PHP script where I do Crop on Server-side:

  if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        $dst_x = 0;
        $dst_y = 0;
        $src_x = ceil($_POST['x']); // x1 da imagem de origem
        $src_y = ceil($_POST['y']); // y1 da imagem de origem
        $dst_w = ceil($_POST['w']); // largura da imagem de destino
        $dst_h = ceil($_POST['h']); // altura da imagem de destino
        $src_w = ceil($_POST['x2']); // x2 da imagem de origem
        $src_h = ceil($_POST['y2']); // y2 da imagem de origem

        $jpeg_quality = 100;

        $src = 'css/images/luitame.jpg';
        $img_r = imagecreatefromjpeg($src);
        $dst_r = imagecreatetruecolor($_POST['w'], $_POST['h']);
        $imageName = "css/images/thumbs/".time().'.jpg';
        imagecopyresampled($dst_r, $img_r, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
        imagejpeg($dst_r, $imageName, $jpeg_quality);

        header("location: result.php?img=$imageName");
        exit;
    }
  • 1

    Checks if the cutting position values are correct, it is assuming the cutting position as 0 x 0.

  • @touchmx I am using '0.0' because this is the destination X, Y respectively of the target image. In kids this is coordinated where the copied image will be pasted with the function. In my view this parameter is for this. Correct me if you disagree.

  • @bfavaretto can you do this by exemplifying with the function? Please. I don’t understand exactly what you wanted to give me.

  • 1

    @Has Luitame checked in the php documentation if the parameters passed are correct? http://php.net/manual/en/function.imageresampled.php

  • @touchmx checked yes, but I can’t understand clearly. This is the part where I’m in doubt.

2 answers

5


I think my comments above did not proceed, Jcrop already automatically handles the scale, according to documentation. I removed the comments (and soon remove this notice as well).


I think you’re passing the wrong dimensions of the cutting area, which are obtained here:

$src_w = ceil($_POST['x2']); // x2 da imagem de origem
$src_h = ceil($_POST['y2']); // y2 da imagem de origem

It was necessary to subtract the original position to have the height and width. Or use the height and width that Jcrop has already calculated for you by changing the last two parameters passed to $dst_w and $dst_h:

imagecopyresampled($dst_r, $img_r, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);
  • I made the change here last and did not change much. Keep cutting in the wrong place.

  • 1

    But did you change the error, or do you continue to make the same mistake? : ) I’m not sure what is happening... Do you understand the logic of my answer? The dimensions of the generated image will be equal to the dimensions of the defined area for Crop. I again find that you are having some scale problem.

  • I understood what I wanted to go through. But, still it cuts in the wrong place.

  • @Luitame When you define the cutting area, the x,y of the origin it shows is relative to the scaled image or full size? For example, if you start dialing at a 10px corner of the image, it says 10px or more?

  • And also try to remove the aspectRatio passed to Jcrop on startup (client-side).

  • 2

    I did several tests and fell there where you had spoken. The problem was a chaotic tag IMG had set a fixed height of X pixels. What complicated the scale... But it worked! I removed the height and set through a jCrop method called boxHeight. Functioning... Required

Show 1 more comment

2

Observing the code in crop.php in the jCrop plugin examples, we have the code executed when the user gives a POST:

$targ_w = $targ_h = 150;
$jpeg_quality = 90;

$src = 'demo_files/pool.jpg';
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

imagecopyresampled($dst_r,$img_r,
//destino x_y           src x_y
0,          0,          $_POST['x'],    $_POST['y'],
//destino w_h           src w_h
$targ_w,     $targ_h,     $_POST['w'],    $_POST['h']);  

Below we have the graphic example of what happens in the code of imagecopyresampled of the example code: 1

What you must change is the $src_h and the $src_w for the values $_POST['h'] and $_POST['w'] respectively.

  • Note: It seems that $_POST['x2'] and $_POST['y2'] are for internal use (or to calculate the appearance) when aspectRatio = 0, is what I’m checking at the moment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.