簡體   English   中英

PHP-imagecopyresampled

[英]php - imagecopyresampled

我正在嘗試使用jcrop允許用戶創建其圖像的縮略圖,它將為190x190 pixels

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

我很不知所措,我檢查了四個$_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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM