PHP调整图像大小然后裁剪图像问题

3

我希望将一张图片的大小从600px * 500px减小到60px * 50px,然后裁剪成50px * 50px的大小。我有两组代码,其中一组是用于减小图像尺寸的,另一组是用于裁剪图像的。问题在于它们分别工作,如何将这两组代码合并起来使它们能够一起工作?以下是我的代码:

<?php

//codes of group A - Reduce the size of image from 600px * 500px to 60px * 50px   
    $save2 = "images/users/" . $image_name_2; //This is the new file you saving
    list($width2, $height2) = getimagesize($file) ; 
    $modwidth2 = 50; 
    $diff2 = $width2 / $modwidth2;
    $modheight2 = $height2 / $diff2; 
    $tn2 = imagecreatetruecolor($modwidth2, $modheight2) ; 
    $image2 = imagecreatefromjpeg($file) ; 
    imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2) ; 
    imagejpeg($tn2, $save2, 100) ; 



//codes of group B - Crop the image from 60px * 50px to 50px * 50px 
    $save3 = "images/users/" . $image_name_3;  
    list($width3, $height3) = getimagesize($file) ; 
    $modwidth3 = 60;  
    $diff3 = $width3 / $modwidth3;
    $modheight3 = $height3 / $diff3; 

    $left = 0; 
    $top = 0;

    $cropwidth = 50; //thumb size
    $cropheight = 50;

    $tn3 = imagecreatetruecolor($cropwidth, $cropheight) ; 
    $image3 = imagecreatefromjpeg($file) ; 
    imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ; 
    imagejpeg($tn3, $save3, 100) ; //save the cropped image
?>

从上面的两组代码可以看出,第一组将图片调整大小然后保存到文件夹中。第二组代码则是将图片裁剪后也保存到了同一个文件夹中。我的问题是...在第一组代码调整图片大小后,是否需要先将其保存到文件夹中才能进行裁剪?如果是必需的,那么我需要编写新的代码行来从文件夹中检索已调整大小的图片,并将其传递给第二组代码进行裁剪吗?如果不是必需的,在调整大小后,我应该如何将图片传递给第二组代码进行裁剪呢?


你在哪里调整图片大小?能不能说出具体的代码行数? - Arman P.
嗨,我已经更新了我的问题,添加了调整图片大小的代码。请看一下。谢谢。 - zac1987
让我澄清一下:您有一个分辨率为900x600的图片,想要调整图片大小,使得较小的一侧为50(在这种情况下将图像调整为75x50),然后将其裁剪为50x50。是这样吗? - Arman P.
@Arman P,是的,你说得对。 - zac1987
使用此类: https://github.com/qaribhaider/php-image-resize ,1-将图像调整为精确的宽度高度,2-使用裁剪方法进行调整大小。 - Qarib Haider
3个回答

5

你好 @zac1987。
这里提供一份完整的PHP代码,可以生成指定大小的正方形缩略图而不会拉伸图片。该代码支持png和jpg/jpeg图像扩展名。只需更改设置部分即可。您可以复制粘贴代码并在您的Web服务器上进行测试。

<?php

    // SETTINGS
    $image_name = 'file.jpg';    // Full path and image name with extension
    $thumb_name = 'thumbnail';   // Generated thumbnail name without extension
    $thumb_side = 100;           // Desired thumbnail side size
    // END OF SETTINGS

    $image_extension = explode('.', $image_name); // I assume that images are named only following 'imagename.ext' pattern

    if (preg_match('/jpg|jpeg/', $image_extension[1])) {
        $src_image = imagecreatefromjpeg($image_name);
        $image_extension = 'jpg';
    } else if (preg_match('/png/', $image_extension[1])) {
        $src_image = imagecreatefrompng($image_name);
        $image_extension = 'png';
    }

    $src_width = imageSX($src_image);   // Width of the original image
    $src_height = imageSY($src_image);  // Height of the original image

    $min_side = min($src_width, $src_height);

    /*********** If you need this part uncomment it
    $ratio = $min_side / $thumb_width;
    $new_width = floor($src_width / $ratio);
    $new_height = floor($src_height / $ratio);
    **********************************************/    

    $dst_image = imagecreatetruecolor($thumb_side, $thumb_side);

    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $thumb_side, $thumb_side, $min_side, $min_side);

    switch ($image_extension)
    {
        case 'jpg':
            imagejpeg($dst_image, $thumb_name . '.jpg', 100);
            break;
        case 'png':
            imagepng($dst_image, $thumb_name . '.jpg', 100);
            break;
    }

    imagedestroy($src_image);
    imagedestroy($dst_image);

?>

@zac1987 不用谢,如果要从其他坐标裁剪(不是左:0,上:0),只需编辑 imagecopyresampled 中的第三个和第四个零即可。 - Arman P.
是的,我已将坐标分配给你的代码。 if($src_width > $src_height){ $top=0; $left=70;}elseif ($src_width < $src_height){$top=70; $left=0;} 再次感谢你的帮助之手。 - zac1987

1

1
$modwidth3 = 500;  //I resize the picture width to smaller as 60px.
$diff3 = $width3 / $modwidth3;
$modheight3 = $height3 / $diff3;  //I resize the picture height to smaller.

$left = 1; //getting the left and top coordinate
$top = 1;

$cropwidth = 50; //thumb size
$cropheight = 50;

imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ; 

$modwidth3$modheight3需要对应所需裁剪的图像的宽度和高度。此外,$top$left应该匹配您想要裁剪的区域的左上角坐标。如果原始图像为600x500像素,您想要调整大小并裁剪为50x50像素,则应将$top设置为0,$left设置为50(像素)。$modwidth和$modheight都应设置为500。这保留了原始图像的完整高度,并从左侧和右侧各裁剪了50像素。其余的500像素被裁剪为50像素。


原始图像大小不固定,可能是800px * 1200px,因此您关于削减两侧50px的概念对此无效。此外,如果我按照您所说的“$modwidth和$modheight应设置为500”的方式进行操作,则图像将看起来被拉伸,因为原始图像大小为500px * 600px。 - zac1987
是的,这段代码并不完整,你可能会在计算中发现错误。我没有测试过这段代码。但是,代码下面的文本应该会给你提供很多关于如何解决问题的信息。 - Arjan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接