我想将图片尺寸从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
?>
正如您从上面的两组代码中看到的那样,第一组调整图片大小,然后将其保存到文件夹中。第二组代码裁剪图片,然后将其保存到文件夹中。我的问题是......在第一组代码调整图片大小后,是否有必要将其保存到文件夹中才能裁剪它?如果有必要,那么我需要编写新的代码行来从第二组代码的文件夹中检索已调整大小的图片来裁剪它?如果没有必要,在调整图片大小后,如何将图片传递给第二组代码进行裁剪?
答案 0 :(得分: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);
?>
答案 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(px)。 $ modwidth和$ modheight都应设置为500.这样可以保持原始图像的整个高度,并从左侧切出50px,从右侧切掉50px。剩余的500px裁剪为50px。
答案 2 :(得分:1)
链接到我写的关于如何将任何大小的图像调整为任意大小的博客文章。包括letterboxing和crop-to-fit的选项。