PHP's imagecopyresampled () function hacking in the wrong place

2

I have already inverted the parameters several times to check if I was passing them wrong, but I did not succeed. What is happening is that the crop is carried out in a different location than specified. I'm using jQuery and the jCrop plugin to find the coordinates. On the client side everything works fine.

As in the image below:

Iselectedtheareathatisintheprintaboveanditcroppedinatotallydifferentlocation:

Jcrop startup

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

JSFiddle

This is PHP script where I 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;
    }
    
asked by anonymous 18.11.2014 / 00:18

2 answers

5

I think my comments above did not work, JCrop already automatically handles the scale, according to documentation . I removed the comments (and soon removed this warning as well).

I think you are passing the wrong dimensions of the cut 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

You have had to subtract the original position so that you have the height and width. Or use the height and width that JCrop has already calculated for you, 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);
    
18.11.2014 / 01:30
2

Looking at the code in crop.php in the examples of the jCrop plugin, 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 is the graphical example of what happens in the code of the imagecopyresampled of the sample code:  

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

    
18.11.2014 / 02:38