我想调整图像大小并裁剪图像。250x250
我可以调整它的大小:
$newfilename = "image.jpg";
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $newfilename;
$source = $_FILES['new_image']['tmp_name'];
$target = "img/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "img/" . $imagepath;
$file = "img/" . $imagepath;
list($width, $height) = getimagesize($file) ;
$modwidth = 250;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresized($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
}
}
但我不知道调整大小后如何裁剪它。它裁剪图像。如果我有10000x10000的图像,这是一个糟糕的决定
imagecopyresized($tn, $image, 0, 0, 0, 0, 250, 250, 250, 250) ;
使用imagecreatetruecolor
创建具有指定裁剪大小的NEW图像,然后使用imagecopy
将其从旧画布复制到新画布。
示例(未测试):
$newfilename = "image.jpg";
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $newfilename;
$source = $_FILES['new_image']['tmp_name'];
$target = "img/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "img/" . $imagepath;
$file = "img/" . $imagepath;
list($width, $height) = getimagesize($file) ;
$modwidth = 250;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$cropped = imagecreatetruecolor(250, 250);
$cropLeft = 0; // start from left
$cropTop = 0; // start from top
$image = imagecreatefromjpeg($file) ;
imagecopyresized($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagecopy($cropped, $image, 0, 0, $cropLeft, $cropTop, $modwidth, $modheight);
imagejpeg($cropped, $save, 100); // save cropped image
// imagejpeg($tn, $save, 100) ; // save resized image
}
}
尝试使用croppie。github url为http://foliotek.github.io/Croppie/我用过它,它的效果很好
您可以简单地使用PHP获取图像,并在HTML中回显它们,并对它们应用特定的宽度/高度。