php-imagecopyresampled

php-imagecopyresampled,php,resize,crop,jcrop,Php,Resize,Crop,Jcrop,我正在尝试使用jcrop来允许用户创建其图像的缩略图,它将是190x190像素 jcrop似乎正在工作并向我发送正确的坐标。然而,imagecopyresampled似乎表现得非常不可预测,没有给我我所期望的。这是我的密码: $destWidth = $destHeight = 190; $jpeg_quality = 90; $path = Yii::app()->basePath."/../images/user/"; $src = $path.$model->image; $

我正在尝试使用
jcrop
来允许用户创建其图像的缩略图,它将是
190x190像素

jcrop
似乎正在工作并向我发送正确的坐标。然而,
imagecopyresampled
似乎表现得非常不可预测,没有给我我所期望的。这是我的密码:

$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);

我真是不知所措,我检查了四个
$\u POST
变量是否都正确输入,但由于某些原因,我无法获得正确的缩略图。我所能确定的是,缩略图通常放大过多,并且我希望它从左上角开始的位置没有被使用。

这是我的最终源代码。我发现我的CSS与我的代码冲突,因为它允许最大高度为550px,最大宽度为700px。这导致较大的图像具有不正确的宽度和/或高度。因此,在这些情况下,我必须根据图像的纵横比以及CSS如何调整其大小添加一个乘数

        $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);

这是我最后的源代码。我发现我的CSS与我的代码冲突,因为它允许最大高度为550px,最大宽度为700px。这导致较大的图像具有不正确的宽度和/或高度。因此,在这些情况下,我必须根据图像的纵横比以及CSS如何调整其大小添加一个乘数

        $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);

Jcrop是否像您的目标190x190图像一样被锁定为1.0纵横比?Jcrop被锁定为1:1的正方形选择区域,但就我对imagecopyresampled的理解而言,这不会产生任何影响。对吗?是的,你是对的。我在想,不正确的纵横比可能会导致缩放效果,但它实际上只会导致一维拉伸或收缩。是否有现场示例或结果图像可供查看?我开始认为问题可能在别处。您的代码看起来不错。找到我的问题,请参阅问题中的post belowA客户端变量。有道理。感谢发布您的解决方案。Jcrop是否像目标190x190图像一样锁定为1.0纵横比?Jcrop锁定为1:1的正方形选择区域,但就我对imagecopyresampled的理解而言,这不会产生任何影响。对吗?是的,你是对的。我在想,不正确的纵横比可能会导致缩放效果,但它实际上只会导致一维拉伸或收缩。是否有现场示例或结果图像可供查看?我开始认为问题可能在别处。您的代码看起来不错。找到我的问题,请参阅问题中的post belowA客户端变量。有道理。感谢您发布您的解决方案。