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

php调整图像大小,然后裁剪图像问题,php,gd,crop,image-resizing,Php,Gd,Crop,Image Resizing,我想将图片大小从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

我想将图片大小从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
?>

从上面的两组代码中可以看出,第一组调整pic的大小,然后将其保存到文件夹中。第二组代码裁剪图片,然后将其保存到文件夹中。我的问题是。。。在第一组代码调整图片大小后,是否需要将其保存到文件夹中,然后才能进行裁剪?如果有必要,那么我需要写新的代码行,从文件夹中检索调整大小的图片,以便第二组代码进行裁剪?如果没有必要,在调整图片大小后,我如何将图片传递给第二组代码进行裁剪

$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。这将保持原始图像的完整高度,并从左侧和右侧分别切下50px和50px。剩下的500px被裁剪为50px。

链接到我写的一篇博客文章,文章讲述了如何将任意大小的图像调整为任意大小。包括用于字母装箱和裁剪以适合的选项

给你@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);

?>


在哪里调整图片的大小?你能提到这行吗?嗨,我已经通过添加调整大小的图片代码更新了我的问题。请看一看。谢谢。让我澄清一下:您有分辨率为900x600的图片,您希望调整图像的大小,使较小的一面为50(在本例中,将图像调整为75x50),然后将其裁剪为50x50。是吗?@Arman P,是的,你是对的。使用这个类:,1-调整图像大小到精确的宽度高度2-使用裁剪方法调整图像大小原始图像大小不是固定的,可能是800px*1200px,所以你关于两边切割50px的概念不适用于此。此外,如果我按照你说的那样做,比如“$modwidth和$modheight应该设置为500”,那么图像看起来会拉伸,因为原始图像是500px*600px。是的,代码不完整,您可能会在计算中发现错误。我没有测试代码。但是,代码下面的文本应该会为您提供大量有关如何解决问题的信息。@zac1987欢迎您同时从其他坐标裁剪(不是左:0,顶部:0)-只需在
imagecopyresampled
中编辑第3和第4个零。是的,我已将坐标分配到您的代码中。如果($src_width>$src_height){$top=0;$left=70;}elseif($src_width<$src_height){$top=70;$left=0;}再次感谢您的帮助。