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