php调整图像大小然后裁剪图像问题
我想将图片尺寸从 600px * 500px 缩小到 60px * 50px 尺寸,然后裁剪成 50px * 50px。我有两组代码,1组是缩小图像的大小,另外1组是裁剪图像。问题是它们是分开工作的,如何将这两组代码组合起来使它们一起工作?下面是我的代码:
<?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
?>
正如您从上面的 2 组代码中看到的,第一组调整图片大小然后将其保存到文件夹中。第二组代码裁剪图片然后将其也保存到文件夹中。我的问题是......第一组代码调整图片大小后,是否需要将其保存到文件夹中才能裁剪?如果有必要,那么我需要编写新的代码行来从文件夹中检索调整大小的图片,以便第二组代码来裁剪它?如果不需要,调整图片大小后,如何将图片传递给第二组代码进行裁剪?
I want to reduce a picture size from 600px * 500px to 60px * 50px size, then crop it become 50px *50px. I have two groups of codes, 1 is to reduce the size of image, other 1 is to crop the image. The problem is they works separately, how to combine this two groups of codes to make them work together? Below is my codes :
<?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
?>
As you can see from 2 groups of codes above, 1st group resize the pic then save it to a folder. 2nd group of codes crop the pic then save it into the folder too. My question is ... After 1st group of codes resize the picture, is it necessary to save it into folder before I can crop it? If it is necessary, then I need to write new lines of codes to retrieve the resized pic from the folder for 2nd group of codes to crop it? If it is not necessary, after resizing the pic, how do I pass the pic to 2nd group of codes to crop it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在这里@zac1987。
完整的 PHP 代码生成所需大小的方形缩略图,无需拉伸图像。该代码支持 png 和 jpg/jpeg 图像扩展名。只需将设置部分更改为所需的部分即可。您可以复制粘贴代码并在您的网络服务器上进行测试。
Here you are @zac1987.
Full PHP code generating square thumbnail of desired size without stretching image. The code supports both png and jpg/jpeg image extensions. Simply change settings part to desired one. You can copy paste the code and test it on your web server.
$modwidth3
和$modheight3
需要对应于要裁剪的图像的宽度和高度。此外,$top
和$left
应与要裁剪区域的左上角坐标相匹配。如果原始图像为 600x500 像素,并且您想要调整大小并裁剪为 50x50,则应将 $top 设置为 0,将 $left 设置为 50 (px)。 $modwidth 和 $modheight 都应设置为 500。这将保持原始图像的完整高度,并从左侧切掉 50px,从右侧切掉 50px。剩余的 500 像素被裁剪为 50 像素。$modwidth3
and$modheight3
need to correspond to the width and height of the image you want to crop. Also$top
and$left
should match the top left coordinate of the area you want to crop. If the original image is 600x500 px and you want to resize and crop to 50x50, you should set $top to 0 and $left to 50 (px). Both $modwidth and $modheight should be set to 500. That keeps the full height of the original image and cuts away 50px from the left hand side and 50px from the right hand side. The remaining 500px is cropped to 50px.链接到我写的关于如何将任何尺寸的图像调整为任意尺寸的博客文章。包括信箱和裁剪以适合的选项。
http://www.spotlesswebdesign.com/blog.php?id=1
link to a blog article i wrote about how to resize any size image to any arbitrary size. includes options for letterboxing and crop-to-fit.
http://www.spotlesswebdesign.com/blog.php?id=1