【发布时间】:2011-05-09 19:53:40
【问题描述】:
我想将图片大小从 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
?>
从上面的两组代码中可以看出,第一组调整图片大小,然后将其保存到文件夹中。第二组代码裁剪图片,然后将其保存到文件夹中。我的问题是......在第一组代码调整图片大小后,是否有必要将其保存到文件夹中才能裁剪它?如果有必要,那么我需要编写新的代码行以从文件夹中检索调整大小的图片,以便第二组代码进行裁剪?如果没有必要,在调整图片大小后,如何将图片传递给第二组代码进行裁剪?
【问题讨论】:
-
你在哪里调整图片大小?你能提一下这条线吗?
-
嗨,我已经通过添加调整图片代码来更新我的问题。请看一看。谢谢。
-
让我澄清一下:你有分辨率的图片,例如900x600,并且您想调整图像的大小,使较小的边为 50(在本例中将图像大小调整为 75x50),然后将其裁剪为 50x50。不是吗?
-
@Arman P,是的,你是对的。
-
使用此类:github.com/qaribhaider/php-image-resize,1- 将图像大小调整为精确的宽度高度 2- 使用裁剪方法调整大小
标签: php gd crop image-resizing