Search code examples
phpresizecropjcrop

php - imagecopyresampled


I'm trying to use jcrop to allow users to create a thumbnail of their image, it will be 190x190 pixels.

jcrop seems to be working and sending me the correct coordinates. However, imagecopyresampled seems to be acting very unpredictably and not giving me what I expect. Here is my code:

$destWidth = $destHeight = 190;
$jpeg_quality = 90;
$path = Yii::app()->basePath."/../images/user/";
$src = $path.$model->image;

$img_src = imagecreatefromjpeg($src);
$img_dest = ImageCreateTrueColor( $destWidth, $destHeight );

imagecopyresampled(
$img_dest, //destination image
$img_src, //source image
0, //top left coordinate of the destination image in x direction
0, //top left coordinate of the destination image in y direction
$_POST['x'], //top left coordinate in x direction of source image that I want copying to start at
$_POST['y'], //top left coordinate in y direction of source image that I want copying to start at
$destWidth, //190, thumbnail width
$destHeight, //190, thumbnail height
$_POST['w'], //how wide the rectangle from the source image we want thumbnailed is
$_POST['h'] //how high the rectangle from the source image we want thumbnailed is
);

imagejpeg($img_dest,$path."thumbs/test.jpg",$jpeg_quality);

I'm seriously at a loss, I've checked that the four $_POST variables are all coming in correctly, but for some reason I can't get a proper thumbnail. All I can tell for sure is that the thumbnail is usually zoomed in too much and that the top left corner I want it to start from isn't being used.


Solution

  • This is my final source code. I found out that my CSS was conflicting with my code since it allowed a max-height of 550px and a max-width of 700px. This caused images larger to have incorrect widths and/or heights. So in these cases I had to add a multiplier based on the aspect ratio of the image, and how the CSS would have resized it.

            $destWidth = $destHeight = 190;
            $jpeg_quality = 90;
    
            $path = Yii::app()->basePath."/../images/user/";
            $src = $path.$model->image;
            $img_src = imagecreatefromjpeg($src);
            $img_dest = ImageCreateTrueColor( $destWidth, $destHeight );
    
            //
            // IMPORTANT!! 
            // If you change the max-width or max-height in the css, you MUST change them here too!!
            //
            $maxWidth = 700;
            $maxHeight = 550;
    
            $srcWidth = imagesx($img_src);
            $srcHeight = imagesy($img_src);
            $srcRatio = $srcWidth/$srcHeight;
            $mult = 1;
    
            //if the image is wider than the max allowed width AND has a wider aspect ratio than a max height + max width image
            if ($srcWidth > $maxWidth && $srcRatio > $maxWidth/$maxHeight) {
                $mult = $srcWidth/$maxWidth;
            //else if the image is taller than the max height
            } else if ($srcHeight > $maxHeight) {
                $mult = $srcHeight/$maxHeight;
            }
    
            imagecopyresampled(
                $img_dest, //destination image
                $img_src, //source image
                0, //top left coordinate of the destination image in x direction
                0, //top left coordinate of the destination image in y direction
                $_POST['x']*$mult, //top left coordinate in x direction of source image that I want copying to start at
                $_POST['y']*$mult, //top left coordinate in y direction of source image that I want copying to start at
                $destWidth, //190, thumbnail width
                $destHeight, //190, thumbnail height
                $_POST['w']*$mult, //how wide the rectangle from the source image we want thumbnailed is
                $_POST['h']*$mult //how high the rectangle from the source image we want thumbnailed is
            );
    
            imagejpeg($img_dest,$path."thumbs/$model->image",$jpeg_quality);