Php 调整图像大小并裁剪图像
我想调整大小和作物的形象。250x250 我可以调整它的大小:Php 调整图像大小并裁剪图像,php,image,Php,Image,我想调整大小和作物的形象。250x250 我可以调整它的大小: $newfilename = "image.jpg"; if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $imagename = $newfilename; $source = $_FILES['new_image']['tmp_name']; $target = "
$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) ;
}
}
但我不知道调整大小后如何裁剪。
它能塑造形象。如果我有10000x1000的图像,这是个错误的决定
imagecopyresized($tn, $image, 0, 0, 0, 0, 250, 250, 250, 250) ;
您只需使用PHP获取图像并以HTML格式回显它们,并对它们应用特定的宽度/高度。使用
imagecreatetruecolor
创建具有指定裁剪大小的新图像,然后使用imagecopy
将其从旧画布复制到新画布
示例(未测试):
试着用crupie。github url是
我用过它,效果很好这根本不是他想要的,老实说,这是一种非常懒惰和浪费的方式代码>只裁剪原始图像,然后将
imagecopy
更改为imagecopy($croped,$tn,0,0,$cropLeft,$cropTop,$modwidth,$modheight)代码>他要求的是后端解决方案,而不是另一个npm包。
$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
}
}