Rajohan
Rajohan

Reputation: 1415

Image crop/resize returns more of the image then selected. PHP

I'm creating a image resizing tool. The selecting etc is done with JavaScript and then the values is sent to PHP. That is working perfectly fine. But, The image is not correctly resized. The new images get the correct width/height but, the selected part of the image get's "zoomed" and more of the image compared to what's selected is returned. An example on the images below.

Image width/height before resizing

Width: 280px Height: 210px

Resize Details

Resize details

Output image

Output image

<?php

    $width = 163.094;
    $height = 148.5;
    $new_width = 62.18745;
    $new_height = 69.87505;

    $dst_left = 0;
    $dst_top = 0;
    $src_left = 49.15625;
    $src_top = 39.03125;

    header('Content-Type: image/png');

    // Create a empty image
    $new_image = imagecreatetruecolor($new_width, $new_height);

    // Set background transparent
    imagefill($new_image,0,0,0x7fff0000);

    // Create image
    $image = imagecreatefrompng("../../uploads/img/imageNX4jDQ.png");

    // Create new image
    imagecopyresampled($new_image, $image, $dst_left, $dst_top, $src_left, $src_top, $new_width, $new_height, $width, $height);

    // Keep transparency
    imageAlphaBlending($new_image, true);
    imageSaveAlpha($new_image, true);

    // Output
    $test = imagepng($new_image, "../../uploads/img/test.png");

    $test;

?>

Upvotes: 0

Views: 57

Answers (1)

ypoulakas
ypoulakas

Reputation: 421

This diagram always helps me:

http://php.net/manual/en/function.imagecopyresampled.php#112742

That aside I would anticipate your JS passing through an original width of 62.18745, and height of 69.87505.

If you need to scale the image down after that then your new_width and height values will have to be adjusted in accordance to what you need them to be.

Upvotes: 2

Related Questions